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

Defined in:
lib/mongo_db/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_db/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

#available_as_yaml(name) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/mongo_db/model/attribute_convertors.rb', line 41

def available_as_yaml name
  raise "delimiter not specified for :#{name} field!" unless delimiter
  method = "#{name}_as_string"
  define_method method do
    self.send(name).join(delimiter)
  end
  define_method "#{method}=" do |value|
    value = (value || "").split(delimiter.strip).collect{|s| s.strip}
    self.send "#{name}=", value
  end
end