Sha256: e2904ab76d417ccaa2a1d0606919e2a54b89f514f0a066278a98d52356622158

Contents?: true

Size: 1.81 KB

Versions: 46

Compression:

Stored size: 1.81 KB

Contents

module Neo4j::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 Neo4j::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}="
        send(writer, value) if respond_to?(writer)
      end
    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

46 entries across 46 versions & 2 rubygems

Version Path
neo4j-8.0.0.alpha.12 lib/neo4j/shared/mass_assignment.rb
neo4j-7.2.3 lib/neo4j/shared/mass_assignment.rb
neo4j-8.0.0.alpha.11 lib/neo4j/shared/mass_assignment.rb
neo4j-7.2.2 lib/neo4j/shared/mass_assignment.rb
neo4j-7.1.4 lib/neo4j/shared/mass_assignment.rb
neo4j-7.0.16 lib/neo4j/shared/mass_assignment.rb
neo4j-7.2.1 lib/neo4j/shared/mass_assignment.rb
neo4j-8.0.0.alpha.10 lib/neo4j/shared/mass_assignment.rb
neo4j-8.0.0.alpha.9 lib/neo4j/shared/mass_assignment.rb
neo4j-8.0.0.alpha.8 lib/neo4j/shared/mass_assignment.rb
neo4j-8.0.0.alpha.7 lib/neo4j/shared/mass_assignment.rb
neo4j_legacy-7.2.0.2 lib/neo4j/shared/mass_assignment.rb
neo4j_legacy-7.2.0.1 lib/neo4j/shared/mass_assignment.rb
neo4j-8.0.0.alpha.6 lib/neo4j/shared/mass_assignment.rb
neo4j-8.0.0.alpha.5 lib/neo4j/shared/mass_assignment.rb
neo4j-8.0.0.alpha.4 lib/neo4j/shared/mass_assignment.rb
neo4j-7.2.0 lib/neo4j/shared/mass_assignment.rb
neo4j-7.1.3 lib/neo4j/shared/mass_assignment.rb
neo4j-7.0.15 lib/neo4j/shared/mass_assignment.rb
neo4j-8.0.0.alpha.2 lib/neo4j/shared/mass_assignment.rb