Skip to content

Commit

Permalink
ensure we correctly wrap nested classes
Browse files Browse the repository at this point in the history
  • Loading branch information
markburns committed Dec 30, 2023
1 parent c34cfa1 commit 55ac5e0
Show file tree
Hide file tree
Showing 17 changed files with 388 additions and 3 deletions.
12 changes: 9 additions & 3 deletions lib/interactify/dsl/each_chain.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ def klass
context[this.singular_resource_name] = resource # context.package = package
context[this.singular_resource_index_name] = index # context.package_index = index

klasses = Wrapper.wrap_many(self, this.each_loop_klasses)

klasses.each do |interactor| # [A, B, C].each do |interactor|
self.class.klasses.each do |interactor| # [A, B, C].each do |interactor|
interactor.call!(context) # interactor.call!(context)
end # end
end # end
Expand All @@ -54,6 +52,14 @@ def klass
context # context
end # 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
Expand Down
Empty file added spec/fixtures/asdf
Empty file.
59 changes: 59 additions & 0 deletions spec/fixtures/integration_app/app/interactors/all_the_things.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
class AllTheThings
include Interactify

expect :things
optional :optional_thing
promise :a

organize \
self.if(
:things,
then: [If::A, If::B],
else: [If::C, If::D]
),

self.each(
:things,
If::A,
If::B,
-> (c) { c.lambda_set = true }
),

self.if(
-> (c) { c.a && c.b } ,
then: -> (c) { c.both_a_and_b = true },
else: -> (c) { c.both_a_and_b = false}
),

-> (c) { c.more_things = [1, 2, 3, 4] },

self.each(
:more_things,
-> (c) {
if c.more_thing_index.zero?
c.first_more_thing = true
end
},
-> (c) {
if c.more_thing_index == 1
c.next_more_thing = true
end
},
{if: :not_set_thing, then: -> (c) { c.not_set_thing = true } },
-> (c) { c.more_thing = true }
),

self.if(
:optional_thing,
then: [
-> (c) { c.optional_thing_was_set = true },
-> (c) { c.and_then_another_thing = true },
-> (c) { c.and_one_more_thing = true },
self.each(:more_things,
-> (c) { c.more_things[c.more_thing_index] = c.more_thing + 5 },
-> (c) { c.more_things[c.more_thing_index] = c.more_thing + 5 }
)
],
else: -> (c) { c.optional_thing_was_set = false }
)
end
13 changes: 13 additions & 0 deletions spec/fixtures/integration_app/app/interactors/each/a.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Each
class A
include Interactify

def call
context.a = 'a'
return unless context.thing

context.thing.a = 'a'
context.thing.a_index = context.thing_index
end
end
end
13 changes: 13 additions & 0 deletions spec/fixtures/integration_app/app/interactors/each/b.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Each
class B
include Interactify

def call
context.b = 'b'
return unless context.thing

context.thing.b = 'b'
context.thing.b_index = context.thing_index
end
end
end
13 changes: 13 additions & 0 deletions spec/fixtures/integration_app/app/interactors/each/c.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Each
class C
include Interactify

def call
context.c = 'c'
return unless context.thing

context.thing.c = 'c'
context.thing.c_index = context.thing_index
end
end
end
13 changes: 13 additions & 0 deletions spec/fixtures/integration_app/app/interactors/each/d.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Each
class D
include Interactify

def call
context.d = 'd'
return unless context.thing

context.thing.d = 'd'
context.thing.d_index = context.thing_index
end
end
end
10 changes: 10 additions & 0 deletions spec/fixtures/integration_app/app/interactors/each/organizer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module Each
class Organizer
include Interactify
expect :things

organize \
A, B, C, D,
self.each(:things, A, B, C, D)
end
end
13 changes: 13 additions & 0 deletions spec/fixtures/integration_app/app/interactors/if/a.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module If
class A
include Interactify

def call
context.a = 'a'
return unless context.thing

context.thing.a = 'a'
context.thing.a_index = context.thing_index
end
end
end
13 changes: 13 additions & 0 deletions spec/fixtures/integration_app/app/interactors/if/b.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module If
class B
include Interactify

def call
context.b = 'b'
return unless context.thing

context.thing.b = 'b'
context.thing.b_index = context.thing_index
end
end
end
13 changes: 13 additions & 0 deletions spec/fixtures/integration_app/app/interactors/if/c.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module If
class C
include Interactify

def call
context.c = 'c'
return unless context.thing

context.thing.c = 'c'
context.thing.c_index = context.thing_index
end
end
end
13 changes: 13 additions & 0 deletions spec/fixtures/integration_app/app/interactors/if/d.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module If
class D
include Interactify

def call
context.d = 'd'
return unless context.thing

context.thing.d = 'd'
context.thing.d_index = context.thing_index
end
end
end
40 changes: 40 additions & 0 deletions spec/fixtures/integration_app/app/interactors/if/organizer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
module If
class Anyways
include Interactify

expect :blah, filled: false

def call
context.anyways = blah
end
end

class MethodSyntaxOrganizer
include Interactify
expect :blah, filled: false

organize \
self.if(:blah, [A, B], [C, D]),
Anyways
end

class AlternativeMethodSyntaxOrganizer
include Interactify
expect :blah, filled: false

organize(
self.if(:blah, then: [A, B], else: [C, D]),
Anyways
)
end

class HashSyntaxOrganizer
include Interactify
expect :blah, filled: false

organize(
{if: :blah, then: [A, B], else: [C, D]},
Anyways
)
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

63 changes: 63 additions & 0 deletions spec/integration/all_the_things_integration_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# frozen_string_literal: true

RSpec.describe "Interactify" do
let(:things) do
[thing1, thing2]
end

let(:thing1) do
OpenStruct.new
end

let(:thing2) do
OpenStruct.new
end

before do
require_files("each/")
require_files("if/")
require_files('')
end

context 'without an optional thing' do
let(:result) { AllTheThings.promising(:a).call!(things:, optional_thing: false) }

it "sets A and B, then lambda_set, then both_a_and_b, then first_more_thing, next_more_thing" do
expect(result.a).to eq("a")
expect(result.b).to eq("b")
expect(result.c).to eq(nil)
expect(result.d).to eq(nil)
expect(result.lambda_set).to eq(true)
expect(result.both_a_and_b).to eq(true)
expect(result.more_things).to eq([1, 2, 3, 4])
expect(result.first_more_thing).to eq(true)
expect(result.next_more_thing).to eq(true)
expect(result.optional_thing_was_set).to eq(false)
end
end

context "with an optional thing" do
let(:result) { AllTheThings.promising(:a).call!(things:, optional_thing: true) }

it "sets A and B, then lambda_set, then both_a_and_b, then first_more_thing, next_more_thing" do
expect(result.a).to eq("a")
expect(result.b).to eq("b")
expect(result.c).to eq(nil)
expect(result.d).to eq(nil)
expect(result.lambda_set).to eq(true)
expect(result.both_a_and_b).to eq(true)
expect(result.more_things).to eq([6, 7, 8, 9])
expect(result.first_more_thing).to eq(true)
expect(result.next_more_thing).to eq(true)
expect(result.optional_thing_was_set).to eq(true)
end
end

def require_files(dir)
files = Dir.glob("./spec/fixtures/integration_app/app/interactors/#{dir}**/*.rb")

files.each do |file|
require file
end
end
end
27 changes: 27 additions & 0 deletions spec/integration/each_integration_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true

RSpec.describe "Interactify.each" do
before do
files = Dir.glob("./spec/fixtures/integration_app/app/interactors/each/**/*.rb")
files.each do |file|
require file
end
end

let(:thing_1) do
OpenStruct.new
end

let(:thing_2) do
OpenStruct.new
end

it "runs the outer interactors" do
result = Each::Organizer.call!(things: [thing_1, thing_2])

expect(result.a).to eq("a")
expect(result.b).to eq("b")
expect(result.c).to eq("c")
expect(result.d).to eq("d")
end
end
Loading

0 comments on commit 55ac5e0

Please sign in to comment.