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/nullify_blank.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, NullifyBlank, 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.
-
#nullify_blank? ⇒ Boolean
Return if the attribute is in the nullify blank coercion mode.
-
#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.
85 86 87 88 89 90 91 |
# File 'lib/virtus/attribute.rb', line 85 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.
51 52 53 |
# File 'lib/virtus/attribute.rb', line 51 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.
51 52 53 |
# File 'lib/virtus/attribute.rb', line 51 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.
51 52 53 |
# File 'lib/virtus/attribute.rb', line 51 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.
51 52 53 |
# File 'lib/virtus/attribute.rb', line 51 def primitive @primitive end |
#type ⇒ Axiom::Types::Type (readonly)
Return type of this attribute
48 49 50 |
# File 'lib/virtus/attribute.rb', line 48 def type @type end |
Class Method Details
.build(type, options = {}) ⇒ Attribute
Builds an attribute instance
65 66 67 |
# File 'lib/virtus/attribute.rb', line 65 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.
70 71 72 |
# File 'lib/virtus/attribute.rb', line 70 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.
75 76 77 |
# File 'lib/virtus/attribute.rb', line 75 def self.build_type(definition) Axiom::Types.infer(definition.primitive) end |
.coerce(value = Undefined) ⇒ Object
36 37 38 39 40 41 |
# File 'lib/virtus/attribute.rb', line 36 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.
80 81 82 |
# File 'lib/virtus/attribute.rb', line 80 def self.(*) # noop end |
Instance Method Details
#coerce(input) ⇒ Object
Coerce the input into the expected type
103 104 105 |
# File 'lib/virtus/attribute.rb', line 103 def coerce(input) coercer.call(input) end |
#coercible? ⇒ Boolean
Return if the attribute is coercible
142 143 144 |
# File 'lib/virtus/attribute.rb', line 142 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.
232 233 234 235 |
# File 'lib/virtus/attribute.rb', line 232 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.
238 239 240 241 |
# File 'lib/virtus/attribute.rb', line 238 def finalize freeze self end |
#finalized? ⇒ Boolean
Return if the attribute was already finalized
227 228 229 |
# File 'lib/virtus/attribute.rb', line 227 def finalized? frozen? end |
#lazy? ⇒ Boolean
Return if the attribute has lazy default value evaluation
159 160 161 |
# File 'lib/virtus/attribute.rb', line 159 def lazy? kind_of?(LazyDefault) end |
#nullify_blank? ⇒ Boolean
Return if the attribute is in the nullify blank coercion mode
193 194 195 |
# File 'lib/virtus/attribute.rb', line 193 def nullify_blank? kind_of?(NullifyBlank) end |
#rename(name) ⇒ Attribute
Return a new attribute with the new name
114 115 116 |
# File 'lib/virtus/attribute.rb', line 114 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
210 211 212 |
# File 'lib/virtus/attribute.rb', line 210 def required? [:required] end |
#strict? ⇒ Boolean
Return if the attribute is in the strict coercion mode
176 177 178 |
# File 'lib/virtus/attribute.rb', line 176 def strict? kind_of?(Strict) end |
#value_coerced?(value) ⇒ Boolean
Return if the given value was coerced
125 126 127 |
# File 'lib/virtus/attribute.rb', line 125 def value_coerced?(value) coercer.success?(primitive, value) end |