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.



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

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.



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

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.



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

def type
  @type
end

Instance Method Details

#call(input) ⇒ Object

Coerce input value into expected primitive type

Parameters:

  • input (Object)

Returns:

  • (Object)

    coerced input



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

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



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

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