class ConsoleUpdate::Filter

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.

Public Class Methods

new(filter_type) click to toggle source

Creates a filter given a type.

# 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) click to toggle source

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

# File lib/console_update/filter.rb, line 31
def hashes_to_string(hashes)
  raise AbstractMethodError
end
string_to_hashes(string) click to toggle source

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

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