class Dry::Interface

Public Class Methods

const_missing(name) click to toggle source

@param name [Symbol]

@return [Abstract::Class]

Calls superclass method
# File lib/dry/interface.rb, line 105
def self.const_missing(name)
  case name
  in :Concrete
    Class.new(self) do
      prepend Interfaces::Concrete
    end
  in :Abstract
    Class.new(self) do
      prepend Interfaces::Abstract
    end
  in :Unit
    Class.new(self) do
      prepend Interfaces::Unit
    end
  else
    super
  end
end
order(*names) click to toggle source

Allow types structs to be ordered

@param names [Array<Symbol>]

# File lib/dry/interface.rb, line 36
def self.order(*names)
  result = names.each_with_index.reduce(EMPTY_HASH) do |acc, (name, index)|
    acc.merge(name.to_s => index)
  end

  config.order = result
end
reduce(input, subtype) click to toggle source
# File lib/dry/interface.rb, line 49
def self.reduce(input, subtype)
  case input
  in { result: }
    input
  in { value: }
    { result: subtype.call(value) }
  end
rescue Dry::Struct::Error => e
  em = Dry::Types::ConstraintError.new(e.message, input.fetch(:value))
  input.merge(errors: input.fetch(:errors, []) + [em])
rescue Dry::Types::CoercionError => e
  input.merge(errors: input.fetch(:errors, []) + [e])
end
subtype() click to toggle source

Internal type represented by {self}

@return [Dry::Struct::Sum, Dry::Struct::Class]

# File lib/dry/interface.rb, line 66
def self.subtype
  Constructor(self) do |value, _type, &error|
    error ||= -> error do
      raise error
    end

    if subtypes.empty?
      raise NotImplementedError, "No subtypes defined for #{name}"
    end

    output = subtypes.reduce({ value: value }, &method(:reduce))

    case output
    in { result: }
      result
    in { errors: }
      error[Dry::Types::MultipleError.new(errors)]
    in Dry::Struct
      output
    end
  end
end
subtypes() click to toggle source

Internal types represented by {self}

@return [Dry::Struct::Class]

# File lib/dry/interface.rb, line 92
def self.subtypes
  types = subclasses.flat_map(&:subclasses)

  return types if config.order.empty?

  types.sort_by do |type|
    config.order.fetch(demodulize(type.name))
  end
end
to_s() click to toggle source

@return [String]

# File lib/dry/interface.rb, line 45
def self.to_s
  "%s<[%s]>" % [name, subtypes.map(&:to_s).join(" | ")]
end