interactor_with_steroids 0.0.1 → 1.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a01fa569ccb55a7a8c0c84cf3083e2f5a8eb43b34f1259494e9e0e2bce1efea5
4
- data.tar.gz: 78dd08e37856356eca03ec1bd1c6ebf1c371f9222fe7711bf72acc31b5ededf0
3
+ metadata.gz: 66961ad27a598bec0b93dc0297720eb97d0cdd5199b05e7e1490b731bc098de0
4
+ data.tar.gz: 6432d13df06f22d9f30c8ad4a766b80dc5f3726be2f5a80495efc3edd6b0d444
5
5
  SHA512:
6
- metadata.gz: b3087d4c955abb3b8c7f6e0baa067826e47504ad2aab8623daaffca2058ef37864e5c9bb9899693cf1cd24f6bda626fb926c91e5aea52502270a2976472c45ad
7
- data.tar.gz: c8012d42a398afd2340d5ec00cf30f7c9116bdf4be3ab7c48d4bb521a44cfd540fe206af7a3de3bf99013fcc638161a9bdc77c6a87967d13fd27acad3319570c
6
+ metadata.gz: 23295d8872c9888924deb8f6937f3030f371a74ecb6c3ee0e5d0d79a7bf2a0419aa590c1705e18b76a731bb1f1bf5893599cd677835c8666b075fb3b86766cd4
7
+ data.tar.gz: ac5eaf844aa979041a8fe09c11336f413310576d9736209083baaf27a617208f5d8cc39be9a7e2ff308d0879488171099afd4e845d4bbb6da804a61e0c62e282
data/interactor.gemspec CHANGED
@@ -2,7 +2,7 @@ require "English"
2
2
 
3
3
  Gem::Specification.new do |spec|
4
4
  spec.name = "interactor_with_steroids"
5
- spec.version = "0.0.1"
5
+ spec.version = "1.1.2"
6
6
 
7
7
  spec.author = "Collective Idea/Sorare Team"
8
8
  spec.email = "hello@sorare.com"
@@ -53,15 +53,14 @@ module Interactor
53
53
  #
54
54
  # Returns the Interactor::Context.
55
55
  def self.build(context = {})
56
- new(**context.to_h)
56
+ new.tap do |instance|
57
+ instance.assign_attributes(context.to_h)
58
+ end
57
59
  end
58
60
 
59
61
  attr_accessor :error
60
62
  delegate :to_s, to: :to_h
61
63
 
62
- def initialize(*)
63
- end
64
-
65
64
  # Public: Whether the Interactor::Context is successful. By default, a new
66
65
  # context is successful and only changes when explicitly failed.
67
66
  #
@@ -126,12 +125,18 @@ module Interactor
126
125
  # # => Interactor::Failure: #<Interactor::Context foo="baz">
127
126
  #
128
127
  # Raises Interactor::Failure initialized with the Interactor::Context.
129
- def fail!(error: nil)
130
- self.error = error
128
+ def fail!(params = {})
129
+ assign_attributes(params)
131
130
  @failure = true
132
131
  raise Failure, self
133
132
  end
134
133
 
134
+ def assign_attributes(params)
135
+ params.each do |attribute, value|
136
+ self.send("#{attribute}=", value) if respond_to?(attribute)
137
+ end
138
+ end
139
+
135
140
  def to_h
136
141
  { error: error }
137
142
  end
@@ -37,19 +37,19 @@ module Interactor
37
37
  end
38
38
 
39
39
  class_eval %Q<
40
- def initialize(
40
+ def self.build(
41
41
  #{new_required_arguments.map { |a| "#{a}:" }.join(', ')}#{new_required_arguments.empty? ? '' : ', '}
42
42
  **rest
43
43
  )
44
- super(**rest)
45
-
46
- #{new_required_arguments.map { |a| "self.#{a} = #{a}" }.join(';')}
47
-
48
- #{
49
- optional_arguments.keys.map do |k|
50
- "instance_variable_set('@#{k}', rest[:#{k}]) if rest.key?(:#{k})"
51
- end.join("\n")
52
- }
44
+ super(**rest).tap do |instance|
45
+ #{new_required_arguments.map { |a| "instance.#{a} = #{a}" }.join(';')}
46
+
47
+ #{
48
+ optional_arguments.keys.map do |k|
49
+ "instance.instance_variable_set('@#{k}', rest[:#{k}]) if rest.key?(:#{k})"
50
+ end.join("\n")
51
+ }
52
+ end
53
53
  end
54
54
  >
55
55
 
data/lib/interactor.rb CHANGED
@@ -92,7 +92,7 @@ module Interactor
92
92
  # MyInteractor.new
93
93
  # # => #<MyInteractor @context=#<Interactor::Context>>
94
94
  def initialize(context = {})
95
- @context = self.context_class.build(context)
95
+ @context = self.context_class.build(**context.to_h)
96
96
  end
97
97
 
98
98
  # Internal: Invoke an interactor instance along with all defined hooks. The
@@ -27,7 +27,7 @@ module Interactor
27
27
 
28
28
  it "doesn't affect the original hash" do
29
29
  hash = { foo: "bar" }
30
- context = interactor.context_class.build(hash)
30
+ context = interactor.context_class.build(**hash)
31
31
 
32
32
  expect(context).to be_a(interactor.context_class)
33
33
  expect {
@@ -36,6 +36,11 @@ module Interactor
36
36
  hash[:foo]
37
37
  }
38
38
  end
39
+
40
+ it "ignores any additional argument" do
41
+ hash = { foo: 'bar', bar: "baz" }
42
+ expect { interactor.context_class.build(**hash) }.not_to raise_error
43
+ end
39
44
  end
40
45
 
41
46
  describe "#success?" do
@@ -23,11 +23,11 @@ module Interactor
23
23
  }
24
24
 
25
25
  it "cannot be initialized without foo" do
26
- expect { subject.new }.to raise_error(ArgumentError)
26
+ expect { subject.build }.to raise_error(ArgumentError)
27
27
  end
28
28
 
29
29
  it "can be initialized with foo" do
30
- expect(subject.new(foo: 'bar').foo).to eq('bar')
30
+ expect(subject.build(foo: 'bar').foo).to eq('bar')
31
31
  end
32
32
 
33
33
  context 'when duplicated in a submodule' do
@@ -52,7 +52,7 @@ module Interactor
52
52
  before { stub_const("Submodule", submodule) }
53
53
 
54
54
  it "can be initialized with foo" do
55
- expect(subject.new(foo: 'bar').foo).to eq('bar')
55
+ expect(subject.build(foo: 'bar').foo).to eq('bar')
56
56
  end
57
57
  end
58
58
 
@@ -67,15 +67,15 @@ module Interactor
67
67
  }
68
68
 
69
69
  it "can be initialized without foo" do
70
- expect(subject.new.foo).to eq('bar')
70
+ expect(subject.build.foo).to eq('bar')
71
71
  end
72
72
 
73
73
  it "can be initialized with foo" do
74
- expect(subject.new(foo: 'baz').foo).to eq('baz')
74
+ expect(subject.build(foo: 'baz').foo).to eq('baz')
75
75
  end
76
76
 
77
77
  it "can be initialized with nil" do
78
- expect(subject.new(foo: nil).foo).to be nil
78
+ expect(subject.build(foo: nil).foo).to be nil
79
79
  end
80
80
  end
81
81
 
@@ -87,11 +87,11 @@ module Interactor
87
87
  }
88
88
 
89
89
  it "can be initialized without foo" do
90
- expect(subject.new.foo).to be nil
90
+ expect(subject.build.foo).to be nil
91
91
  end
92
92
 
93
93
  it "can be initialized with foo" do
94
- expect(subject.new(foo: 'baz').foo).to eq('baz')
94
+ expect(subject.build(foo: 'baz').foo).to eq('baz')
95
95
  end
96
96
  end
97
97
 
@@ -103,14 +103,28 @@ module Interactor
103
103
  }
104
104
 
105
105
  it "can be initialized without foo" do
106
- expect(subject.new(bar: 'bar').foo).to eq('bar')
106
+ expect(subject.build(bar: 'bar').foo).to eq('bar')
107
107
  end
108
108
 
109
109
  it "can be initialized with foo" do
110
- expect(subject.new(bar: 'bar', foo: 'baz').foo).to eq('baz')
110
+ expect(subject.build(bar: 'bar', foo: 'baz').foo).to eq('baz')
111
111
  end
112
112
  end
113
113
  end
114
114
  end
115
+
116
+ describe "#hold" do
117
+ let(:declared) {
118
+ build_declared do
119
+ hold :foo
120
+ end
121
+ }
122
+
123
+ it 'can hold foo' do
124
+ c = subject.build
125
+ c.foo = 'bar'
126
+ expect(c.foo).to eq('bar')
127
+ end
128
+ end
115
129
  end
116
130
  end
@@ -52,7 +52,7 @@ describe Interactor do
52
52
  end
53
53
 
54
54
  it "initializes a blank context if none is given" do
55
- expect(Interactor::Context).to receive(:build).once.with({}) { context }
55
+ expect(Interactor::Context).to receive(:build).once.with(no_args) { context }
56
56
 
57
57
  instance = interactor.new
58
58
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: interactor_with_steroids
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 1.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Collective Idea/Sorare Team
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-01-09 00:00:00.000000000 Z
11
+ date: 2022-01-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -100,7 +100,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
100
100
  - !ruby/object:Gem::Version
101
101
  version: '0'
102
102
  requirements: []
103
- rubygems_version: 3.1.6
103
+ rubygems_version: 3.3.3
104
104
  signing_key:
105
105
  specification_version: 4
106
106
  summary: Simple interactor implementation