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)
  get_attribute(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)
  set_attribute(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
# File 'lib/virtus/instance_methods.rb', line 84

def attributes
  get_attributes(&:public_reader?)
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)


111
112
113
# File 'lib/virtus/instance_methods.rb', line 111

def attributes=(attributes)
  set_attributes(attributes)
end

#freezeself

Freeze object

Examples:


class User
  include Virtus

  attribute :name, String
  attribute :age,  Integer
end

user = User.new(:name => 'John', :age => 28)
user.frozen? # => false
user.freeze
user.frozen? # => true

Returns:

  • (self)


156
157
158
159
# File 'lib/virtus/instance_methods.rb', line 156

def freeze
  set_defaults
  super
end

#initialize(attributes = nil) ⇒ 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) (defaults to: nil)

    the attributes hash to be set

Returns:

  • (undefined)


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

def initialize(attributes = nil)
  self.attributes = attributes if attributes
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)


131
132
133
# File 'lib/virtus/instance_methods.rb', line 131

def to_hash
  attributes
end