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
53 54 55 56 |
# File 'lib/mongo_mapper/plugins/keys.rb', line 53 def from_mongo(value) return nil if value.nil? value.is_a?(self) ? value : load(value) end |
#inherited(descendant) ⇒ Object
12 13 14 15 16 |
# File 'lib/mongo_mapper/plugins/keys.rb', line 12 def inherited(descendant) key :_type, String unless keys.keys.include?(:_type) descendant.instance_variable_set(:@keys, keys.dup) super end |
#key(*args) ⇒ Object
22 23 24 25 26 27 28 29 30 |
# File 'lib/mongo_mapper/plugins/keys.rb', line 22 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
32 33 34 |
# File 'lib/mongo_mapper/plugins/keys.rb', line 32 def key?(key) keys.keys.include?(key.to_s) end |
#keys ⇒ Object
18 19 20 |
# File 'lib/mongo_mapper/plugins/keys.rb', line 18 def keys @keys ||= HashWithIndifferentAccess.new end |
#load(attrs) ⇒ Object
load is overridden in identity map to ensure same objects are loaded
59 60 61 62 63 64 65 66 67 |
# File 'lib/mongo_mapper/plugins/keys.rb', line 59 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
44 45 46 |
# File 'lib/mongo_mapper/plugins/keys.rb', line 44 def object_id_key?(name) object_id_keys.include?(name.to_sym) end |
#object_id_keys ⇒ Object
40 41 42 |
# File 'lib/mongo_mapper/plugins/keys.rb', line 40 def object_id_keys keys.keys.select { |key| keys[key].type == ObjectId }.map(&:to_sym) end |
#to_mongo(instance) ⇒ Object
48 49 50 51 |
# File 'lib/mongo_mapper/plugins/keys.rb', line 48 def to_mongo(instance) return nil if instance.nil? instance.to_mongo end |
#using_object_id? ⇒ Boolean
36 37 38 |
# File 'lib/mongo_mapper/plugins/keys.rb', line 36 def using_object_id? object_id_key?(:_id) end |