Module: MongoMapper::Plugins::Keys::ClassMethods

Defined in:
lib/mongo_mapper/plugins/keys.rb

Instance Method Summary collapse

Instance Method Details

#can_default_id?Boolean

API Private

Returns:



48
49
50
# File 'lib/mongo_mapper/plugins/keys.rb', line 48

def can_default_id?
  keys['_id'].can_default_id?
end

#from_mongo(value) ⇒ Object



57
58
59
60
# File 'lib/mongo_mapper/plugins/keys.rb', line 57

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

Returns:



31
32
33
# File 'lib/mongo_mapper/plugins/keys.rb', line 31

def key?(key)
  keys.keys.include?(key.to_s)
end

#keysObject



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



63
64
65
66
67
68
69
70
71
# File 'lib/mongo_mapper/plugins/keys.rb', line 63

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

Returns:



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_keysObject



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



52
53
54
55
# File 'lib/mongo_mapper/plugins/keys.rb', line 52

def to_mongo(instance)
  return nil if instance.nil?
  instance.to_mongo
end

#using_object_id?Boolean

Returns:



35
36
37
# File 'lib/mongo_mapper/plugins/keys.rb', line 35

def using_object_id?
  object_id_key?(:_id)
end