Class: Interactify::Dsl::EachChain
- Inherits:
-
Object
- Object
- Interactify::Dsl::EachChain
- Defined in:
- lib/interactify/dsl/each_chain.rb
Constant Summary collapse
- MissingIteratableValueInContext =
Class.new(ArgumentError)
Instance Attribute Summary collapse
-
#caller_info ⇒ Object
readonly
Returns the value of attribute caller_info.
-
#each_loop_klasses ⇒ Object
readonly
Returns the value of attribute each_loop_klasses.
-
#evaluating_receiver ⇒ Object
readonly
Returns the value of attribute evaluating_receiver.
-
#plural_resource_name ⇒ Object
readonly
Returns the value of attribute plural_resource_name.
Class Method Summary collapse
- .attach_klass(evaluating_receiver, *each_loop_klasses, plural_resource_name:, caller_info:) ⇒ Object
Instance Method Summary collapse
-
#attach_klass ⇒ Object
rubocop:enable all.
-
#initialize(each_loop_klasses, plural_resource_name, evaluating_receiver, caller_info:) ⇒ EachChain
constructor
A new instance of EachChain.
- #iterator_klass_name ⇒ Object
-
#klass ⇒ Object
allows us to dynamically create an interactor chain that iterates over the packages and uses the passed in each_loop_klasses rubocop:disable all.
- #namespace ⇒ Object
- #singular_resource_index_name ⇒ Object
- #singular_resource_name ⇒ Object
Constructor Details
#initialize(each_loop_klasses, plural_resource_name, evaluating_receiver, caller_info:) ⇒ EachChain
Returns a new instance of EachChain.
20 21 22 23 24 25 |
# File 'lib/interactify/dsl/each_chain.rb', line 20 def initialize(each_loop_klasses, plural_resource_name, evaluating_receiver, caller_info:) @each_loop_klasses = each_loop_klasses @plural_resource_name = plural_resource_name @evaluating_receiver = evaluating_receiver @caller_info = caller_info end |
Instance Attribute Details
#caller_info ⇒ Object (readonly)
Returns the value of attribute caller_info.
10 11 12 |
# File 'lib/interactify/dsl/each_chain.rb', line 10 def caller_info @caller_info end |
#each_loop_klasses ⇒ Object (readonly)
Returns the value of attribute each_loop_klasses.
10 11 12 |
# File 'lib/interactify/dsl/each_chain.rb', line 10 def each_loop_klasses @each_loop_klasses end |
#evaluating_receiver ⇒ Object (readonly)
Returns the value of attribute evaluating_receiver.
10 11 12 |
# File 'lib/interactify/dsl/each_chain.rb', line 10 def evaluating_receiver @evaluating_receiver end |
#plural_resource_name ⇒ Object (readonly)
Returns the value of attribute plural_resource_name.
10 11 12 |
# File 'lib/interactify/dsl/each_chain.rb', line 10 def plural_resource_name @plural_resource_name end |
Class Method Details
.attach_klass(evaluating_receiver, *each_loop_klasses, plural_resource_name:, caller_info:) ⇒ Object
12 13 14 15 16 17 18 |
# File 'lib/interactify/dsl/each_chain.rb', line 12 def self.attach_klass(evaluating_receiver, *each_loop_klasses, plural_resource_name:, caller_info:) iteratable = new(each_loop_klasses, plural_resource_name, evaluating_receiver, caller_info:) iteratable.attach_klass end |
Instance Method Details
#attach_klass ⇒ Object
rubocop:enable all
88 89 90 91 92 93 |
# File 'lib/interactify/dsl/each_chain.rb', line 88 def attach_klass name = iterator_klass_name namespace.const_set(name, klass) namespace.const_get(name) end |
#iterator_klass_name ⇒ Object
99 100 101 102 103 |
# File 'lib/interactify/dsl/each_chain.rb', line 99 def iterator_klass_name prefix = "Each#{singular_resource_name.to_s.camelize}" UniqueKlassName.for(namespace, prefix) end |
#klass ⇒ Object
allows us to dynamically create an interactor chain that iterates over the packages and uses the passed in each_loop_klasses rubocop:disable all
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/interactify/dsl/each_chain.rb', line 31 def klass this = self Class.new do # class SomeNamespace::EachPackage include Interactify # include Interactify expects do # expects do required(this.plural_resource_name) # required(:packages) end # end define_singleton_method(:source_location) do # def self.source_location file, line = this.caller_info&.split(':') return const_source_location(this.evaluating_receiver.to_s) if file.nil? [file, line&.to_i] end # end define_method(:run!) do # def run! resources = context.send(this.plural_resource_name) # packages = context.packages bail_with_error(resources) unless resources.respond_to?(:each_with_index) # raise MissingIteratableValueInContext unless packages.respond_to?(:each_with_index) context.send(this.plural_resource_name).each_with_index do |resource, index|# context.packages.each_with_index do |package, index| context[this.singular_resource_name] = resource # context.package = package context[this.singular_resource_index_name] = index # context.package_index = index self.class.klasses.each do |interactor| # [A, B, C].each do |interactor| interactor.call!(context) # interactor.call!(context) end # end end # end context[this.singular_resource_name] = nil # context.package = nil context[this.singular_resource_index_name] = nil # context.package_index = nil context # context end # end define_method(:bail_with_error) do |resources| = "Expected `context.#{this.plural_resource_name}`: #{resources.inspect}\nto respond to :each_with_index" raise MissingIteratableValueInContext, end define_singleton_method(:klasses) do # def self.klasses klasses = instance_variable_get(:@klasses) # @klasses ||= Wrapper.wrap_many(self, [A, B, C]) return klasses if klasses instance_variable_set(:@klasses, Wrapper.wrap_many(self, this.each_loop_klasses)) end # "<SomeNamespace::EachPackage iterates_over: [A, B, C]>" define_method(:inspect) do "<#{this.namespace}::#{this.iterator_klass_name} iterates_over: #{this.each_loop_klasses.inspect}>" end end end |
#namespace ⇒ Object
95 96 97 |
# File 'lib/interactify/dsl/each_chain.rb', line 95 def namespace evaluating_receiver end |
#singular_resource_index_name ⇒ Object
109 110 111 |
# File 'lib/interactify/dsl/each_chain.rb', line 109 def singular_resource_index_name "#{singular_resource_name}_index".to_sym end |
#singular_resource_name ⇒ Object
105 106 107 |
# File 'lib/interactify/dsl/each_chain.rb', line 105 def singular_resource_name plural_resource_name.to_s.singularize.to_sym end |