Module: Virtus::InstanceMethods
- Defined in:
- lib/virtus/instance_methods.rb
Overview
Instance methods that are added when you include Virtus
Instance Method Summary collapse
-
#[](name) ⇒ Object
Returns a value of the attribute with the given name.
-
#[]=(name, value) ⇒ Object
Sets a value of the attribute with the given name.
-
#attributes ⇒ Hash
Returns a hash of all publicly accessible attributes.
-
#attributes=(attribute_values) ⇒ Hash
Mass-assign attribute values.
-
#initialize(attribute_values = {}) ⇒ undefined
private
Set attributes during initialization of an object.
-
#to_hash ⇒ Hash
Returns a hash of all publicly accessible attributes.
Instance Method Details
#[](name) ⇒ Object
Returns a value of the attribute with the given name
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
64 65 66 |
# File 'lib/virtus/instance_methods.rb', line 64 def []=(name, value) __send__("#{name}=", value) end |
#attributes ⇒ Hash
Returns a hash of all publicly accessible attributes
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.
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
14 15 16 |
# File 'lib/virtus/instance_methods.rb', line 14 def initialize(attribute_values = {}) self.attributes = attribute_values end |
#to_hash ⇒ Hash
Returns a hash of all publicly accessible attributes
138 139 140 |
# File 'lib/virtus/instance_methods.rb', line 138 def to_hash attributes end |