module ActiveGraph::Shared::MassAssignment

MassAssignment allows you to bulk set and update attributes

Including MassAssignment into your model gives it a set of mass assignment methods, similar to those found in ActiveRecord.

@example Usage

class Person
  include ActiveGraph::Shared::MassAssignment
end

Originally part of ActiveAttr, github.com/cgriego/active_attr

Public Class Methods

new(attributes = nil) click to toggle source

Initialize a model with a set of attributes

@example Initializing with a hash

person = Person.new(:first_name => "Chris", :last_name => "Griego")
person.first_name #=> "Chris"
person.last_name #=> "Griego"

@param (see assign_attributes)

Calls superclass method
   # File lib/active_graph/shared/mass_assignment.rb
58 def initialize(attributes = nil)
59   assign_attributes(attributes)
60   super()
61 end

Public Instance Methods

add_undeclared_property(_, _) click to toggle source
   # File lib/active_graph/shared/mass_assignment.rb
36 def add_undeclared_property(_, _); end
assign_attributes(new_attributes = nil) click to toggle source

Mass update a model's attributes

@example Assigning a hash

person.assign_attributes(:first_name => "Chris", :last_name => "Griego")
person.first_name #=> "Chris"
person.last_name #=> "Griego"

@param [Hash{#to_s => Object}, each] new_attributes Attributes used to

populate the model
   # File lib/active_graph/shared/mass_assignment.rb
24 def assign_attributes(new_attributes = nil)
25   return unless new_attributes.present?
26   new_attributes.each do |name, value|
27     writer = :"#{name}="
28     if respond_to?(writer)
29       send(writer, value)
30     else
31       add_undeclared_property(name, value)
32     end
33   end
34 end
attributes=(new_attributes) click to toggle source

Mass update a model's attributes

@example Assigning a hash

person.attributes = { :first_name => "Chris", :last_name => "Griego" }
person.first_name #=> "Chris"
person.last_name #=> "Griego"

@param (see assign_attributes)

   # File lib/active_graph/shared/mass_assignment.rb
46 def attributes=(new_attributes)
47   assign_attributes(new_attributes)
48 end