Class: Virtus::Coercer

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

Overview

Abstract coercer class

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type) ⇒ Coercer

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 Coercer.


12
13
14
15
# File 'lib/virtus/coercer.rb', line 12

def initialize(type)
  @type      = type
  @primitive = type.primitive
end

Instance Attribute Details

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


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

def primitive
  @primitive
end

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


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

def type
  @type
end

Instance Method Details

#call(input) ⇒ Object

Coerce input value into expected primitive type

Parameters:

  • input (Object)

Returns:

  • (Object)

    coerced input


24
25
26
# File 'lib/virtus/coercer.rb', line 24

def call(input)
  NotImplementedError.new("#{self.class}#call must be implemented")
end

#success?(primitive, input) ⇒ Object

Return if the input value was successfuly coerced

Parameters:

  • input (Object)

Returns:

  • (Object)

    coerced input


35
36
37
# File 'lib/virtus/coercer.rb', line 35

def success?(primitive, input)
  input.kind_of?(primitive)
end