Module: MongoMapper::Plugins::Keys::ClassMethods
- Defined in:
- lib/mongo_mapper/plugins/keys.rb
Instance Method Summary collapse
- #from_mongo(value) ⇒ Object
- #inherited(descendant) ⇒ Object
- #key(*args) ⇒ Object
- #key?(key) ⇒ Boolean
- #keys ⇒ Object
-
#load(attrs) ⇒ Object
load is overridden in identity map to ensure same objects are loaded.
- #object_id_key?(name) ⇒ Boolean
- #object_id_keys ⇒ Object
- #to_mongo(instance) ⇒ Object
- #using_object_id? ⇒ Boolean
Instance Method Details
#from_mongo(value) ⇒ Object
52 53 54 55 |
# File 'lib/mongo_mapper/plugins/keys.rb', line 52 def from_mongo(value) return nil if value.nil? value.is_a?(self) ? value : load(value) end |
#inherited(descendant) ⇒ Object
12 13 14 15 |
# File 'lib/mongo_mapper/plugins/keys.rb', line 12 def inherited(descendant) descendant.instance_variable_set(:@keys, keys.dup) super end |
#key(*args) ⇒ Object
21 22 23 24 25 26 27 28 29 |
# File 'lib/mongo_mapper/plugins/keys.rb', line 21 def key(*args) Key.new(*args).tap do |key| keys[key.name] = key create_accessors_for(key) create_key_in_descendants(*args) create_indexes_for(key) create_validations_for(key) end end |
#key?(key) ⇒ Boolean
31 32 33 |
# File 'lib/mongo_mapper/plugins/keys.rb', line 31 def key?(key) keys.keys.include?(key.to_s) end |
#keys ⇒ Object
17 18 19 |
# File 'lib/mongo_mapper/plugins/keys.rb', line 17 def keys @keys ||= HashWithIndifferentAccess.new end |
#load(attrs) ⇒ Object
load is overridden in identity map to ensure same objects are loaded
58 59 60 61 62 63 64 65 66 |
# File 'lib/mongo_mapper/plugins/keys.rb', line 58 def load(attrs) return nil if attrs.nil? begin klass = attrs['_type'].present? ? attrs['_type'].constantize : self klass.new(attrs, true) rescue NameError new(attrs, true) end end |
#object_id_key?(name) ⇒ Boolean
43 44 45 |
# File 'lib/mongo_mapper/plugins/keys.rb', line 43 def object_id_key?(name) object_id_keys.include?(name.to_sym) end |
#object_id_keys ⇒ Object
39 40 41 |
# File 'lib/mongo_mapper/plugins/keys.rb', line 39 def object_id_keys keys.keys.select { |key| keys[key].type == ObjectId }.map(&:to_sym) end |
#to_mongo(instance) ⇒ Object
47 48 49 50 |
# File 'lib/mongo_mapper/plugins/keys.rb', line 47 def to_mongo(instance) return nil if instance.nil? instance.to_mongo end |
#using_object_id? ⇒ Boolean
35 36 37 |
# File 'lib/mongo_mapper/plugins/keys.rb', line 35 def using_object_id? object_id_key?(:_id) end |