Sha256: 159abdaeb0ef3800fc239f6f8cbde047a19bc10012cbbdf8f0aef3bf9fd6a4ba

Contents?: true

Size: 1.94 KB

Versions: 81

Compression:

Stored size: 1.94 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}="
        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

81 entries across 81 versions & 2 rubygems

Version Path
neo4j-8.0.17 lib/neo4j/shared/mass_assignment.rb
neo4j-8.0.16 lib/neo4j/shared/mass_assignment.rb
neo4j-8.0.15 lib/neo4j/shared/mass_assignment.rb
neo4j-8.0.13 lib/neo4j/shared/mass_assignment.rb
neo4j-8.0.12 lib/neo4j/shared/mass_assignment.rb
neo4j-8.0.11 lib/neo4j/shared/mass_assignment.rb
neo4j-8.0.10 lib/neo4j/shared/mass_assignment.rb
neo4j-8.0.9 lib/neo4j/shared/mass_assignment.rb
neo4j-8.0.8 lib/neo4j/shared/mass_assignment.rb
neo4j-8.0.7 lib/neo4j/shared/mass_assignment.rb
neo4j-8.0.6 lib/neo4j/shared/mass_assignment.rb
neo4j-8.0.5 lib/neo4j/shared/mass_assignment.rb
neo4j-8.0.4 lib/neo4j/shared/mass_assignment.rb
neo4j-8.0.3 lib/neo4j/shared/mass_assignment.rb
neo4j-8.0.2 lib/neo4j/shared/mass_assignment.rb
neo4j-8.0.1 lib/neo4j/shared/mass_assignment.rb
neo4j-8.0.0 lib/neo4j/shared/mass_assignment.rb
neo4j-8.0.0.rc.4 lib/neo4j/shared/mass_assignment.rb
neo4j-8.0.0.rc.3 lib/neo4j/shared/mass_assignment.rb
neo4j-8.0.0.rc.2 lib/neo4j/shared/mass_assignment.rb