Module: Mongo::Model::Assignment

Defined in:
lib/mongo/model/assignment.rb

Defined Under Namespace

Modules: ClassMethods Classes: Dsl

Instance Method Summary collapse

Instance Method Details

#initialize(attributes = nil) ⇒ Object



40
41
42
43
# File 'lib/mongo/model/assignment.rb', line 40

def initialize attributes = nil
  super()
  set attributes if attributes
end

#set(attributes, options = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/mongo/model/assignment.rb', line 16

def set attributes, options = {}
  if rules = self.class._assign
    force = options[:force]
    attributes.each do |n, v|
      n = n.to_sym
      type, mass_assignment = rules[n]
      if mass_assignment or force
        v = type.cast(v) if type
        send "#{n}=", v
      else
        # Do nothing, just silently skip it.
        # raise "mass assignment for :#{n} attribute not allowed!"
      end
    end
  else
    attributes.each{|n, v| send "#{n}=", v}
  end
  self
end

#set!(attributes, options = {}) ⇒ Object



36
37
38
# File 'lib/mongo/model/assignment.rb', line 36

def set! attributes, options = {}
  set attributes, options.merge(force: true)
end