Module: MassAssignable

Defined in:
lib/mass_assignable.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



2
3
4
# File 'lib/mass_assignable.rb', line 2

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#attributes=(attribute_hash) ⇒ Object

Public: Assigns a Hash of attributes to their corresponding instance methods. If attributes are specified with attr_assignable, then only those attributes are allowed to be assigned.

attribute_hash - A Hash of attribute values with symbol keys.

Returns nothing.



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/mass_assignable.rb', line 52

def attributes=(attribute_hash)
  paranoid = self.class.raise_on_invalid_mass_assignment
  allowed = self.class.mass_assignable_attributes.map { |a| a.to_s }
  
  attribute_hash.each do |key, value|
    if allowed.include?(key.to_s)
      send(:"#{key}=", value)
    else
      raise(RuntimeError, "Mass assignment error") if paranoid
    end
  end
end