Sha256: 5aa48ffc8170ae09814785250c7b3685787a49232b91d966cb2738ef1038e45e

Contents?: true

Size: 881 Bytes

Versions: 8

Compression:

Stored size: 881 Bytes

Contents

module Alf
  module Support

    # Defines all coercion rules, through Myrrha inheritance
    Coercions = Myrrha::Coerce.dup.append do |g|
      g.error_handler = lambda{|v,t,c|
        raise TypeError, "Unable to coerce `#{v.inspect}` to `#{t}`", caller
      }
      g.coercion String, Time,     lambda{|s,t| Time.parse(s) }
      g.coercion String, DateTime, lambda{|s,t| DateTime.parse(s) }
    end

    # Coerces a value to a particular domain.
    #
    # Example:
    #
    #   Support.coerce("123", Integer) # => 123
    #
    # @param [Object] val any value
    # @param [Class] domain a domain, represented by a ruby class
    # @return [Object] an instance of `domain` resulting from the coercion
    # @raise [Myrrha::CoercionError] if something goes wrong
    def coerce(val, domain)
      Coercions.apply(val, domain)
    end

  end # module Support
end # module Alf

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
alf-core-0.16.3 lib/alf/support/coerce.rb
alf-core-0.16.2 lib/alf/support/coerce.rb
alf-core-0.16.1 lib/alf/support/coerce.rb
alf-core-0.16.0 lib/alf/support/coerce.rb
alf-core-0.15.0 lib/alf/support/coerce.rb
alf-core-0.14.0 lib/alf-support/alf/support/coerce.rb
alf-core-0.13.1 lib/alf-support/alf/support/coerce.rb
alf-core-0.13.0 lib/alf-support/alf/support/coerce.rb