Module: Virtus::InstanceMethods

Defined in:
lib/virtus/instance_methods.rb

Instance Method Summary collapse

Instance Method Details

#attribute_get(name) ⇒ Object

Returns a value of the attribute with the given name

Parameters:

  • name (Symbol)

    a name of an attribute

Returns:

  • (Object)

    a value of an attribute



25
26
27
# File 'lib/virtus/instance_methods.rb', line 25

def attribute_get(name)
  __send__(name)
end

#attribute_set(name, value) ⇒ Object

Sets a value of the attribute with the given name

Parameters:

  • name (Symbol)

    a name of an attribute

  • value (Object)

    a value to be set

Returns:

  • (Object)

    the value set on an object



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

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

#attributesHash

Returns a hash of all publicly accessible attributes

Returns:

  • (Hash)

    the attributes



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/virtus/instance_methods.rb', line 68

def attributes
  attributes = {}

  self.class.attributes.each do |name, attribute|
    if self.class.public_method_defined?(name)
      attributes[name] = __send__(attribute.name)
    end
  end

  attributes
end

#attributes=(attributes) ⇒ Hash

Mass-assign of attribute values

Parameters:

  • attributes (Hash)

    a hash of attribute values to be set on an object

Returns:

  • (Hash)

    the attributes



54
55
56
57
58
59
60
# File 'lib/virtus/instance_methods.rb', line 54

def attributes=(attributes)
  attributes.each do |name, value|
    if self.class.public_method_defined?(writer_name = "#{name}=")
      __send__(writer_name, value)
    end
  end
end

#initialize(attributes = {}) ⇒ Object

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.

Chains Class.new to be able to set attributes during initialization of an object.

Parameters:

  • attributes (Hash) (defaults to: {})

    the attributes hash to be set

Returns:

  • (Object)


12
13
14
# File 'lib/virtus/instance_methods.rb', line 12

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