module Virtus::InstanceMethods
Instance methods that are added when you include Virtus
Public Instance Methods
[](name)
click to toggle source
Returns a value of the attribute with the given name
@example
class User include Virtus attribute :name, String end user = User.new(:name => 'John') user[:name] # => "John"
@param [Symbol] name
a name of an attribute
@return [Object]
a value of an attribute
@api public
# File lib/virtus/instance_methods.rb, line 95 def [](name) public_send(name) end
[]=(name, value)
click to toggle source
Sets a value of the attribute with the given name
@example
class User include Virtus attribute :name, String end user = User.new user[:name] = "John" # => "John" user.name # => "John"
@param [Symbol] name
a name of an attribute
@param [Object] value
a value to be set
@return [Object]
the value set on an object
@api public
# File lib/virtus/instance_methods.rb, line 122 def []=(name, value) public_send("#{name}=", value) end
freeze()
click to toggle source
Freeze object
@return [self]
@api public
@example
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
@api public
Calls superclass method
# File lib/virtus/instance_methods.rb, line 147 def freeze set_default_attributes! super end
reset_attribute(attribute_name)
click to toggle source
Reset an attribute to its default
@return [self]
@api public
@example
class User include Virtus attribute :age, Integer, default: 21 end user = User.new(:name => 'John', :age => 28) user.age = 30 user.age # => 30 user.reset_attribute(:age) user.age # => 21
@api public
# File lib/virtus/instance_methods.rb, line 173 def reset_attribute(attribute_name) attribute = attribute_set[attribute_name] attribute.set_default_value(self) if attribute self end
set_default_attributes()
click to toggle source
Set default attributes
@return [self]
@api private
# File lib/virtus/instance_methods.rb, line 184 def set_default_attributes attribute_set.set_defaults(self) self end
set_default_attributes!()
click to toggle source
Set default attributes even lazy ones
@return [self]
@api public
# File lib/virtus/instance_methods.rb, line 194 def set_default_attributes! attribute_set.set_defaults(self, proc { |object, attribute| attribute.defined?(object) }) self end
Private Instance Methods
allowed_methods()
click to toggle source
The list of allowed public methods
@return [Array<String>]
@api private
# File lib/virtus/instance_methods.rb, line 206 def allowed_methods public_methods.map(&:to_s) end
assert_valid_name(name)
click to toggle source
@api private
# File lib/virtus/instance_methods.rb, line 211 def assert_valid_name(name) if respond_to?(:attributes) && name.to_sym == :attributes || name.to_sym == :attribute_set raise ArgumentError, "#{name.inspect} is not allowed as an attribute name" end end