Module: MongoMapper::Plugins::AttributeConvertors::ClassMethods
- Defined in:
- lib/mongo_mapper_ext/mongo_mapper/plugins/attribute_convertors.rb
Constant Summary collapse
- ATTRIBUTE_CONVERTORS =
{ line: { from_string: -> s {(s || "").split(',').collect{|s| s.strip}}, to_string: -> v {v.join(', ')} }, column: { from_string: -> s {(s || "").split("\n").collect{|s| s.strip}}, to_string: -> v {v.join("\n")} }, yaml: { from_string: -> s {YAML.load s rescue {}}, to_string: -> v { # MongoMapper uses it's internal Hash that doesn't support to_yaml hash = {}; v.each{|k, v| hash[k] = v} hash.to_yaml.strip } }, json: { from_string: -> s {JSON.parse s rescue {}}, to_string: -> v { # MongoMapper uses it's internal Hash that doesn't support to_yaml hash = {}; v.each{|k, v| hash[k] = v} hash.to_json.strip } } }
Instance Method Summary collapse
- #available_as_string(key_name, converter_name) ⇒ Object
- #available_as_yaml(key_name) ⇒ Object
-
#key(*args) ⇒ Object
supporf for :as_string option.
Instance Method Details
#available_as_string(key_name, converter_name) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/mongo_mapper_ext/mongo_mapper/plugins/attribute_convertors.rb', line 46 def available_as_string key_name, converter_name converter = ATTRIBUTE_CONVERTORS[converter_name] raise "unknown converter name :#{converter_name} for :#{key_name} key!" unless converter from_string, to_string = converter[:from_string], converter[:to_string] key_name_as_string = "#{key_name}_as_string".to_sym define_method key_name_as_string do cache[key_name_as_string] ||= to_string.call(self.send(key_name)) end define_method "#{key_name_as_string}=" do |value| cache.delete key_name_as_string self.send "#{key_name}=", from_string.call(value) end end |
#available_as_yaml(key_name) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/mongo_mapper_ext/mongo_mapper/plugins/attribute_convertors.rb', line 61 def available_as_yaml key_name raise "delimiter not specified for :#{key_name} key!" unless delimiter method = "#{key_name}_as_string" define_method method do self.send(key_name).join(delimiter) end define_method "#{method}=" do |value| value = (value || "").split(delimiter.strip).collect{|s| s.strip} self.send "#{key_name}=", value end end |
#key(*args) ⇒ Object
supporf for :as_string option
36 37 38 39 40 41 42 43 44 |
# File 'lib/mongo_mapper_ext/mongo_mapper/plugins/attribute_convertors.rb', line 36 def key(*args) key = super if converter_name = key.[:as_string] key_name = key.name.to_sym available_as_string key_name, converter_name attr_protected "#{key_name}_as_string".to_sym if key.[:protected] end key end |