Module: ABBYY::Cloud::Types

Defined in:
lib/abbyy/cloud/types.rb

Constant Summary collapse

VERSIONS =
[0].freeze
UNIT_TYPES =
%w(Chars Words Pages Documents).freeze
DISCOUNT_TYPES =
%w(TMTextMatch TMTaggedTextMatch TMHalfContextMatch TMFullContextMatch)
.freeze
Version =

Gem-specific primitive types

Coercible::Int.constrained(included_in: VERSIONS)
UnitType =
Strict::String.constrained(included_in: UNIT_TYPES)
DiscountType =
Strict::String.constrained(included_in: DISCOUNT_TYPES)
Currency =
Strict::String.constrained(format: /\A[A-Z]{3}\z/)

Class Method Summary collapse

Class Method Details

.register_type(struct, as: nil, constructor: :new) ⇒ Object

Registers new coercible type from a struct class



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/abbyy/cloud/types.rb', line 17

def self.register_type(struct, as: nil, constructor: :new)
  type_name  = Inflecto.underscore(as || struct.name.split("::").last)
  definition = Dry::Types::Definition.new(struct).constructor do |value|
    case value
    when nil    then raise
    when struct then value
    else struct.send(constructor, value)
    end
  end

  Dry::Types.register type_name, definition
  Dry::Types.define_constants self, [type_name]
end