Module: Dry::Types::BuilderMethods
- Defined in:
- lib/dry/types/builder_methods.rb
Overview
Common API for building type objects in a convenient way
Instance Method Summary collapse
-
#Array(type) ⇒ Dry::Types::Array
Build an array type.
-
#Constant(object) ⇒ Dry::Types::Type
Build a type with a single value The equality check done with
equal?
. -
#Constructor(klass, cons = nil, &block) ⇒ Dry::Types::Type
Build a constructor type If no constructor block given it uses .new method.
-
#Hash(type_map) ⇒ Dry::Types::Array
Build a hash schema.
- #included(base) ⇒ Object private
-
#Instance(klass) ⇒ Dry::Types::Type
(also: #Strict)
Build a type which values are instances of a given class Values are checked using
is_a?
call. -
#Interface(*methods) ⇒ Dry::Types::Contrained
Builds a constrained nominal type accepting any value that responds to given methods.
-
#Map(key_type, value_type) ⇒ Dry::Types::Map
Build a map type.
-
#Nominal(klass) ⇒ Dry::Types::Type
Build a nominal type.
-
#Value(value) ⇒ Dry::Types::Type
Build a type with a single value The equality check done with
eql?
.
Instance Method Details
#Array(type) ⇒ Dry::Types::Array
Build an array type.
Shortcut for Array#of.
26 27 28 |
# File 'lib/dry/types/builder_methods.rb', line 26 def Array(type) Strict(::Array).of(type) end |
#Constant(object) ⇒ Dry::Types::Type
Build a type with a single value
The equality check done with equal?
71 72 73 |
# File 'lib/dry/types/builder_methods.rb', line 71 def Constant(object) Nominal(object.class).constrained(is: object) end |
#Constructor(klass, cons = nil, &block) ⇒ Dry::Types::Type
Build a constructor type If no constructor block given it uses .new method
83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/dry/types/builder_methods.rb', line 83 def Constructor(klass, cons = nil, &block) # rubocop:disable Metrics/PerceivedComplexity: if klass.is_a?(Type) if cons || block klass.constructor(cons || block) else klass end else Nominal(klass).constructor(cons || block || klass.method(:new)) end end |
#Hash(type_map) ⇒ Dry::Types::Array
Build a hash schema
35 36 37 |
# File 'lib/dry/types/builder_methods.rb', line 35 def Hash(type_map) Strict(::Hash).schema(type_map) end |
#included(base) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
11 12 13 14 |
# File 'lib/dry/types/builder_methods.rb', line 11 def included(base) super base.extend(BuilderMethods) end |
#Instance(klass) ⇒ Dry::Types::Type Also known as: Strict
Build a type which values are instances of a given class
Values are checked using is_a?
call
50 51 52 |
# File 'lib/dry/types/builder_methods.rb', line 50 def Instance(klass) Nominal(klass).constrained(type: klass) end |
#Interface(*methods) ⇒ Dry::Types::Contrained
Builds a constrained nominal type accepting any value that responds to given methods
134 135 136 137 138 |
# File 'lib/dry/types/builder_methods.rb', line 134 def Interface(*methods) methods.reduce(Types["nominal.any"]) do |type, method| type.constrained(respond_to: method) end end |
#Map(key_type, value_type) ⇒ Dry::Types::Map
Build a map type
120 121 122 |
# File 'lib/dry/types/builder_methods.rb', line 120 def Map(key_type, value_type) Nominal(::Hash).map(key_type, value_type) end |
#Nominal(klass) ⇒ Dry::Types::Type
Build a nominal type
100 101 102 103 104 105 106 107 108 |
# File 'lib/dry/types/builder_methods.rb', line 100 def Nominal(klass) if klass <= ::Array Array.new(klass) elsif klass <= ::Hash Hash.new(klass) else Nominal.new(klass) end end |
#Value(value) ⇒ Dry::Types::Type
Build a type with a single value
The equality check done with eql?
61 62 63 |
# File 'lib/dry/types/builder_methods.rb', line 61 def Value(value) Nominal(value.class).constrained(eql: value) end |