Module: Interactify::Dsl

Defined in:
lib/interactify/dsl.rb,
lib/interactify/dsl/wrapper.rb,
lib/interactify/dsl/if_klass.rb,
lib/interactify/dsl/organizer.rb,
lib/interactify/dsl/each_chain.rb,
lib/interactify/dsl/if_interactor.rb,
lib/interactify/dsl/unique_klass_name.rb

Defined Under Namespace

Modules: Organizer, UniqueKlassName Classes: EachChain, IfInteractor, IfKlass, Wrapper

Constant Summary collapse

Error =
Class.new(::ArgumentError)
IfDefinitionUnexpectedKey =
Class.new(Error)

Instance Method Summary collapse

Instance Method Details

#chain(klass_name, *chained_klasses, expect: [], caller_info: nil) ⇒ Object

this method allows us to dynamically create an organizer from a name, and a chain of interactors

e.g.

Interactify.chain(:SomeClass, A, B, C, expect: [:foo, :bar])

is the programmable equivalent to

class SomeClass

include Interactify
organize(A, B, C)

end

it will attach the generate class to the currenct class and use the class name passed in rubocop:disable all



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

def chain(klass_name, *chained_klasses, expect: [], caller_info: nil)
  caller_info ||= caller(1..1).first
  expectations = expect

  klass = Class.new do                             # class EvaluatingNamespace::SomeClass
    include Interactify                            #   include Interactify
    expect(*expectations) if expectations.any?     #   expect :foo, :bar

    define_singleton_method(:source_location) do   #   def self.source_location
      caller_info                                  #     [file, line]
    end                                            #   end

    organize(*chained_klasses)                     #   organize(A, B, C)
  end                                              # end

  # attach the class to the calling namespace
  where_to_attach = self.binding.receiver
  klass_name = UniqueKlassName.for(where_to_attach, klass_name)
  where_to_attach.const_set(klass_name, klass)
end

#each(plural_resource_name, *each_loop_klasses) ⇒ Object

creates a class in the attach_klass_to’s namespace e.g.

in Orders Interactify.each(self, :packages, A, B, C)

will create a class called Orders::EachPackage, that will call the interactor chain A, B, C for each package in the context



20
21
22
23
24
25
26
27
28
29
# File 'lib/interactify/dsl.rb', line 20

def each(plural_resource_name, *each_loop_klasses)
  caller_info = caller(1..1).first

  EachChain.attach_klass(
    self,
    *each_loop_klasses,
    caller_info:,
    plural_resource_name:
  )
end

#if(condition, success_arg, failure_arg = nil) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/interactify/dsl.rb', line 31

def if(condition, success_arg, failure_arg = nil)
  then_else = parse_if_args(condition, success_arg, failure_arg)

  caller_info = caller(1..1).first

  IfInteractor.attach_klass(
    self,
    condition,
    then_else[:then],
    then_else[:else],
    caller_info:
  )
end