Class: Formalism::Form::Coercion

Inherits:
Object
  • Object
show all
Defined in:
lib/formalism/form/coercion.rb,
lib/formalism/form/coercion/date.rb,
lib/formalism/form/coercion/hash.rb,
lib/formalism/form/coercion/time.rb,
lib/formalism/form/coercion/_base.rb,
lib/formalism/form/coercion/array.rb,
lib/formalism/form/coercion/class.rb,
lib/formalism/form/coercion/float.rb,
lib/formalism/form/coercion/object.rb,
lib/formalism/form/coercion/string.rb,
lib/formalism/form/coercion/symbol.rb,
lib/formalism/form/coercion/boolean.rb,
lib/formalism/form/coercion/integer.rb,
lib/formalism/form/coercion/_numeric.rb,
lib/formalism/form/coercion/bigdecimal.rb

Overview

Class for coercion (check, initialization)

Defined Under Namespace

Classes: Array, Base, BigDecimal, Boolean, Class, Date, Float, Hash, Integer, Numeric, Object, String, Symbol, Time

Instance Method Summary collapse

Constructor Details

#initialize(type, of = nil) ⇒ Coercion

Returns a new instance of Coercion.



16
17
18
19
# File 'lib/formalism/form/coercion.rb', line 16

def initialize(type, of = nil)
  @type = type
  @of = of
end

Instance Method Details

#checkObject

Raises:



21
22
23
24
25
26
27
28
29
30
# File 'lib/formalism/form/coercion.rb', line 21

def check
  ## It's custom error! But cop triggers for single argument anyway.
  # rubocop:disable Style/RaiseArgs
  raise NoCoercionError.new(@type) unless exist?
  # rubocop:enable Style/RaiseArgs

  return unless const_name == 'Array' && @of

  self.class.new(@of).check
end

#result_for(value) ⇒ Object



32
33
34
35
36
# File 'lib/formalism/form/coercion.rb', line 32

def result_for(value)
  coercion_class = exist? ? const_name : 'Base'

  self.class.const_get(coercion_class, false).new(value, @of).result
end