Module: Virtus::Attribute::Accessor

Defined in:
lib/virtus/attribute/accessor.rb

Overview

Accessor extension provides methods to read and write attributes

Examples:


attribute = Virtus::Attribute.build(String, :name => :email)
model     = Class.new { attr_reader :email }
object    = model.new

attribute.set(object, '[email protected]')
attribute.get(object) # => '[email protected]'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#instance_variable_nameObject (readonly)

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.

Return instance_variable_name used by this accessor



27
28
29
# File 'lib/virtus/attribute/accessor.rb', line 27

def instance_variable_name
  @instance_variable_name
end

#nameSymbol (readonly)

Return name of this accessor attribute

Returns:

  • (Symbol)


22
23
24
# File 'lib/virtus/attribute/accessor.rb', line 22

def name
  @name
end

Class Method Details

.extended(descendant) ⇒ 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.



30
31
32
33
34
35
# File 'lib/virtus/attribute/accessor.rb', line 30

def self.extended(descendant)
  super
  name = descendant.options.fetch(:name).to_sym
  descendant.instance_variable_set('@name', name)
  descendant.instance_variable_set('@instance_variable_name', "@#{name}")
end

Instance Method Details

#get(instance) ⇒ Object

Return value of the attribute

Parameters:

  • instance (Object)

Returns:

  • (Object)


44
45
46
# File 'lib/virtus/attribute/accessor.rb', line 44

def get(instance)
  instance.instance_variable_get(instance_variable_name)
end

#public_reader?Boolean

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.

Returns a Boolean indicating whether the reader method is public

Returns:



76
77
78
# File 'lib/virtus/attribute/accessor.rb', line 76

def public_reader?
  options[:reader] == :public
end

#public_writer?Boolean

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.

Returns a Boolean indicating whether the writer method is public

Returns:



85
86
87
# File 'lib/virtus/attribute/accessor.rb', line 85

def public_writer?
  options[:writer] == :public
end

#set(instance, value) ⇒ Object

Set value of the attribute

Parameters:

  • instance (Object)
  • value (Object)

Returns:

  • (Object)

    value that was set



56
57
58
# File 'lib/virtus/attribute/accessor.rb', line 56

def set(instance, value)
  instance.instance_variable_set(instance_variable_name, value)
end

#set_default_value(instance) ⇒ Object

Set default value

Parameters:

  • instance (Object)

Returns:

  • (Object)

    value that was set



67
68
69
# File 'lib/virtus/attribute/accessor.rb', line 67

def set_default_value(instance)
  set(instance, default_value.call(instance, self))
end