Class: Virtus::Attribute
- Inherits:
-
Object
- Object
- Virtus::Attribute
- Extended by:
- DescendantsTracker, Options, TypeLookup
- Defined in:
- lib/virtus/attribute.rb,
lib/virtus/attribute/hash.rb,
lib/virtus/attribute/strict.rb,
lib/virtus/attribute/boolean.rb,
lib/virtus/attribute/builder.rb,
lib/virtus/attribute/coercer.rb,
lib/virtus/attribute/accessor.rb,
lib/virtus/attribute/coercible.rb,
lib/virtus/attribute/collection.rb,
lib/virtus/attribute/lazy_default.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
Attribute objects handle coercion and provide interface to hook into an attribute set instance that’s included into a class or object
Direct Known Subclasses
Defined Under Namespace
Modules: Accessor, Coercible, LazyDefault, Strict Classes: Boolean, Builder, Coercer, Collection, DefaultValue, EmbeddedValue, Hash
Constant Summary
Constants included from TypeLookup
Instance Attribute Summary collapse
- #coercer ⇒ Object readonly private
- #default_value ⇒ Object readonly private
- #options ⇒ Object readonly private
- #primitive ⇒ Object readonly private
-
#type ⇒ Axiom::Types::Type
readonly
Return type of this attribute.
Class Method Summary collapse
-
.build(type, options = {}) ⇒ Attribute
Builds an attribute instance.
- .build_coercer(type, options = {}) ⇒ Object private
- .build_type(definition) ⇒ Object private
- .coerce(value = Undefined) ⇒ Object deprecated Deprecated.
- .merge_options! ⇒ Object private
Instance Method Summary collapse
-
#coerce(input) ⇒ Object
Coerce the input into the expected type.
-
#coercible? ⇒ Boolean
Return if the attribute is coercible.
- #define_accessor_methods(attribute_set) ⇒ Object private
- #finalize ⇒ Object private
-
#finalized? ⇒ Boolean
Return if the attribute was already finalized.
-
#initialize(type, options) ⇒ Attribute
constructor
private
A new instance of Attribute.
-
#lazy? ⇒ Boolean
Return if the attribute has lazy default value evaluation.
-
#rename(name) ⇒ Attribute
Return a new attribute with the new name.
-
#required? ⇒ Boolean
Return if the attribute is accepts nil values as valid coercion output.
-
#strict? ⇒ Boolean
Return if the attribute is in the strict coercion mode.
-
#value_coerced?(value) ⇒ Boolean
Return if the given value was coerced.
Methods included from TypeLookup
Methods included from Options
accept_options, accepted_options
Constructor Details
#initialize(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.
Returns a new instance of Attribute.
84 85 86 87 88 89 90 |
# File 'lib/virtus/attribute.rb', line 84 def initialize(type, ) @type = type @primitive = type.primitive @options = @default_value = .fetch(:default_value) @coercer = .fetch(:coercer) end |
Instance Attribute Details
#coercer ⇒ 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.
50 51 52 |
# File 'lib/virtus/attribute.rb', line 50 def coercer @coercer end |
#default_value ⇒ 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.
50 51 52 |
# File 'lib/virtus/attribute.rb', line 50 def default_value @default_value end |
#options ⇒ 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.
50 51 52 |
# File 'lib/virtus/attribute.rb', line 50 def @options end |
#primitive ⇒ 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.
50 51 52 |
# File 'lib/virtus/attribute.rb', line 50 def primitive @primitive end |
#type ⇒ Axiom::Types::Type (readonly)
Return type of this attribute
47 48 49 |
# File 'lib/virtus/attribute.rb', line 47 def type @type end |
Class Method Details
.build(type, options = {}) ⇒ Attribute
Builds an attribute instance
64 65 66 |
# File 'lib/virtus/attribute.rb', line 64 def self.build(type, = {}) Builder.call(type, ) end |
.build_coercer(type, options = {}) ⇒ 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.
69 70 71 |
# File 'lib/virtus/attribute.rb', line 69 def self.build_coercer(type, = {}) Coercer.new(type, .fetch(:configured_coercer) { Virtus.coercer }) end |
.build_type(definition) ⇒ 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.
74 75 76 |
# File 'lib/virtus/attribute.rb', line 74 def self.build_type(definition) Axiom::Types.infer(definition.primitive) end |
.coerce(value = Undefined) ⇒ Object
35 36 37 38 39 40 |
# File 'lib/virtus/attribute.rb', line 35 def self.coerce(value = Undefined) Virtus.warn "#{self}.coerce is deprecated and will be removed in 1.0.0. Use Virtus.coerce instead: ##{caller.first}" return Virtus.coerce if value.equal?(Undefined) Virtus.coerce = value self end |
.merge_options! ⇒ 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.
79 80 81 |
# File 'lib/virtus/attribute.rb', line 79 def self.(*) # noop end |
Instance Method Details
#coerce(input) ⇒ Object
Coerce the input into the expected type
102 103 104 |
# File 'lib/virtus/attribute.rb', line 102 def coerce(input) coercer.call(input) end |
#coercible? ⇒ Boolean
Return if the attribute is coercible
141 142 143 |
# File 'lib/virtus/attribute.rb', line 141 def coercible? kind_of?(Coercible) end |
#define_accessor_methods(attribute_set) ⇒ 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.
214 215 216 217 |
# File 'lib/virtus/attribute.rb', line 214 def define_accessor_methods(attribute_set) attribute_set.define_reader_method(self, name, [:reader]) attribute_set.define_writer_method(self, "#{name}=", [:writer]) end |
#finalize ⇒ 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.
220 221 222 223 |
# File 'lib/virtus/attribute.rb', line 220 def finalize freeze self end |
#finalized? ⇒ Boolean
Return if the attribute was already finalized
209 210 211 |
# File 'lib/virtus/attribute.rb', line 209 def finalized? frozen? end |
#lazy? ⇒ Boolean
Return if the attribute has lazy default value evaluation
158 159 160 |
# File 'lib/virtus/attribute.rb', line 158 def lazy? kind_of?(LazyDefault) end |
#rename(name) ⇒ Attribute
Return a new attribute with the new name
113 114 115 |
# File 'lib/virtus/attribute.rb', line 113 def rename(name) self.class.build(type, .merge(:name => name)) end |
#required? ⇒ Boolean
Return if the attribute is accepts nil values as valid coercion output
192 193 194 |
# File 'lib/virtus/attribute.rb', line 192 def required? [:required] end |
#strict? ⇒ Boolean
Return if the attribute is in the strict coercion mode
175 176 177 |
# File 'lib/virtus/attribute.rb', line 175 def strict? kind_of?(Strict) end |
#value_coerced?(value) ⇒ Boolean
Return if the given value was coerced
124 125 126 |
# File 'lib/virtus/attribute.rb', line 124 def value_coerced?(value) coercer.success?(primitive, value) end |