1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
|
module Virtus
class Attribute
# Coercer accessor wrapper
#
# @api private
class Coercer < Virtus::Coercer
# @api private
attr_reader :method, :coercers
# Initialize a new coercer object
#
# @param [Object] coercers accessor
# @param [Symbol] coercion method
#
# @return [undefined]
#
# @api private
def initialize(type, coercers)
super(type)
@method = type.coercion_method
@coercers = coercers
end
# Coerce given value
#
# @return [Object]
#
# @api private
def call(value)
coercers[value.class].public_send(method, value)
rescue ::Coercible::UnsupportedCoercion
value
end
# @api public
def success?(primitive, value)
coercers[primitive].coerced?(value)
end
end # class Coercer
end # class Attribute
end # module Virtus
|