Module: Virtus::Attribute::Accessor
- Defined in:
- lib/virtus/attribute/accessor.rb
Overview
Accessor extension provides methods to read and write attributes
Instance Attribute Summary collapse
-
#instance_variable_name ⇒ Object
readonly
private
Return instance_variable_name used by this accessor.
-
#name ⇒ Symbol
readonly
Return name of this accessor attribute.
Class Method Summary collapse
- .extended(descendant) ⇒ Object private
Instance Method Summary collapse
-
#defined?(instance) ⇒ Boolean
Return if attribute value is defined.
-
#get(instance) ⇒ Object
Return value of the attribute.
-
#public_reader? ⇒ Boolean
private
Returns a Boolean indicating whether the reader method is public.
-
#public_writer? ⇒ Boolean
private
Returns a Boolean indicating whether the writer method is public.
-
#set(instance, value) ⇒ Object
Set value of the attribute.
-
#set_default_value(instance) ⇒ Object
Set default value.
Instance Attribute Details
#instance_variable_name ⇒ Object (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 |
#name ⇒ Symbol (readonly)
Return name of this accessor attribute
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..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
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
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
87 88 89 |
# File 'lib/virtus/attribute/accessor.rb', line 87 def public_reader? [: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
96 97 98 |
# File 'lib/virtus/attribute/accessor.rb', line 96 def public_writer? [:writer] == :public end |
#set(instance, value) ⇒ Object
Set value of the attribute
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
78 79 80 |
# File 'lib/virtus/attribute/accessor.rb', line 78 def set_default_value(instance) set(instance, default_value.call(instance, self)) end |