Class: Plumb::Decorator

Inherits:
Object
  • Object
show all
Defined in:
lib/plumb/decorator.rb

Overview

A class to help decorate all or some types in a type composition. Example:

Type = Types::Array[Types::String | Types::Integer]
Decorated = Plumb::Decorator.(Type) do |type|
  if type.is_a?(Plumb::ArrayClass)
    LoggerType.new(type, 'array')
  else
    type
  end
end

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(block) ⇒ Decorator

Returns a new instance of Decorator.



20
21
22
# File 'lib/plumb/decorator.rb', line 20

def initialize(block)
  @block = block
end

Class Method Details

.call(type, &block) ⇒ Object



16
17
18
# File 'lib/plumb/decorator.rb', line 16

def self.call(type, &block)
  new(block).visit(type)
end

Instance Method Details

#visit(type) ⇒ Composable

Parameters:

Returns:



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/plumb/decorator.rb', line 26

def visit(type)
  type = case type
         when And
           left, right = visit_children(type)
           And.new(left, right)
         when Or
           left, right = visit_children(type)
           Or.new(left, right)
         when Not
           child = visit_children(type).first
           Not.new(child, errors: type.errors)
         when Policy
           child = visit_children(type).first
           Policy.new(type.policy_name, type.arg, child)
         else
           type
         end

  decorate(type)
end