Class: Virtus::Attribute Abstract
- Inherits:
-
Object
- Object
- Virtus::Attribute
- Extended by:
- DescendantsTracker, Options, TypeLookup
- Defined in:
- lib/virtus/attribute.rb,
lib/virtus/attribute/date.rb,
lib/virtus/attribute/hash.rb,
lib/virtus/attribute/time.rb,
lib/virtus/attribute/array.rb,
lib/virtus/attribute/class.rb,
lib/virtus/attribute/float.rb,
lib/virtus/attribute/object.rb,
lib/virtus/attribute/string.rb,
lib/virtus/attribute/boolean.rb,
lib/virtus/attribute/decimal.rb,
lib/virtus/attribute/integer.rb,
lib/virtus/attribute/numeric.rb,
lib/virtus/attribute/date_time.rb,
lib/virtus/attribute/default_value.rb
Overview
Abstract class implementing base API for attribute types
Direct Known Subclasses
Defined Under Namespace
Classes: Array, Boolean, Class, Date, DateTime, Decimal, DefaultValue, Float, Hash, Integer, Numeric, Object, String, Time
Constant Summary collapse
- DEFAULT_ACCESSOR =
:public
- OPTIONS =
[ :primitive, :accessor, :reader, :writer, :coercion_method, :default ].freeze
Constants included from TypeLookup
Instance Attribute Summary collapse
-
#coercion_method ⇒ Symbol
readonly
private
Returns method name that should be used for coerceing.
-
#default ⇒ Object
readonly
private
Returns default value.
-
#instance_variable_name ⇒ String
readonly
private
Returns instance variable name of the attribute.
-
#name ⇒ Symbol
readonly
Returns name of the attribute.
-
#options ⇒ Hash
readonly
private
Returns options hash for the attribute.
-
#reader_visibility ⇒ Symbol
readonly
private
Returns reader visibility.
-
#writer_visibility ⇒ Symbol
readonly
private
Returns write visibility.
Instance Method Summary collapse
-
#coerce(value) ⇒ Object
Converts the given value to the primitive type.
-
#define_reader_method(mod) ⇒ self
private
Creates an attribute reader method.
-
#define_writer_method(mod) ⇒ self
private
Creates an attribute writer method.
-
#get(instance) ⇒ Object
Returns value of an attribute for the given instance.
-
#get!(instance) ⇒ Object
Returns the instance variable of the attribute.
-
#initialize(name, options = {}) ⇒ undefined
constructor
private
Initializes an attribute instance.
-
#inspect ⇒ String
Returns a concise string representation of the attribute instance.
-
#set(instance, value) ⇒ self
Sets the value on the instance.
-
#set!(instance, value) ⇒ self
Sets instance variable of the attribute.
Methods included from DescendantsTracker
Methods included from TypeLookup
Methods included from Options
accept_options, accepted_options
Constructor Details
#initialize(name, options = {}) ⇒ 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.
Initializes an attribute instance
81 82 83 84 85 86 87 88 89 90 |
# File 'lib/virtus/attribute.rb', line 81 def initialize(name, = {}) @name = name @options = self.class..merge(.to_hash).freeze @instance_variable_name = "@#{@name}".freeze @coercion_method = @options.fetch(:coercion_method) @default = DefaultValue.new(self, @options[:default]) set_visibility end |
Instance Attribute Details
#coercion_method ⇒ Symbol (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.
Returns method name that should be used for coerceing
54 55 56 |
# File 'lib/virtus/attribute.rb', line 54 def coercion_method @coercion_method end |
#default ⇒ 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.
Returns default value
61 62 63 |
# File 'lib/virtus/attribute.rb', line 61 def default @default end |
#instance_variable_name ⇒ String (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.
Returns instance variable name of the attribute
33 34 35 |
# File 'lib/virtus/attribute.rb', line 33 def instance_variable_name @instance_variable_name end |
#name ⇒ Symbol (readonly)
Returns name of the attribute
19 20 21 |
# File 'lib/virtus/attribute.rb', line 19 def name @name end |
#options ⇒ Hash (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.
Returns options hash for the attribute
26 27 28 |
# File 'lib/virtus/attribute.rb', line 26 def @options end |
#reader_visibility ⇒ Symbol (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.
Returns reader visibility
40 41 42 |
# File 'lib/virtus/attribute.rb', line 40 def reader_visibility @reader_visibility end |
#writer_visibility ⇒ Symbol (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.
Returns write visibility
47 48 49 |
# File 'lib/virtus/attribute.rb', line 47 def writer_visibility @writer_visibility end |
Instance Method Details
#coerce(value) ⇒ Object
Converts the given value to the primitive type
174 175 176 |
# File 'lib/virtus/attribute.rb', line 174 def coerce(value) Coercion[value.class].send(coercion_method, value) end |
#define_reader_method(mod) ⇒ self
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.
Creates an attribute reader method
185 186 187 188 189 190 191 192 193 |
# File 'lib/virtus/attribute.rb', line 185 def define_reader_method(mod) reader_method_name = name attribute = self mod.send(:define_method, reader_method_name) { attribute.get(self) } mod.send(reader_visibility, reader_method_name) self end |
#define_writer_method(mod) ⇒ self
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.
Creates an attribute writer method
202 203 204 205 206 207 208 209 210 |
# File 'lib/virtus/attribute.rb', line 202 def define_writer_method(mod) writer_method_name = "#{name}=" attribute = self mod.send(:define_method, writer_method_name) { |value| attribute.set(self, value) } mod.send(writer_visibility, writer_method_name) self end |
#get(instance) ⇒ Object
Returns value of an attribute for the given instance
Sets the default value if an ivar is not set and default value is configured
117 118 119 120 121 122 123 |
# File 'lib/virtus/attribute.rb', line 117 def get(instance) if instance.instance_variable_defined?(instance_variable_name) get!(instance) else set!(instance, default.evaluate(instance)) end end |
#get!(instance) ⇒ Object
Returns the instance variable of the attribute
134 135 136 |
# File 'lib/virtus/attribute.rb', line 134 def get!(instance) instance.instance_variable_get(instance_variable_name) end |
#inspect ⇒ String
Returns a concise string representation of the attribute instance
101 102 103 |
# File 'lib/virtus/attribute.rb', line 101 def inspect "#<#{self.class.name} @name=#{name.inspect}>" end |
#set(instance, value) ⇒ self
Sets the value on the instance
146 147 148 |
# File 'lib/virtus/attribute.rb', line 146 def set(instance, value) set!(instance, coerce(value)) end |
#set!(instance, value) ⇒ self
Sets instance variable of the attribute
158 159 160 |
# File 'lib/virtus/attribute.rb', line 158 def set!(instance, value) instance.instance_variable_set(instance_variable_name, value) end |