Sha256: 7f7d0e594306389ac3c8e6baa689d863014be8f6b5278b9b1de90e75e7217c96

Contents?: true

Size: 1.95 KB

Versions: 11

Compression:

Stored size: 1.95 KB

Contents

module ActiveGraph::Shared
  # 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, https://github.com/cgriego/active_attr
  module MassAssignment
    extend ActiveSupport::Concern
    # 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] attributes Attributes used to
    #   populate the model
    # @param [Hash, #[]] options Options that affect mass assignment
    def assign_attributes(new_attributes = nil)
      return unless new_attributes.present?
      new_attributes.each do |name, value|
        writer = :"#{name}="
        if respond_to?(writer)
          send(writer, value)
        else
          add_undeclared_property(name, value)
        end
      end
    end

    def add_undeclared_property(_, _); end

    # 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)
    def attributes=(new_attributes)
      assign_attributes(new_attributes)
    end

    # 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)
    def initialize(attributes = nil)
      assign_attributes(attributes)
      super()
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
activegraph-10.0.0.pre.beta.11 lib/active_graph/shared/mass_assignment.rb
activegraph-10.0.0.pre.beta.10 lib/active_graph/shared/mass_assignment.rb
activegraph-10.0.0.pre.beta.9 lib/active_graph/shared/mass_assignment.rb
activegraph-10.0.0.pre.beta.8 lib/active_graph/shared/mass_assignment.rb
activegraph-10.0.0.pre.beta.7 lib/active_graph/shared/mass_assignment.rb
activegraph-10.0.0.pre.beta.6 lib/active_graph/shared/mass_assignment.rb
activegraph-10.0.0.pre.beta.5 lib/active_graph/shared/mass_assignment.rb
activegraph-10.0.0.pre.beta.4 lib/active_graph/shared/mass_assignment.rb
activegraph-10.0.0.pre.beta.3 lib/active_graph/shared/mass_assignment.rb
activegraph-10.0.0.pre.beta.2 lib/active_graph/shared/mass_assignment.rb
activegraph-10.0.0.pre.beta.1 lib/active_graph/shared/mass_assignment.rb