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

#defined?(instance) ⇒ Boolean

Return if attribute value is defined

Parameters:

  • instance (Object)

Returns:



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

def defined?(instance)
  instance.instance_variable_defined?(instance_variable_name)
end

#get(instance) ⇒ Object

Return value of the attribute

Parameters:

  • instance (Object)

Returns:

  • (Object)


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

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:



87
88
89
# File 'lib/virtus/attribute/accessor.rb', line 87

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:



96
97
98
# File 'lib/virtus/attribute/accessor.rb', line 96

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



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

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



78
79
80
# File 'lib/virtus/attribute/accessor.rb', line 78

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