Class: Virtus::Attribute Abstract
- Inherits:
-
Object
- Object
- Virtus::Attribute
- Extended by:
- DescendantsTracker, Options, TypeLookup
- Defined in:
- lib/virtus/attribute.rb,
lib/virtus/attribute/set.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/symbol.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/collection.rb,
lib/virtus/attribute/default_value.rb,
lib/virtus/attribute/embedded_value.rb,
lib/virtus/attribute/default_value/from_symbol.rb,
lib/virtus/attribute/default_value/from_callable.rb,
lib/virtus/attribute/default_value/from_clonable.rb
Overview
Abstract class implementing base API for attribute types
Direct Known Subclasses
Defined Under Namespace
Classes: Array, Boolean, Class, Collection, Date, DateTime, Decimal, DefaultValue, EmbeddedValue, Float, Hash, Integer, Numeric, Object, Set, String, Symbol, Time
Constant Summary
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.
-
#name ⇒ Symbol
readonly
Returns name of the attribute.
-
#options ⇒ Hash
readonly
private
Returns options hash for the attribute.
Class Method Summary collapse
-
.build(name, type, options = {}) ⇒ Attribute
private
Builds an attribute instance.
-
.determine_type(class_or_name) ⇒ Class
Determine attribute type based on class or name.
-
.merge_options(type, options) ⇒ Hash
private
A hook for Attributes to update options based on the type from the caller.
Instance Method Summary collapse
-
#coerce(value) ⇒ Object
Converts the given value to the primitive type.
-
#define_accessor_methods(mod) ⇒ self
private
Define reader and writer methods for an Attribute.
-
#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.
-
#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) ⇒ self
Sets the value on the instance.
-
#set!(instance, value) ⇒ self
Sets instance variable of the attribute.
-
#value_coerced?(value) ⇒ Boolean
private
Is the given value coerced into the target type for this attribute?.
Methods included from DescendantsTracker
Methods included from TypeLookup
determine_type, extended, primitive
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
124 125 126 127 128 129 130 131 132 |
# File 'lib/virtus/attribute.rb', line 124 def initialize(name, = {}) @name = name.to_sym @options = self.class..merge().freeze @instance_variable_name = "@#{@name}".to_sym @primitive = @options.fetch(:primitive) @coercion_method = @options.fetch(:coercion_method) @default = DefaultValue.build(self, @options[:default]) initialize_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
38 39 40 |
# File 'lib/virtus/attribute.rb', line 38 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
45 46 47 |
# File 'lib/virtus/attribute.rb', line 45 def default @default end |
#name ⇒ Symbol (readonly)
Returns name of the attribute
24 25 26 |
# File 'lib/virtus/attribute.rb', line 24 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
31 32 33 |
# File 'lib/virtus/attribute.rb', line 31 def @options end |
Class Method Details
.build(name, type, options = {}) ⇒ Attribute
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.
Builds an attribute instance
61 62 63 64 65 66 |
# File 'lib/virtus/attribute.rb', line 61 def self.build(name, type, = {}) attribute_class = determine_type(type) or raise ArgumentError, "#{type.inspect} does not map to an attribute type" = attribute_class.(type, ) attribute_class.new(name, ) end |
.determine_type(class_or_name) ⇒ Class
Determine attribute type based on class or name
Returns Attribute::EmbeddedValue if a virtus class is passed
81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/virtus/attribute.rb', line 81 def self.determine_type(class_or_name) case class_or_name when ::Class if class_or_name <= Virtus Attribute::EmbeddedValue else super end when ::Array, ::Set super(class_or_name.class) else super end end |
.merge_options(type, options) ⇒ Hash
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.
add type arg to Attribute#initialize signature and handle there?
A hook for Attributes to update options based on the type from the caller
109 110 111 |
# File 'lib/virtus/attribute.rb', line 109 def self.(type, ) end |
Instance Method Details
#coerce(value) ⇒ Object
Converts the given value to the primitive type
219 220 221 |
# File 'lib/virtus/attribute.rb', line 219 def coerce(value) Coercion[value.class].public_send(coercion_method, value) end |
#define_accessor_methods(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.
Define reader and writer methods for an Attribute
250 251 252 253 254 |
# File 'lib/virtus/attribute.rb', line 250 def define_accessor_methods(mod) define_reader_method(mod) define_writer_method(mod) self 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
263 264 265 266 |
# File 'lib/virtus/attribute.rb', line 263 def define_reader_method(mod) mod.define_reader_method(self, name, @reader_visibility) 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
275 276 277 278 |
# File 'lib/virtus/attribute.rb', line 275 def define_writer_method(mod) mod.define_writer_method(self, "#{name}=".to_sym, @writer_visibility) 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
159 160 161 162 163 164 165 166 167 |
# File 'lib/virtus/attribute.rb', line 159 def get(instance) if instance.instance_variable_defined?(@instance_variable_name) get!(instance) else value = default.evaluate(instance) set!(instance, value) value end end |
#get!(instance) ⇒ Object
Returns the instance variable of the attribute
178 179 180 |
# File 'lib/virtus/attribute.rb', line 178 def get!(instance) instance.instance_variable_get(@instance_variable_name) end |
#inspect ⇒ String
Returns a concise string representation of the attribute instance
143 144 145 |
# File 'lib/virtus/attribute.rb', line 143 def inspect "#<#{self.class.inspect} @name=#{name.inspect}>" 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
285 286 287 |
# File 'lib/virtus/attribute.rb', line 285 def public_reader? @reader_visibility == :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
294 295 296 |
# File 'lib/virtus/attribute.rb', line 294 def public_writer? @writer_visibility == :public end |
#set(instance, value) ⇒ self
Sets the value on the instance
190 191 192 |
# File 'lib/virtus/attribute.rb', line 190 def set(instance, value) set!(instance, coerce(value)) end |
#set!(instance, value) ⇒ self
Sets instance variable of the attribute
202 203 204 205 |
# File 'lib/virtus/attribute.rb', line 202 def set!(instance, value) instance.instance_variable_set(@instance_variable_name, value) self end |
#value_coerced?(value) ⇒ 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.
Is the given value coerced into the target type for this attribute?
239 240 241 |
# File 'lib/virtus/attribute.rb', line 239 def value_coerced?(value) @primitive === value end |