Module: Params::Registry::Types

Defined in:
lib/params/registry/types.rb

Overview

All the type coercions used in Params::Registry.

A bunch of integer types collapse

DecimalInteger =

The problem with Kernel.Integer is that if a string representing a number begins with a zero it's treated as octal, so we have to compensate for that.

Nominal::Integer.constructor do |i|
  i.is_a?(::Numeric) ? i.to_i : ::Kernel.Integer(i.to_s, 10)
end
NonPositiveInteger =

xsd:nonPositiveInteger

DecimalInteger.constrained lteq: 0
NonNegativeInteger =

xsd:nonNegativeInteger

DecimalInteger.constrained gteq: 0
PositiveInteger =

xsd:positiveInteger

DecimalInteger.constrained gt: 0
NegativeInteger =

xsd:negativeInteger

DecimalInteger.constrained lt: 0

Stringy stuff, à la XSD plus some others collapse

NormalizedString =

This is xsd:normalizedString.

Nominal::String.constructor do |s|
  s.to_s.gsub(/[\t\r\n]/, ' ')
end
Token =

This is xsd:token.

NormalizedString.constructor { |s| s.tr_s(' ', ' ').strip }
Symbol =

Coerce an xsd:token into a Symbol.

Token.constructor { |t| t.to_sym }
LCSymbol =

Coerce an xsd:token into a symbol with all lower-case letters.

Token.constructor { |t| t.downcase.to_sym }
UCSymbol =

Do the same but with upper-case letters.

Token.constructor { |t| t.upcase.to_sym }
HyphenSymbol =

Create a symbol with all whitespace and underscores turned to hyphens.

Token.constructor { |t| t.tr_s(' _', ?-).to_sym }
LCHyphenSymbol =

Do the same but with all lower-case letters.

HyphenSymbol.constructor { |s| s.to_s.downcase.to_sym }
UCHyphenSymbol =

Do the same but with all upper-case letters.

HyphenSymbol.constructor { |s| s.to_s.upcase.to_sym }
UnderscoreSymbol =

Create a symbol with all whitespace and hyphens turned to underscores.

Token.constructor { |t| t.tr_s(' -', ?_).to_sym }
LCUnderscoreSymbol =

Do the same but with all lower-case letters.

UnderscoreSymbol.constructor do |s|
  s.to_s.downcase.to_sym
end
UCUnderscoreSymbol =

Do the same but with all upper-case letters.

UnderscoreSymbol.constructor do |s|
  s.to_s.upcase.to_sym
end
Symbolish =

This one is symbol-ish

self.Constructor(::Object) do |x|
  if [::String, ::Symbol].any? { |c| x.is_a? c }
    Symbol[x]
  else
    x
  end
end

Dates & Times collapse

Date =

Ye olde Date

self.Constructor(::Date) do |x|
  case x
  when ::Array then ::Date.new(*x.take(3))
  else ::Date.parse x
  end
end
DateTime =

And DateTime

self.Constructor(::DateTime) do |x|
  ::DateTime.parse x
end
Time =

Aaand Time

self.Constructor(::Time) do |x|
  case x
  when ::Array then ::Time.new(*x)
  when (DecimalInteger[x] rescue nil) then ::Time.at(DecimalInteger[x])
  else ::Time.parse x
  end
end

Composite types not already defined collapse

Set =

Set

self.Constructor(::Set) { |x| ::Set[*x] }
Range =

Range

self.Constructor(::Range) { |x| ::Range.new(*x.take(2)) }
Registry =

The registry itself

self.Instance(::Params::Registry)
TemplateSpec =

Templates

Hash.map(Symbol, Strict::Any)
TemplateMap =
Hash|Hash.map(NonNil, TemplateSpec)
GroupMap =

Groups

Hash|Hash.map(NonNil, Array|TemplateMap)
Input =
self.Constructor(::Hash) do |input|
  input = input.query.to_s if input.is_a? ::URI
  input = ::URI.decode_www_form input if input.is_a? ::String

  case input
  when ::Hash then Hash.map(Symbolish, Array.of(String))[input]
  when ::Array
    input.reduce({}) do |out, pair|
      k, *v = Strict::Array.constrained(min_size: 2)[pair]
      (out[Symbolish[k]] ||= []).push(*v)
      out
    end
  else
    raise Dry::Types::CoercionError,
      "not sure what to do with #{input.inspect}"
  end
end

Constant Summary collapse

NonNil =

Can be anything, as long as it isn't nil.

Strict::Any.constrained not_eql: nil
Bool =

Gotta have a coercible boolean (which doesn't come stock for some reason)

Nominal::Bool.constructor do |x|
  case x.to_s.strip
  when /\A(1|true|on|yes)\Z/i then true
  when /\A(0|false|off|no|)\Z/i then false
  else
    raise Dry::Types::CoercionError, "#{x} can't be coerced to true or false"
  end
end
Proc =

For some reason there isn't a stock Proc type.

self.Instance(::Proc)

Class Method Summary collapse

Class Method Details

.[](const) ⇒ Dry::Types::Type

Syntactic sugar for retrieving types in the library.

Parameters:

  • const (#to_sym)

    The type (name).

Returns:

  • (Dry::Types::Type)

    The type instance.



21
22
23
24
25
26
27
28
# File 'lib/params/registry/types.rb', line 21

def self.[] const
  return const if const.is_a? Dry::Types::Type
  begin
    const_get const.to_s.to_sym
  rescue NameError
    raise ArgumentError, "No type named #{const}"
  end
end