Module: MongoModel::Attributes

Extended by:
ActiveSupport::Concern
Included in:
EmbeddedDocument
Defined in:
lib/mongomodel.rb,
lib/mongomodel/attributes/mongo.rb,
lib/mongomodel/attributes/store.rb,
lib/mongomodel/concerns/attributes.rb,
lib/mongomodel/attributes/typecasting.rb

Defined Under Namespace

Modules: ClassMethods, Mongo, Typecasting Classes: Store

Instance Method Summary collapse

Instance Method Details

#assign_attributes(attrs, options = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/mongomodel/concerns/attributes.rb', line 16

def assign_attributes(attrs, options={})
  return unless attrs
  
  attrs.each do |attr, value|
    if respond_to?("#{attr}=")
      send("#{attr}=", value)
    else
      write_attribute(attr, value)
    end
  end
end

#attributesObject



12
13
14
# File 'lib/mongomodel/concerns/attributes.rb', line 12

def attributes
  @attributes ||= Attributes::Store.new(self)
end

#attributes=(attrs) ⇒ Object



28
29
30
# File 'lib/mongomodel/concerns/attributes.rb', line 28

def attributes=(attrs)
  assign_attributes(attrs)
end

#dupObject

Returns duplicated record with unfreezed attributes.



41
42
43
44
45
# File 'lib/mongomodel/concerns/attributes.rb', line 41

def dup
  obj = super
  obj.instance_variable_set('@attributes', instance_variable_get('@attributes').dup)
  obj
end

#embedded_documentsObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/mongomodel/concerns/attributes.rb', line 51

def embedded_documents
  docs = []
  
  docs.concat attributes.values.select { |attr| attr.is_a?(EmbeddedDocument) }
  
  attributes.values.select { |attr| attr.is_a?(Collection) }.each do |collection|
    docs.concat collection.embedded_documents
  end
  
  attributes.values.select { |attr| attr.is_a?(Map) && attr.to <= EmbeddedDocument }.each do |map|
    docs.concat map.values
  end
  
  docs
end

#freezeObject



32
33
34
# File 'lib/mongomodel/concerns/attributes.rb', line 32

def freeze
  attributes.freeze; self
end

#frozen?Boolean

Returns:



36
37
38
# File 'lib/mongomodel/concerns/attributes.rb', line 36

def frozen?
  attributes.frozen?
end

#initialize(attrs = {}, options = {}) {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



7
8
9
10
# File 'lib/mongomodel/concerns/attributes.rb', line 7

def initialize(attrs={}, options={})
  assign_attributes(attrs || {}, options)
  yield self if block_given?
end

#to_mongoObject



47
48
49
# File 'lib/mongomodel/concerns/attributes.rb', line 47

def to_mongo
  attributes.to_mongo
end