Class: Fech::Translator

Inherits:
Object
  • Object
show all
Defined in:
lib/fech/translator.rb

Overview

Fech::Translator stores a collection of Procs which are associated with one or many field types, row types and filing versions. When a row that matches all of these is mapped in Filing, the Proc is run on that value.

:action => :convert alters a single value in place.

:combine creates a new row out of others.

It also stores a set of aliases, allowing fields on the returned Hash of mapped data to be accessed by other names.

Constant Summary collapse

NAME_PARSER =
People::NameParser.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Translator

Returns a new instance of Translator.



20
21
22
23
24
25
26
# File 'lib/fech/translator.rb', line 20

def initialize(opts = {})
  @cache        = {}
  @aliases      = []
  @translations = []
  # op-in default translation packs
  add_default_translations(opts[:include] || [])
end

Instance Attribute Details

#aliasesObject

Returns the value of attribute aliases.



16
17
18
# File 'lib/fech/translator.rb', line 16

def aliases
  @aliases
end

#cacheObject

Returns the value of attribute cache.



16
17
18
# File 'lib/fech/translator.rb', line 16

def cache
  @cache
end

#translationsObject

Returns the value of attribute translations.



16
17
18
# File 'lib/fech/translator.rb', line 16

def translations
  @translations
end

Class Method Details

.applicable_translation?(translation, opts) ⇒ Boolean

Given a translation and any or all of field, version, row, action: Returns true if all the given options are compatible with those specified in the translation.

Parameters:

  • translation (Hash)

    the translation being tested for relevance

  • opts (Hash)

    a customizable set of options

Options Hash (opts):

  • :field (String)

    the current field’s value

  • :version (String)

    the current filing’s version

  • :row (String)

    the row type

  • :action (Symbol)

    match only :combine or :convert translations

Returns:

  • (Boolean)


52
53
54
# File 'lib/fech/translator.rb', line 52

def self.applicable_translation?(translation, opts)
  opts.keys.all? { |k| translation[k].match(opts[k].to_s) }
end

Instance Method Details

#alias(new_name, old_name, row = /.*/) ⇒ Object

Allows @old_name on @row to be accessible on the returned hash as @new_name

t.alias(:new, :old, “sa”)

Parameters:

  • new_name (Symbol)

    the given field will be accessible using this token

  • old_name (Symbol)

    the existing field whose value to alias

  • row (Symbol, Regex) (defaults to: /.*/)

    the types of rows this alias should be applied to



87
88
89
90
91
92
93
# File 'lib/fech/translator.rb', line 87

def alias(new_name, old_name, row=/.*/)
  aliases << {
    :row => Fech.regexify(row),
    :alias => new_name,
    :for => old_name
  }
end

#combine(args = {}, &block) ⇒ Object

Adds a translation that uses other fields to create a new one

t.combine(:row => “sa”, :field => :net_individual_contributions) do |row|

row.individual_contributions - row.individual_refunds

end

Parameters:

  • opts (Hash)

    a customizable set of options



76
77
78
# File 'lib/fech/translator.rb', line 76

def combine(args={}, &block)
  add_translation(args.merge(:action => :combine), &block)
end

#convert(args = {}, &block) ⇒ Object

Adds a tranlation for preprocessing a single field’s value

t.convert(:row => /^sa/, :field => :date_coverage_from) { |v| Date.parse(v) }

Parameters:

  • opts (Hash)

    a customizable set of options



63
64
65
# File 'lib/fech/translator.rb', line 63

def convert(args={}, &block)
  add_translation(args.merge(:action => :convert), &block)
end

#get_translations(opts) ⇒ Object

Returns list of all translations that should be applied to values of specified row and field.

Parameters:

  • opts (Hash)

    a customizable set of options

Options Hash (opts):

  • :field (String)

    the current field’s value

  • :version (String)

    the current filing’s version

  • :row (String)

    the row type

  • :action (Symbol)

    match only :combine or :convert translations



35
36
37
38
39
40
41
# File 'lib/fech/translator.rb', line 35

def get_translations(opts)
  key = [:field, :row, :version, :action].collect { |key| opts[key] }.join(":")
  @cache[key] ||= \
  procs = translations.collect do |t|
    t if self.class.applicable_translation?(t, opts)
  end.compact
end