class Formalism::Form::Coercion

Class for coercion (check, initialization)

Public Class Methods

new(type, of = nil) click to toggle source
# File lib/formalism/form/coercion.rb, line 16
def initialize(type, of = nil)
        @type = type
        @of = of
end

Public Instance Methods

check() click to toggle source
# 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) click to toggle source
# 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

Private Instance Methods

const_name() click to toggle source
# File lib/formalism/form/coercion.rb, line 42
def const_name
        @type.to_s.camelize
end
exist?() click to toggle source
# File lib/formalism/form/coercion.rb, line 46
def exist?
        self.class.const_defined?(const_name, false)
rescue NameError
        false
end