Module: Virtus::InstanceMethods

Defined in:
lib/virtus/instance_methods.rb

Overview

Instance methods that are added when you include Virtus

Instance Method Summary collapse

Instance Method Details

#[](name) ⇒ Object

Returns a value of the attribute with the given name

Examples:

class User
  include Virtus

  attribute :name, String
end

user = User.new(:name => 'John')
user[:name]  # => "John"

Parameters:

  • name (Symbol)

    a name of an attribute

Returns:

  • (Object)

    a value of an attribute



37
38
39
# File 'lib/virtus/instance_methods.rb', line 37

def [](name)
  __send__(name)
end

#[]=(name, value) ⇒ Object

Sets a value of the attribute with the given name

Examples:

class User
  include Virtus

  attribute :name, String
end

user = User.new
user[:name] = "John"  # => "John"
user.name             # => "John"

Parameters:

  • name (Symbol)

    a name of an attribute

  • value (Object)

    a value to be set

Returns:

  • (Object)

    the value set on an object



64
65
66
# File 'lib/virtus/instance_methods.rb', line 64

def []=(name, value)
  __send__("#{name}=", value)
end

#attributesHash

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)


84
85
86
87
88
89
# File 'lib/virtus/instance_methods.rb', line 84

def attributes
  self.class.attributes.each_with_object({}) do |attribute, attributes|
    name = attribute.name
    attributes[name] = self[name] if attribute.public_reader?
  end
end

#attributes=(attribute_values) ⇒ Hash

Mass-assign attribute values

Keys in the attribute_values param can be symbols or strings. Only non-private referenced Attribute writer methods will be called. Non-attribute setter methods on the receiver will not be called.

Examples:

class User
  include Virtus

  attribute :name, String
  attribute :age,  Integer
end

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

Parameters:

  • attribute_values (#to_hash)

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

Returns:

  • (Hash)


114
115
116
117
118
119
120
# File 'lib/virtus/instance_methods.rb', line 114

def attributes=(attribute_values)
  attributes = self.class.attributes
  set_attributes(attribute_values.select { |name,|
    attribute = attributes[name]
    attribute && attribute.public_writer?
  })
end

#initialize(attribute_values = {}) ⇒ undefined

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Set attributes during initialization of an object

Parameters:

  • attributes (#to_hash)

    the attributes hash to be set

Returns:

  • (undefined)


14
15
16
# File 'lib/virtus/instance_methods.rb', line 14

def initialize(attribute_values = {})
  self.attributes = attribute_values
end

#to_hashHash

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)


138
139
140
# File 'lib/virtus/instance_methods.rb', line 138

def to_hash
  attributes
end