Class: Dry::Interface
- Includes:
- ActiveSupport::Configurable
- Defined in:
- lib/dry/interface.rb,
lib/dry/interface/patch.rb,
lib/dry/interface/types.rb,
lib/dry/interface/interfaces.rb,
lib/dry/interface/interfaces/unit.rb,
lib/dry/interface/interfaces/abstract.rb,
lib/dry/interface/interfaces/concrete.rb
Defined Under Namespace
Modules: Interfaces, Patch, Types
Class Method Summary collapse
- .const_missing(name) ⇒ Abstract::Class
-
.order(*names) ⇒ Object
Allow types structs to be ordered.
- .reduce(input, subtype) ⇒ Object
-
.subtype ⇒ Dry::Struct::Sum, Dry::Struct::Class
Internal type represented by self.
-
.subtypes ⇒ Dry::Struct::Class
Internal types represented by self.
- .to_s ⇒ String
Methods inherited from Concrete
alias_fields, attribute, attribute?, build_type_from, initializer
Class Method Details
.const_missing(name) ⇒ Abstract::Class
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# 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) ⇒ Object
Allow types structs to be ordered
36 37 38 39 40 41 42 |
# 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) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 |
# 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., input.fetch(:value)) input.merge(errors: input.fetch(:errors, []) + [em]) rescue Dry::Types::CoercionError => e input.merge(errors: input.fetch(:errors, []) + [e]) end |
.subtype ⇒ Dry::Struct::Sum, Dry::Struct::Class
Internal type represented by self
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# 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 ⇒ Dry::Struct::Class
Internal types represented by self
92 93 94 95 96 97 98 99 100 |
# 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 ⇒ String
45 46 47 |
# File 'lib/dry/interface.rb', line 45 def self.to_s "%s<[%s]>" % [name, subtypes.map(&:to_s).join(" | ")] end |