Class ConsoleUpdate::Filter

  1. lib/console_update/filter/yaml.rb
  2. lib/console_update/filter.rb
  3. show all
Parent: Object

A filter converts database records to a string and vice versa. A database record is represented as a hash of attributes with stringified keys. Each hash should have an id attribute to map it to its database record. To create your own filter, create a module in the ConsoleUpdate::Filter namespace. Although the name of the module can be anything, if only the first letter is capitalized then a simple lowercase name of the filter can be used with ConsoleUpdate.filter() (ie :yaml instead of Yaml). For now, new filters have to be manually loaded/required.

A filter should have two methods defined, string_to_hashes() and hashes_to_string(). For a good example of a filter see ConsoleUpdate::Filter::Yaml.

Methods

public class

  1. new

public instance

  1. hashes_to_string
  2. string_to_hashes

Public class methods

new (filter_type)

Creates a filter given a type.

[show source]
# File lib/console_update/filter.rb, line 20
    def initialize(filter_type)
      @filter_type = filter_type
      begin
        filter_module = self.class.const_get(filter_type.to_s.gsub(/^([a-z])/) {|e| $1.upcase })
        self.extend(filter_module)
      rescue NameError
        raise FilterNotFoundError
      end
    end

Public instance methods

hashes_to_string (hashes)

Takes an an array of hashes representing database records and converts them to a string for editing.

[show source]
# File lib/console_update/filter.rb, line 31
    def hashes_to_string(hashes)
      raise AbstractMethodError
    end
string_to_hashes (string)

Takes the string from the updated file and converts back to an array of hashes.

[show source]
# File lib/console_update/filter.rb, line 36
    def string_to_hashes(string)
      raise AbstractMethodError
    end