Class: Virtus::Attribute::Coercer Private

Inherits:
Coercer
  • Object
show all
Defined in:
lib/virtus/attribute/coercer.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Coercer accessor wrapper

Instance Attribute Summary collapse

Attributes inherited from Coercer

#primitive, #type

Instance Method Summary collapse

Constructor Details

#initialize(type, coercers) ⇒ 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.

Initialize a new coercer object

Parameters:

  • coercers (Object)

    accessor

  • coercion (Symbol)

    method



20
21
22
23
24
# File 'lib/virtus/attribute/coercer.rb', line 20

def initialize(type, coercers)
  super(type)
  @method   = type.coercion_method
  @coercers = coercers
end

Instance Attribute Details

#coercersObject (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.



10
11
12
# File 'lib/virtus/attribute/coercer.rb', line 10

def coercers
  @coercers
end

#methodObject (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.



10
11
12
# File 'lib/virtus/attribute/coercer.rb', line 10

def method
  @method
end

Instance Method Details

#call(value) ⇒ 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.

Coerce given value

Returns:

  • (Object)


31
32
33
34
35
36
# File 'lib/virtus/attribute/coercer.rb', line 31

def call(value)
  call_method = (method == :to_hash && value.kind_of?(Hash)) ? :itself : method
  coercers[value.class].public_send(call_method, value)
rescue ::Coercible::UnsupportedCoercion
  value
end

#success?(primitive, value) ⇒ Boolean

Returns:



39
40
41
# File 'lib/virtus/attribute/coercer.rb', line 39

def success?(primitive, value)
  coercers[primitive].coerced?(value)
end