Class: Fech::Translator
- Inherits:
-
Object
- Object
- Fech::Translator
- 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
-
#aliases ⇒ Object
Returns the value of attribute aliases.
-
#cache ⇒ Object
Returns the value of attribute cache.
-
#translations ⇒ Object
Returns the value of attribute translations.
Class Method Summary collapse
-
.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.
Instance Method Summary collapse
-
#alias(new_name, old_name, row = /.*/) ⇒ Object
Allows @old_name on @row to be accessible on the returned hash as @new_name.
-
#combine(args = {}, &block) ⇒ Object
Adds a translation that uses other fields to create a new one.
-
#convert(args = {}, &block) ⇒ Object
Adds a tranlation for preprocessing a single field’s value.
-
#get_translations(opts) ⇒ Object
Returns list of all translations that should be applied to values of specified row and field.
-
#initialize(opts = {}) ⇒ Translator
constructor
A new instance of Translator.
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
#aliases ⇒ Object
Returns the value of attribute aliases.
16 17 18 |
# File 'lib/fech/translator.rb', line 16 def aliases @aliases end |
#cache ⇒ Object
Returns the value of attribute cache.
16 17 18 |
# File 'lib/fech/translator.rb', line 16 def cache @cache end |
#translations ⇒ Object
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.
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”)
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
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) }
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.
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 |