Class: Interactify::Dsl::Wrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/interactify/dsl/wrapper.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(organizer, interactor) ⇒ Wrapper

Returns a new instance of Wrapper.



20
21
22
23
# File 'lib/interactify/dsl/wrapper.rb', line 20

def initialize(organizer, interactor)
  @organizer = organizer
  @interactor = interactor
end

Instance Attribute Details

#interactorObject (readonly)

Returns the value of attribute interactor.



8
9
10
# File 'lib/interactify/dsl/wrapper.rb', line 8

def interactor
  @interactor
end

#organizerObject (readonly)

Returns the value of attribute organizer.



8
9
10
# File 'lib/interactify/dsl/wrapper.rb', line 8

def organizer
  @organizer
end

Class Method Details

.wrap(organizer, interactor) ⇒ Object



16
17
18
# File 'lib/interactify/dsl/wrapper.rb', line 16

def self.wrap(organizer, interactor)
  new(organizer, interactor).wrap
end

.wrap_many(organizer, interactors) ⇒ Object



10
11
12
13
14
# File 'lib/interactify/dsl/wrapper.rb', line 10

def self.wrap_many(organizer, interactors)
  Array(interactors).map do |interactor|
    wrap(organizer, interactor)
  end
end

Instance Method Details

#conditionObject



59
# File 'lib/interactify/dsl/wrapper.rb', line 59

def condition = interactor[:if]

#else_doObject



61
# File 'lib/interactify/dsl/wrapper.rb', line 61

def else_do = interactor[:else]

#then_doObject



60
# File 'lib/interactify/dsl/wrapper.rb', line 60

def then_do = interactor[:then]

#wrapObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/interactify/dsl/wrapper.rb', line 25

def wrap
  case interactor
  when Hash
    wrap_conditional
  when Array
    wrap_chain
  when Proc
    wrap_proc
  when Class
    return interactor if interactor < Interactor

    raise ArgumentError, "#{interactor} must respond_to .call" unless interactor.respond_to?(:call)

    wrap_proc
  else
    interactor
  end
end

#wrap_chainObject



44
45
46
47
48
49
# File 'lib/interactify/dsl/wrapper.rb', line 44

def wrap_chain
  return self.class.wrap(organizer, interactor.first) if interactor.length == 1

  klass_name = UniqueKlassName.for(organizer, "Chained")
  organizer.chain(klass_name, *interactor.map { self.class.wrap(organizer, _1) })
end

#wrap_conditionalObject

Raises:

  • (ArgumentError)


51
52
53
54
55
56
57
# File 'lib/interactify/dsl/wrapper.rb', line 51

def wrap_conditional
  raise ArgumentError, "Hash must have at least :if, and :then key" unless condition && then_do

  return organizer.if(condition, then_do, else_do) if else_do

  organizer.if(condition, then_do)
end

#wrap_procObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/interactify/dsl/wrapper.rb', line 63

def wrap_proc
  this = self

  Class.new do
    include Interactify

    define_singleton_method :wrapped do
      this.interactor
    end

    define_method(:call) do
      this.interactor.call(context)
    end
  end
end