Sha256: e8f15e2cbee04b9a09b8155634fb6c3c3881455cedc7873cc0235d13b110252c

Contents?: true

Size: 664 Bytes

Versions: 3

Compression:

Stored size: 664 Bytes

Contents

module Compel

  module Coercion

    def valid?(value, type, options = {})
      !!coerce!(value, type, options) rescue false
    end

    def coerce!(value, type, options = {})
      return nil if value.nil?

      begin
        klass = compel_type?(type) ? type : get_compel_type_klass(type)

        return klass.new(value, options).coerce!
      rescue
        raise ParamTypeError, "'#{value}' is not a valid #{type}"
      end
    end

    def compel_type?(type)
      type.to_s.split('::')[0..1].join('::') == 'Compel::Coercion'
    end

    def get_compel_type_klass(type)
      const_get("Compel::Coercion::#{type}")
    end

    extend self

  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
compel-0.1.3 lib/compel/coercion.rb
compel-0.1.2 lib/compel/coercion.rb
compel-0.1.1 lib/compel/coercion.rb