class MongoModel::Map
Constants
- HASH_CONVERTER
Public Class Methods
[](mapping)
click to toggle source
# File lib/mongomodel/support/map.rb, line 26 def [](mapping) raise "Exactly one mapping must be specified" unless mapping.keys.size == 1 from = mapping.keys.first to = mapping.values.first @map_class_cache ||= {} @map_class_cache[[from, to]] ||= begin map = Class.new(Map) map.from = from map.to = to map end end
from_converter()
click to toggle source
# File lib/mongomodel/support/map.rb, line 55 def from_converter @from_converter ||= Types.converter_for(from) end
from_mongo(hash)
click to toggle source
# File lib/mongomodel/support/map.rb, line 41 def from_mongo(hash) result = new hash.each_pair { |k, v| result[from_converter.from_mongo(k)] = instantiate(v) } result end
inspect()
click to toggle source
# File lib/mongomodel/support/map.rb, line 47 def inspect if self == Map "Map" else "Map[#{from} => #{to}]" end end
new(hash={})
click to toggle source
Calls superclass method
# File lib/mongomodel/support/map.rb, line 73 def initialize(hash={}) super() update(hash) end
to_converter()
click to toggle source
# File lib/mongomodel/support/map.rb, line 59 def to_converter @to_converter ||= Types.converter_for(to) end
Private Class Methods
instantiate(item)
click to toggle source
# File lib/mongomodel/support/map.rb, line 64 def instantiate(item) if item.is_a?(Hash) && item['_type'] item['_type'].constantize.from_mongo(item) else to_converter.from_mongo(item) end end
Public Instance Methods
[](key)
click to toggle source
Calls superclass method
# File lib/mongomodel/support/map.rb, line 82 def [](key) super(convert_key(key)) end
[]=(key, value)
click to toggle source
Calls superclass method
# File lib/mongomodel/support/map.rb, line 86 def []=(key, value) super(convert_key(key), convert_value(value)) end
delete(key)
click to toggle source
Calls superclass method
# File lib/mongomodel/support/map.rb, line 94 def delete(key) super(convert_key(key)) end
fetch(key, *args, &block)
click to toggle source
Calls superclass method
# File lib/mongomodel/support/map.rb, line 98 def fetch(key, *args, &block) super(convert_key(key), *args, &block) end
index(value)
click to toggle source
Calls superclass method
# File lib/mongomodel/support/map.rb, line 116 def index(value) super(convert_value(value)) end
key(value)
click to toggle source
Calls superclass method
# File lib/mongomodel/support/map.rb, line 120 def key(value) super(convert_value(value)) end
key?(key)
click to toggle source
Calls superclass method
# File lib/mongomodel/support/map.rb, line 102 def key?(key) super(convert_key(key)) end
merge(hash)
click to toggle source
Calls superclass method
# File lib/mongomodel/support/map.rb, line 134 def merge(hash) dup.update(super(hash)) end
merge!(hash)
click to toggle source
# File lib/mongomodel/support/map.rb, line 138 def merge!(hash) update(merge(hash)) end
replace(hash)
click to toggle source
# File lib/mongomodel/support/map.rb, line 129 def replace(hash) clear update(hash) end
store(key, value)
click to toggle source
Calls superclass method
# File lib/mongomodel/support/map.rb, line 90 def store(key, value) super(convert_key(key), convert_value(value)) end
to_mongo()
click to toggle source
# File lib/mongomodel/support/map.rb, line 78 def to_mongo HASH_CONVERTER.to_mongo(self) end
update(hash)
click to toggle source
# File lib/mongomodel/support/map.rb, line 124 def update(hash) hash.each_pair { |k, v| self[k] = v } self end
value?(value)
click to toggle source
Calls superclass method
# File lib/mongomodel/support/map.rb, line 110 def value?(value) super(convert_value(value)) end
Also aliased as: has_value?
values_at(*keys)
click to toggle source
Calls superclass method
# File lib/mongomodel/support/map.rb, line 142 def values_at(*keys) super(*keys.map { |k| convert_key(k) }) end
Private Instance Methods
convert_key(key)
click to toggle source
# File lib/mongomodel/support/map.rb, line 147 def convert_key(key) self.class.from_converter.cast(key) end
convert_value(value)
click to toggle source
# File lib/mongomodel/support/map.rb, line 151 def convert_value(value) self.class.to_converter.cast(value) end