Module: Structure::Types
- Defined in:
- lib/structure/types.rb
Overview
Type coercion methods for converting values to specific types
Class Method Summary collapse
-
.coerce(type, context = nil) ⇒ Proc?
Main factory method for creating type coercers.
Class Method Details
.coerce(type, context = nil) ⇒ Proc?
Main factory method for creating type coercers
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/structure/types.rb', line 42 def coerce(type, context = nil) case type when :boolean boolean when :self self_referential(context) when ->(t) { t.respond_to?(:name) && t.name && Kernel.respond_to?(t.name) } kernel(type) when ->(t) { t.is_a?(Array) && t.length == 1 } array(type.first, context) when ->(t) { t.respond_to?(:parse) } parseable(type) when nil type when ->(t) { t.respond_to?(:call) } type when String lazy_class(type, context) else raise ArgumentError, "Cannot specify #{type.inspect} as type" end end |