Module: Omnis::Transformer::InstanceMethods

Defined in:
lib/omnis/transformer.rb

Instance Method Summary collapse

Instance Method Details

#__extract(property, source) ⇒ Object



68
69
70
71
72
73
74
75
# File 'lib/omnis/transformer.rb', line 68

def __extract(property, source)
  value = property_value(property, source)
  if property.format
    property.format.call(value)
  else
    value
  end
end

#property_value(property, source) ⇒ Object



77
78
79
80
81
# File 'lib/omnis/transformer.rb', line 77

def property_value(property, source)
  value = property.extract(source)
  return property.default if property.default && (value == Nothing || value.nil?)
  return self.class.apply_to_value(value)
end

#to_procObject

provides a Proc to the transform method, for use e.g. with Mongo documents If you want to cache a transformer for reuse, you can cache just this Proc



90
91
92
# File 'lib/omnis/transformer.rb', line 90

def to_proc
  method(:transform).to_proc
end

#transform(source) ⇒ Object



83
84
85
86
# File 'lib/omnis/transformer.rb', line 83

def transform(source)
  result = Hash[self.class.properties.map do |k, v| [k, __extract(v, source)] end]
  respond_to?(:to_object) ? to_object(result) : result
end