class Virtus::Attribute
Attribute objects handle coercion and provide interface to hook into an attribute set instance that's included into a class or object
@example
# non-strict mode attr = Virtus::Attribute.build(Integer) attr.coerce('1') # => 1 # strict mode attr = Virtus::Attribute.build(Integer, :strict => true) attr.coerce('not really coercible') # => Virtus::CoercionError: Failed to coerce "fsafa" into Integer
Attributes
@api private
@api private
@api private
@api private
Return type of this attribute
@return [Axiom::Types::Type]
@api public
Public Class Methods
Builds an attribute instance
@param [Class,Array,Hash,String,Symbol] type
this can be an explicit class or an object from which virtus can infer the type
@param [#to_hash] options
optional extra options hash
@return [Attribute]
@api public
# File lib/virtus/attribute.rb, line 65 def self.build(type, options = {}) Builder.call(type, options) end
@api private
# File lib/virtus/attribute.rb, line 70 def self.build_coercer(type, options = {}) Coercer.new(type, options.fetch(:configured_coercer) { Virtus.coercer }) end
@api private
# File lib/virtus/attribute.rb, line 75 def self.build_type(definition) Axiom::Types.infer(definition.primitive) end
@see Virtus.coerce
@deprecated
@api public
# 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
@api private
# File lib/virtus/attribute.rb, line 80 def self.merge_options!(*) # noop end
@api private
# File lib/virtus/attribute.rb, line 85 def initialize(type, options) @type = type @primitive = type.primitive @options = options @default_value = options.fetch(:default_value) @coercer = options.fetch(:coercer) end
Public Instance Methods
Coerce the input into the expected type
@example
attr = Virtus::Attribute.build(String) attr.coerce(:one) # => 'one'
@param [Object] input
@api public
# File lib/virtus/attribute.rb, line 103 def coerce(input) coercer.call(input) end
Return if the attribute is coercible
@example
attr = Virtus::Attribute.build(String, :coerce => true) attr.coercible? # => true attr = Virtus::Attribute.build(String, :coerce => false) attr.coercible? # => false
@return [Boolean]
@api public
# File lib/virtus/attribute.rb, line 142 def coercible? kind_of?(Coercible) end
@api private
# File lib/virtus/attribute.rb, line 232 def define_accessor_methods(attribute_set) attribute_set.define_reader_method(self, name, options[:reader]) attribute_set.define_writer_method(self, "#{name}=", options[:writer]) end
@api private
# File lib/virtus/attribute.rb, line 238 def finalize freeze self end
Return if the attribute was already finalized
@example
attr = Virtus::Attribute.build(String, :finalize => true) attr.finalized? # => true attr = Virtus::Attribute.build(String, :finalize => false) attr.finalized? # => false
@return [Boolean]
@api public
# File lib/virtus/attribute.rb, line 227 def finalized? frozen? end
Return if the attribute has lazy default value evaluation
@example
attr = Virtus::Attribute.build(String, :lazy => true) attr.lazy? # => true attr = Virtus::Attribute.build(String, :lazy => false) attr.lazy? # => false
@return [Boolean]
@api public
# File lib/virtus/attribute.rb, line 159 def lazy? kind_of?(LazyDefault) end
Return if the attribute is in the nullify blank coercion mode
@example
attr = Virtus::Attribute.build(String, :nullify_blank => true) attr.nullify_blank? # => true attr = Virtus::Attribute.build(String, :nullify_blank => false) attr.nullify_blank? # => false
@return [Boolean]
@api public
# File lib/virtus/attribute.rb, line 193 def nullify_blank? kind_of?(NullifyBlank) end
Return a new attribute with the new name
@param [Symbol] name
@return [Attribute]
@api public
# File lib/virtus/attribute.rb, line 114 def rename(name) self.class.build(type, options.merge(:name => name)) end
Return if the attribute is accepts nil values as valid coercion output
@example
attr = Virtus::Attribute.build(String, :required => true) attr.required? # => true attr = Virtus::Attribute.build(String, :required => false) attr.required? # => false
@return [Boolean]
@api public
# File lib/virtus/attribute.rb, line 210 def required? options[:required] end
Return if the attribute is in the strict coercion mode
@example
attr = Virtus::Attribute.build(String, :strict => true) attr.strict? # => true attr = Virtus::Attribute.build(String, :strict => false) attr.strict? # => false
@return [Boolean]
@api public
# File lib/virtus/attribute.rb, line 176 def strict? kind_of?(Strict) end
Return if the given value was coerced
@param [Object] value
@return [Boolean]
@api public
# File lib/virtus/attribute.rb, line 125 def value_coerced?(value) coercer.success?(primitive, value) end