Module: DataTranslation::Destination::ClassMethods

Defined in:
lib/data_translation.rb

Instance Method Summary collapse

Instance Method Details

#data_translation_map(name) {|| ... } ⇒ Object

Given the name of a mapping, returns the existing mapping with that name or creates one with that name and yields it if a block is given. Returns the DataTranslation mapping for the specified name.

Yields:

  • ()


233
234
235
236
237
238
239
240
# File 'lib/data_translation.rb', line 233

def data_translation_map(name) # yields DataTranslation
  @dt_mappings       ||= {}
  @dt_mappings[name] ||= DataTranslation.new

  yield @dt_mappings[name] if block_given?

  @dt_mappings[name]
end

#from_source(name, source) ⇒ Object

Given the name of a mapping and a source object, transforms the source object based upon the specified mapping and attempts to process the results using one of several methods that are checked in the following order:

  • DataTranslation#processor defined block

  • Destination class initialize_from_data_translation

  • Destination class default constructor

with the resulting hash if that method is not defined. Returns an instance of the class based upon the source data.



218
219
220
221
222
223
224
225
226
227
228
# File 'lib/data_translation.rb', line 218

def from_source(name, source)
  dtm = data_translation_map(name)

  if dtm.processor
    dtm.from_source(source)
  elsif respond_to?(:initialize_from_data_translation)
    initialize_from_data_translation(dtm.transform(source))
  else
    new(dtm.transform(source))
  end
end

#stub_data_translation_from_column_names(name = 'name') ⇒ Object

Returns a string containing a sample DataTranslation mapping for this instance based upon actual column names (assuming this is an ActiveRecord class or another class that provides an array of string column/attribute names via a column_names class method).

Passing the name argument sets it as the translation name in the output.



248
249
250
251
252
253
254
255
256
257
258
# File 'lib/data_translation.rb', line 248

def stub_data_translation_from_column_names(name = 'name')
  map = ["DataTranslation.destination(#{self}, :#{name}) do |dtm|"]

  column_names.each do |col|
    map << "\tdtm.link :#{col}, :#{col}"
  end

  map << "end"

  map.join("\n")
end