Class: ConsoleUpdate::Filter

Inherits:
Object
  • Object
show all
Defined in:
lib/console_update/filter.rb,
lib/console_update/filter/yaml.rb

Overview

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.

Defined Under Namespace

Modules: Yaml Classes: AbstractMethodError, FilterNotFoundError

Instance Method Summary collapse

Constructor Details

#initialize(filter_type) ⇒ Filter

Creates a filter given a type.



20
21
22
23
24
25
26
27
28
# 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

Instance Method Details

#hashes_to_string(hashes) ⇒ Object

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



31
32
33
# File 'lib/console_update/filter.rb', line 31

def hashes_to_string(hashes)
  raise AbstractMethodError
end

#string_to_hashes(string) ⇒ Object

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



36
37
38
# File 'lib/console_update/filter.rb', line 36

def string_to_hashes(string)
  raise AbstractMethodError
end