Module: Mongo::Model::AttributeConvertors::ClassMethods

Defined in:
lib/mongo/model/attribute_convertors.rb

Instance Method Summary collapse

Instance Method Details

#available_as_string(name, converter_name) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/mongo/model/attribute_convertors.rb', line 25

def available_as_string name, converter_name
  converter = CONVERTORS[converter_name]
  raise "unknown converter name :#{converter_name} for :#{name} field!" unless converter

  from_string, to_string = converter[:from_string], converter[:to_string]
  name_as_string = "#{name}_as_string".to_sym
  define_method name_as_string do
    _cache[name_as_string] ||= to_string.call(send(name))
  end

  define_method "#{name_as_string}=" do |value|
    _cache.delete name_as_string
    self.send "#{name}=", from_string.call(value)
  end
end