Class: Compel::Coercion::Type

Inherits:
Object
  • Object
show all
Defined in:
lib/compel/coercion/types/type.rb

Direct Known Subclasses

Any, Array, Boolean, DateType, Float, Hash, Integer, JSON, Regexp, String

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value, options = {}) ⇒ Type

Returns a new instance of Type.



13
14
15
16
# File 'lib/compel/coercion/types/type.rb', line 13

def initialize(value, options = {})
  @value = value
  @options = options
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



6
7
8
# File 'lib/compel/coercion/types/type.rb', line 6

def options
  @options
end

#valueObject

Returns the value of attribute value.



6
7
8
# File 'lib/compel/coercion/types/type.rb', line 6

def value
  @value
end

Class Method Details

.coerce(value, options = {}) ⇒ Object



9
10
11
# File 'lib/compel/coercion/types/type.rb', line 9

def self.coerce(value, options = {})
  new(value, options).coerce
end

.human_nameObject



32
33
34
# File 'lib/compel/coercion/types/type.rb', line 32

def human_name
  "#{self.name.split('::')[-1]}"
end

Instance Method Details

#coerceObject



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/compel/coercion/types/type.rb', line 18

def coerce
  result = coerce_value

  # There are some special cases that
  # we need to build a custom error
  if result.is_a?(Result)
    return result
  end

  Coercion::Result.new(result, value, self.class)
end