Module: Virtus::InstanceMethods::MassAssignment

Defined in:
lib/virtus/instance_methods.rb

Overview

Constructor

Instance Method Summary collapse

Instance Method Details

#attributesHash Also known as: to_hash, to_h

Returns a hash of all publicly accessible attributes

Examples:

class User
  include Virtus

  attribute :name, String
  attribute :age,  Integer
end

user = User.new(:name => 'John', :age => 28)
user.attributes  # => { :name => 'John', :age => 28 }

Returns:

  • (Hash)


41
42
43
# File 'lib/virtus/instance_methods.rb', line 41

def attributes
  attribute_set.get(self)
end

#attributes=(attributes) ⇒ Hash

Mass-assign attribute values

Keys in the attributes param can be symbols or strings. All referenced Attribute writer methods will be called. Non-attribute setter methods on the receiver will be called.

Examples:

class User
  include Virtus

  attribute :name, String
  attribute :age,  Integer
end

user = User.new
user.attributes = { :name => 'John', 'age' => 28 }

Parameters:

  • attributes (#to_hash)

    a hash of attribute names and values to set on the receiver

Returns:

  • (Hash)


70
71
72
# File 'lib/virtus/instance_methods.rb', line 70

def attributes=(attributes)
  attribute_set.set(self, attributes)
end