module Dry::Types::BuilderMethods
Common API for building type objects in a convenient way
@api public
Public Instance Methods
Build an array type.
Shortcut for Array#of
.
@example
Types::Strings = Types.Array(Types::String)
@param [Dry::Types::Type] type
@return [Dry::Types::Array]
# File lib/dry/types/builder_methods.rb, line 26 def Array(type) Strict(::Array).of(type) end
Build a type with a single value The equality check done with ‘equal?`
@param [Object] object
@return [Dry::Types::Type]
# File lib/dry/types/builder_methods.rb, line 71 def Constant(object) Nominal(object.class).constrained(is: object) end
Build a constructor type If no constructor block given it uses .new method
@param [Class] klass @param [#call,nil] cons Value
constructor @param [#call,nil] block Value
constructor
@return [Dry::Types::Type]
# 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
Build a hash schema
@param [Hash{Symbol => Dry::Types::Type
}] type_map
@return [Dry::Types::Array]
# File lib/dry/types/builder_methods.rb, line 35 def Hash(type_map) Strict(::Hash).schema(type_map) end
Build a type which values are instances of a given class Values are checked using ‘is_a?` call
@example
Types::Error = Types.Instance(StandardError) Types::Error = Types.Strict(StandardError) Types.Strict(Integer) == Types::Strict::Int # => true
@param [Class,Module] klass Class or module
@return [Dry::Types::Type]
# File lib/dry/types/builder_methods.rb, line 50 def Instance(klass) Nominal(klass).constrained(type: klass) end
Builds a constrained nominal type accepting any value that responds to given methods
@example
Types::Callable = Types.Interface(:call) Types::Contact = Types.Interface(:name, :address)
@param methods [Array<String, Symbol>] Method names
@return [Dry::Types::Contrained]
# 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
Build a map type
@example
Types::IntMap = Types.Map(Types::Strict::Integer, 'any') Types::IntStringMap = Types.Map(Types::Strict::Integer, Types::Strict::String)
@param [Type] key_type Key type @param [Type] value_type Value
type
@return [Dry::Types::Map]
# File lib/dry/types/builder_methods.rb, line 120 def Map(key_type, value_type) Nominal(::Hash).map(key_type, value_type) end
Build a nominal type
@param [Class] klass
@return [Dry::Types::Type]
# 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
Build a type with a single value The equality check done with ‘eql?`
@param [Object] value
@return [Dry::Types::Type]
# File lib/dry/types/builder_methods.rb, line 61 def Value(value) Nominal(value.class).constrained(eql: value) end
@api private
# File lib/dry/types/builder_methods.rb, line 11 def included(base) super base.extend(BuilderMethods) end