Skip to content

Commit 398f8bc

Browse files
committed
extract IfKlass
1 parent db15d60 commit 398f8bc

File tree

5 files changed

+90
-32
lines changed

5 files changed

+90
-32
lines changed

lib/interactify/dsl/each_chain.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def klass
2626
this = self
2727

2828
Class.new do # class SomeNamespace::EachPackage
29-
include Interactify # include Interactify
29+
include Interactify # include Interactify
3030

3131
expects do # expects do
3232
required(this.plural_resource_name) # required(:packages)

lib/interactify/dsl/if_interactor.rb

+3-26
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# frozen_string_literal: true
22

33
require "interactify/dsl/unique_klass_name"
4+
require "interactify/dsl/if_klass"
45

56
module Interactify
67
module Dsl
@@ -30,33 +31,9 @@ def failure_interactor
3031
# allows us to dynamically create an interactor chain
3132
# that iterates over the packages and
3233
# uses the passed in each_loop_klasses
33-
# rubocop:disable all
3434
def klass
35-
this = self
36-
37-
klass_basis.tap do |k|
38-
k.instance_eval do
39-
expects do
40-
required(this.condition) unless this.condition.is_a?(Proc)
41-
end
42-
43-
define_singleton_method(:source_location) do
44-
const_source_location this.evaluating_receiver.to_s # [file, line]
45-
end
46-
47-
define_method(:run!) do
48-
result = this.condition.is_a?(Proc) ? this.condition.call(context) : context.send(this.condition)
49-
interactor = result ? this.success_interactor : this.failure_interactor
50-
interactor&.respond_to?(:call!) ? interactor.call!(context) : interactor&.call(context)
51-
end
52-
53-
define_method(:inspect) do
54-
"<#{this.namespace}::#{this.if_klass_name} #{this.condition} ? #{this.success_interactor} : #{this.failure_interactor}>"
55-
end
56-
end
57-
end
35+
IfKlass.new(self).klass
5836
end
59-
# rubocop:enable all
6037

6138
# so we have something to attach subclasses to during building
6239
# of the outer class, before we finalize the outer If class
@@ -93,7 +70,7 @@ def build_chain(arg, truthiness)
9370

9471
case arg
9572
when Array
96-
name = "If#{condition.to_s.camelize}#{truthiness ? "IsTruthy" : "IsFalsey"}"
73+
name = "If#{condition.to_s.camelize}#{truthiness ? 'IsTruthy' : 'IsFalsey'}"
9774
klass_basis.chain(name, *arg)
9875
else
9976
arg

lib/interactify/dsl/if_klass.rb

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# frozen_string_literal: true
2+
3+
module Interactify
4+
module Dsl
5+
class IfKlass
6+
attr_reader :if_builder
7+
8+
def initialize(if_builder)
9+
@if_builder = if_builder
10+
end
11+
12+
def klass
13+
attach_expectations
14+
attach_source_location
15+
attach_run!
16+
attach_inspect
17+
18+
if_builder.klass_basis
19+
end
20+
21+
def run!(context)
22+
result = condition.is_a?(Proc) ? condition.call(context) : context.send(condition)
23+
24+
interactor = result ? success_interactor : failure_interactor
25+
interactor.respond_to?(:call!) ? interactor.call!(context) : interactor&.call(context)
26+
end
27+
28+
private
29+
30+
def attach_source_location
31+
attach do |_klass, this|
32+
define_singleton_method(:source_location) do # def self.source_location
33+
const_source_location this.evaluating_receiver.to_s # [file, line]
34+
end
35+
end
36+
end
37+
38+
def attach_expectations
39+
attach do |klass, this|
40+
klass.expects do
41+
required(this.condition) unless this.condition.is_a?(Proc)
42+
end
43+
end
44+
end
45+
46+
def attach_run!
47+
this = self
48+
49+
attach_method(:run!) do
50+
this.run!(context)
51+
end
52+
end
53+
54+
delegate :condition, :success_interactor, :failure_interactor, to: :if_builder
55+
56+
def attach_inspect
57+
this = if_builder
58+
59+
attach_method(:inspect) do
60+
name = "#{this.namespace}::#{this.if_klass_name}"
61+
"<#{name} #{this.condition} ? #{this.success_interactor} : #{this.failure_interactor}>"
62+
end
63+
end
64+
65+
def attach_method(name, &)
66+
attach do |klass, _this|
67+
klass.define_method(name, &)
68+
end
69+
end
70+
71+
def attach
72+
this = if_builder
73+
74+
this.klass_basis.instance_eval do
75+
yield self, this
76+
end
77+
end
78+
end
79+
end
80+
end

spec/lib/interactify/dsl_spec.rb

+5-4
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@
2424
)
2525
end
2626

27-
let(:on_success1) { ->(ctx) {
28-
ctx.success1 = true }
29-
}
27+
let(:on_success1) do
28+
lambda { |ctx|
29+
ctx.success1 = true
30+
}
31+
end
3032
let(:on_success2) { ->(ctx) { ctx.success2 = true } }
3133

3234
let(:on_failure1) { ->(ctx) { ctx.success1 = false } }
@@ -73,7 +75,6 @@
7375
expect(result.success2).to eq(false)
7476
end
7577
end
76-
7778
end
7879
end
7980
end

spec/lib/interactify_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
describe ".validate_app" do
99
before do
10-
wiring = instance_double(Interactify::Wiring, validate_app: 'ok')
10+
wiring = instance_double(Interactify::Wiring, validate_app: "ok")
1111

1212
expect(Interactify::Wiring)
1313
.to receive(:new)

0 commit comments

Comments
 (0)