Class: DataTranslation
- Inherits:
-
Object
- Object
- DataTranslation
- Defined in:
- lib/data_translation.rb
Overview
Simple class to provide an easy way to map and transform data.
Example Usage
agent = DataTranslation.new do |m|
m.option :strict, true
m.set 'source_id', 1
m.link 'login', 'Username'
m.link 'first_name', 'FirstName'
m.link 'last_name', 'LastName'
m.link 'phone_number', lambda {|source| "(#{source['Area']}) #{source['Phone']}"}
m.processor do |values|
Agent.find_or_create_by_source_id_and_login(values['source_id'], values['login'])
end
end
source = => ‘spatterson’,
'FirstName' => 'Scott',
'LastName' => 'Patterson',
'Area' => '123',
'Phone' => '456-7890'
results = agent.transform(source)
puts results.inspect # => {“phone_number” => “(123) 456-7890”,
# "last_name" => "Patterson",
# "login" => "spatterson",
# "first_name" => "Scott"
# "source_id" => 1}
new_object = agent.from_source(source)
Defined Under Namespace
Modules: Destination Classes: NonresponsiveSource
Constant Summary collapse
- VERSION =
'1.1.0'
Instance Attribute Summary collapse
-
#mappings ⇒ Object
readonly
Returns the value of attribute mappings.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#static_values ⇒ Object
readonly
Returns the value of attribute static_values.
Class Method Summary collapse
-
.destination(klass, name = nil) ⇒ Object
Includes DataTranslation::Destination in the specified class (klass).
Instance Method Summary collapse
-
#from_source(source, options = {}) ⇒ Object
Given a source object, returns the results of #transform if no processor is defined or the results of calling the processor block as defined by #processor.
-
#initialize {|_self| ... } ⇒ DataTranslation
constructor
Constructor, yields self to allow for block-based configuration.
-
#link(to, from = nil, &block) ⇒ Object
Links a destination field to a source method, element, or lambda.
-
#option(name, value) ⇒ Object
Sets the option with name to value.
-
#processor(&block) ⇒ Object
If called without a block, returns the current block.
-
#remove_processor ⇒ Object
Removes the currently defined processor, if any.
-
#set(to, value) ⇒ Object
Sets a specified to field to value during transformation without regard to the source object.
-
#transform(source, options = {}) ⇒ Object
Given a source object, returns a new hash with elements as determined by the current mapping.
-
#update {|_self| ... } ⇒ Object
Yields the mapping object so that options and links can be applied within a block.
Constructor Details
#initialize {|_self| ... } ⇒ DataTranslation
Constructor, yields self to allow for block-based configuration.
Defaults: strict = true
56 57 58 59 60 61 62 63 |
# File 'lib/data_translation.rb', line 56 def initialize # yields self @mappings = {} @static_values = {} = {:strict => true} @processor = nil yield self if block_given? end |
Instance Attribute Details
#mappings ⇒ Object (readonly)
Returns the value of attribute mappings.
39 40 41 |
# File 'lib/data_translation.rb', line 39 def mappings @mappings end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
39 40 41 |
# File 'lib/data_translation.rb', line 39 def end |
#static_values ⇒ Object (readonly)
Returns the value of attribute static_values.
39 40 41 |
# File 'lib/data_translation.rb', line 39 def static_values @static_values end |
Class Method Details
.destination(klass, name = nil) ⇒ Object
Includes DataTranslation::Destination in the specified class (klass). If a name is given, yields and returns a DataTranslation for that name. i.e. DataTranslation.destination(PlainClass, :source_name) {|dtm| dtm.link …}
44 45 46 47 48 49 50 51 52 |
# File 'lib/data_translation.rb', line 44 def self.destination(klass, name = nil) # No need to include ourself again if we've already extended the class klass.class_eval("include(Destination)") unless klass.include?(Destination) if name klass.data_translation_map(name) {|dtm| yield dtm} if block_given? klass.data_translation_map(name) end end |
Instance Method Details
#from_source(source, options = {}) ⇒ Object
Given a source object, returns the results of #transform if no processor is defined or the results of calling the processor block as defined by #processor.
126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/data_translation.rb', line 126 def from_source(source, = {}) results = transform(source, ) if @processor if @processor.arity == 1 @processor.call(results) else @processor.call(results, source) end else results end end |
#link(to, from = nil, &block) ⇒ Object
Links a destination field to a source method, element, or lambda. to may be a string, symbol, or any object that can be used as a hash key. If from is a lambda, called with one argument (the source passed to #transform). Alternatively, can be called with a block instead of a lambda or from.
link ‘field_name’, ‘FieldName’ link :field_name, ‘FieldName’ link :field_name, lambda {|source| source…} link(:field_name) {|source| source…}
92 93 94 |
# File 'lib/data_translation.rb', line 92 def link(to, from = nil, &block) @mappings[to] = block.nil? ? from : block end |
#option(name, value) ⇒ Object
Sets the option with name to value. Current options are:
:strict = boolean; true will raise a NonresponsiveSource exception if the source
object does not respond to a mapping.
false ignores non-existant fields in the source object.
70 71 72 |
# File 'lib/data_translation.rb', line 70 def option(name, value) [name.to_sym] = value end |
#processor(&block) ⇒ Object
If called without a block, returns the current block. If called with a block, sets the block that will be run with the results from #transform when #from_source is called. The results of the transformation will be passed if the block expects only one parameter. If the block expects two parameters both the results and the original source object will be provided.
101 102 103 104 105 106 107 |
# File 'lib/data_translation.rb', line 101 def processor(&block) # |transform_results [, source_data]| if block_given? @processor = block else @processor end end |
#remove_processor ⇒ Object
Removes the currently defined processor, if any.
110 111 112 |
# File 'lib/data_translation.rb', line 110 def remove_processor @processor = nil end |
#set(to, value) ⇒ Object
Sets a specified to field to value during transformation without regard to the source object. If you wish to link to source object data or use a lambda/block, use #link instead. to may be any object that can be stored as a Hash key.
set :field_name, ‘Static value’
79 80 81 |
# File 'lib/data_translation.rb', line 79 def set(to, value) @static_values[to] = value end |
#transform(source, options = {}) ⇒ Object
Given a source object, returns a new hash with elements as determined by the current mapping. Mapping is set by one or more calls to #link. Options passed to this method override the instance options. See #option for a list of options. #link values will override #set values.
118 119 120 121 122 |
# File 'lib/data_translation.rb', line 118 def transform(source, = {}) = .merge() apply_static_values().merge(apply_mappings(source, )) end |
#update {|_self| ... } ⇒ Object
Yields the mapping object so that options and links can be applied within a block.
141 142 143 |
# File 'lib/data_translation.rb', line 141 def update # yields self yield self end |