interactor-initializer 0.4.0 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5af4b74086ee54c325a0978227a41cba1449b7369ae4d77d8144cb93e4392a36
4
- data.tar.gz: cc5551943cffcf530daf69fe18c29bee0fcb46a4c07cb411bb13074a5f74772d
3
+ metadata.gz: 5281abc2705c0b4a4b4df43058e28534c06b5e340dc4e6a246ef0f1558aa6927
4
+ data.tar.gz: be879db3848729be75fb39497950bb290ff5aa20a42471b1c47d2400a301374b
5
5
  SHA512:
6
- metadata.gz: f7cf6987a066b3649813d0c38da664c6c6f49aac8541e34055c636e5f182c03c30a3d55987cce42b36c299bc8b49003e92a3a52780e9ea8dec45a7fb9fc2bc9b
7
- data.tar.gz: 5ee5a3989601d9c70eec1245c1fab592ff78b45aa4a2efe614f5036382bbcf4986e692581dda139470f29ee1d205e8c5f9c29b8be46ea363fdaabed156b9c6f9
6
+ metadata.gz: 9796a8c8e98ede7c50f0c126e20f06a28d1b797b37868275fcdd82bad66636dc7320f487b3165250fd27d6482189442c629dd46a09dc44e28aa238c6ed7fd850
7
+ data.tar.gz: 6a4fed719d00690a75c603dd089a7e438eb4d3117479f60a3bd40912b7caa2bd6978e3a8c2ed0c9f6c3f1752bc901e24bbcf9492b4fac859e2d67a911f947737
data/.gitignore CHANGED
@@ -1,6 +1,5 @@
1
1
  /.bundle/
2
2
  /.yardoc
3
- /Gemfile.lock
4
3
  /_yardoc/
5
4
  /coverage/
6
5
  /doc/
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 3.0.3
data/Gemfile.lock ADDED
@@ -0,0 +1,35 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ interactor-initializer (1.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.5.0)
10
+ rake (10.5.0)
11
+ rspec (3.11.0)
12
+ rspec-core (~> 3.11.0)
13
+ rspec-expectations (~> 3.11.0)
14
+ rspec-mocks (~> 3.11.0)
15
+ rspec-core (3.11.0)
16
+ rspec-support (~> 3.11.0)
17
+ rspec-expectations (3.11.0)
18
+ diff-lcs (>= 1.2.0, < 2.0)
19
+ rspec-support (~> 3.11.0)
20
+ rspec-mocks (3.11.0)
21
+ diff-lcs (>= 1.2.0, < 2.0)
22
+ rspec-support (~> 3.11.0)
23
+ rspec-support (3.11.0)
24
+
25
+ PLATFORMS
26
+ ruby
27
+
28
+ DEPENDENCIES
29
+ bundler
30
+ interactor-initializer!
31
+ rake (~> 10.0)
32
+ rspec (~> 3.0)
33
+
34
+ BUNDLED WITH
35
+ 2.2.32
@@ -1,4 +1,5 @@
1
1
  # coding: utf-8
2
+
2
3
  lib = File.expand_path('../lib', __FILE__)
3
4
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
5
  require 'interactor/initializer/version'
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Interactor::Initializer::Helper
4
+ def self.modify_class(target_class, signature, attributes, class_methods)
5
+ assignments = attr_assignments(attributes)
6
+
7
+ target_class.class_eval <<-RUBY, __FILE__, __LINE__ + 1
8
+ def initialize(#{signature})
9
+ #{assignments}
10
+ end
11
+
12
+ #{class_methods}
13
+ RUBY
14
+
15
+ attr_readers(target_class, attributes)
16
+ end
17
+
18
+ def self.attr_readers(target_class, attributes)
19
+ target_class.class_eval do
20
+ attributes.each do |attr|
21
+ attr_reader(attr)
22
+ protected(attr)
23
+ end
24
+ end
25
+ end
26
+
27
+ def self.methods_with_params
28
+ <<-RUBY
29
+ def self.for(*args)
30
+ new(*args).run
31
+ end
32
+
33
+ def self.run(*args)
34
+ new(*args).run
35
+ end
36
+
37
+ def self.with(*args)
38
+ new(*args).run
39
+ end
40
+ RUBY
41
+ end
42
+
43
+ def self.methods_with_keywords
44
+ <<-RUBY
45
+ def self.for(args)
46
+ new(**args).run
47
+ end
48
+
49
+ def self.run(args)
50
+ new(**args).run
51
+ end
52
+
53
+ def self.with(args)
54
+ new(**args).run
55
+ end
56
+ RUBY
57
+ end
58
+
59
+ def self.attr_assignments(attributes)
60
+ attributes.map { |attr| "@#{attr} = #{attr}" }.join(';')
61
+ end
62
+ end
@@ -1,5 +1,5 @@
1
1
  module Interactor
2
2
  module Initializer
3
- VERSION = '0.4.0'.freeze
3
+ VERSION = '1.1.0'.freeze
4
4
  end
5
5
  end
@@ -1,36 +1,32 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'interactor/initializer/version'
2
- require 'interactor/initializer/attr_readers'
3
- require 'interactor/initializer/initialize'
4
+ require 'interactor/initializer/helper'
4
5
 
5
6
  module Interactor
6
7
  module Initializer
7
8
  def self.included(target_class)
8
9
  target_class.extend ClassMethods
10
+
11
+ class_methods = Interactor::Initializer::Helper.methods_with_params
12
+ Interactor::Initializer::Helper.modify_class(target_class, '', [], class_methods)
9
13
  end
10
14
 
11
15
  module ClassMethods
12
- def for(*args)
13
- new(*args).run
14
- end
15
-
16
- def with(*args)
17
- new(*args).run
18
- end
19
-
20
- def run(*args)
21
- new(*args).run
22
- end
23
-
24
16
  module_function
25
17
 
26
18
  def initialize_with(*attributes)
27
- Interactor::Initializer::Initialize.for(self, attributes)
28
- Interactor::Initializer::AttrReaders.for(self, attributes)
19
+ signature = attributes.join(', ')
20
+ class_methods = Interactor::Initializer::Helper.methods_with_params
21
+
22
+ Interactor::Initializer::Helper.modify_class(self, signature, attributes, class_methods)
29
23
  end
30
24
 
31
25
  def initialize_with_keyword_params(*attributes)
32
- Interactor::Initializer::Initialize.for(self, attributes, keyword_params: true)
33
- Interactor::Initializer::AttrReaders.for(self, attributes)
26
+ signature = attributes.map { |attr| "#{attr}:" }.join(', ')
27
+ class_methods = Interactor::Initializer::Helper.methods_with_keywords
28
+
29
+ Interactor::Initializer::Helper.modify_class(self, signature, attributes, class_methods)
34
30
  end
35
31
  end
36
32
  end
@@ -1,8 +1,11 @@
1
- describe Interactor::Initializer::AttrReaders do
2
- describe '.for' do
3
- subject { described_class.for(dummy_class, attributes) }
1
+ # frozen_string_literal: true
2
+
3
+ describe Interactor::Initializer::Helper do
4
+ describe '.attr_readers' do
5
+ subject { described_class.attr_readers(dummy_class, attributes) }
6
+
4
7
  let(:dummy_class) { Class.new }
5
- let(:attributes) { [:test1, :test2] }
8
+ let(:attributes) { %i[test1 test2] }
6
9
 
7
10
  it 'adds protected attr_reader' do
8
11
  subject
@@ -1,29 +1,157 @@
1
+ # frozen_string_literal: true
2
+
1
3
  describe Interactor::Initializer do
2
4
  let(:interactor) do
3
5
  Class.new do
4
6
  include Interactor::Initializer
5
7
 
8
+ initialize_with :arg1, :arg2
9
+
6
10
  def run
7
11
  :result
8
12
  end
9
13
  end
10
14
  end
15
+ let(:params) { %w[value1 value2] }
11
16
 
12
17
  describe '.for' do
13
- subject { interactor.for }
18
+ subject { interactor.for(*params) }
14
19
 
15
20
  it { is_expected.to eq(:result) }
16
21
  end
17
22
 
18
23
  describe '.with' do
19
- subject { interactor.with }
24
+ subject { interactor.with(*params) }
20
25
 
21
26
  it { is_expected.to eq(:result) }
22
27
  end
23
28
 
24
29
  describe '.run' do
25
- subject { interactor.run }
30
+ subject { interactor.run(*params) }
26
31
 
27
32
  it { is_expected.to eq(:result) }
28
33
  end
34
+
35
+ describe '.new' do
36
+ subject { interactor.new(*params) }
37
+
38
+ it 'adds instance variables' do
39
+ expect(subject.instance_variable_get(:@arg1)).to eq 'value1'
40
+ expect(subject.instance_variable_get(:@arg2)).to eq 'value2'
41
+ end
42
+ end
43
+
44
+ context 'when incorrect number of params' do
45
+ subject { interactor.for(*params) }
46
+
47
+ let(:params) { %w[value1] }
48
+
49
+ it 'raises an error' do
50
+ expect { subject }.to raise_error(/wrong number of arguments/)
51
+ end
52
+ end
53
+
54
+ context 'when initialization with keywords' do
55
+ let(:interactor) do
56
+ Class.new do
57
+ include Interactor::Initializer
58
+
59
+ initialize_with_keyword_params :arg1, :arg2
60
+
61
+ def run
62
+ :result
63
+ end
64
+ end
65
+ end
66
+ let(:params) do
67
+ { arg1: 'value1', arg2: 'value2' }
68
+ end
69
+
70
+ describe '.for' do
71
+ subject { interactor.for(params) }
72
+
73
+ it { is_expected.to eq(:result) }
74
+ end
75
+
76
+ describe '.with' do
77
+ subject { interactor.with(params) }
78
+
79
+ it { is_expected.to eq(:result) }
80
+ end
81
+
82
+ describe '.run' do
83
+ subject { interactor.run(params) }
84
+
85
+ it { is_expected.to eq(:result) }
86
+ end
87
+
88
+ describe '.new' do
89
+ subject { interactor.new(**params) }
90
+
91
+ it 'adds instance variables' do
92
+ expect(subject.instance_variable_get(:@arg1)).to eq 'value1'
93
+ expect(subject.instance_variable_get(:@arg2)).to eq 'value2'
94
+ end
95
+ end
96
+
97
+ context 'when incorrect keywords' do
98
+ subject { interactor.for(params) }
99
+
100
+ let(:params) do
101
+ { arg1: 'value2', arg4: 'value2' }
102
+ end
103
+
104
+ it 'raises an error' do
105
+ expect { subject }.to raise_error(/missing keyword: :arg2/)
106
+ end
107
+ end
108
+ end
109
+
110
+ context 'with bare inclusion' do
111
+ let(:interactor) do
112
+ Class.new do
113
+ include Interactor::Initializer
114
+
115
+ def run
116
+ :result
117
+ end
118
+ end
119
+ end
120
+
121
+ describe '.run' do
122
+ subject { interactor.run }
123
+
124
+ it { is_expected.to eq(:result) }
125
+ end
126
+
127
+ describe '.for' do
128
+ subject { interactor.for }
129
+
130
+ it { is_expected.to eq(:result) }
131
+ end
132
+
133
+ describe '.with' do
134
+ subject { interactor.with }
135
+
136
+ it { is_expected.to eq(:result) }
137
+ end
138
+
139
+ context 'when class is not including interactor initializer' do
140
+ let(:interactor) do
141
+ Class.new do
142
+ def run
143
+ :result
144
+ end
145
+ end
146
+ end
147
+
148
+ describe '.run' do
149
+ subject { interactor.run }
150
+
151
+ it 'cannot call interactor' do
152
+ expect { subject }.to raise_error(/undefined method/)
153
+ end
154
+ end
155
+ end
156
+ end
29
157
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: interactor-initializer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Šarūnas Kūjalis
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-06-21 00:00:00.000000000 Z
11
+ date: 2022-04-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -63,17 +63,17 @@ files:
63
63
  - ".gitignore"
64
64
  - ".rspec"
65
65
  - ".rubocop.yml"
66
+ - ".ruby-version"
66
67
  - Gemfile
68
+ - Gemfile.lock
67
69
  - LICENSE
68
70
  - README.md
69
71
  - Rakefile
70
72
  - interactor-initializer.gemspec
71
73
  - lib/interactor/initializer.rb
72
- - lib/interactor/initializer/attr_readers.rb
73
- - lib/interactor/initializer/initialize.rb
74
+ - lib/interactor/initializer/helper.rb
74
75
  - lib/interactor/initializer/version.rb
75
- - spec/interactor/initializer/attr_readers_spec.rb
76
- - spec/interactor/initializer/initialize_spec.rb
76
+ - spec/interactor/initializer/helper_spec.rb
77
77
  - spec/interactor/initializer_spec.rb
78
78
  - spec/spec_helper.rb
79
79
  homepage: https://github.com/vinted/interactor-initializer
@@ -1,10 +0,0 @@
1
- class Interactor::Initializer::AttrReaders
2
- def self.for(target_class, attributes)
3
- target_class.class_eval do
4
- attributes.each do |attribute|
5
- attr_reader(attribute)
6
- protected(attribute)
7
- end
8
- end
9
- end
10
- end
@@ -1,35 +0,0 @@
1
- class Interactor::Initializer::Initialize
2
- def self.for(*args, **kwargs)
3
- new(*args, **kwargs).run
4
- end
5
-
6
- attr_reader :target_class, :attributes, :keyword_params
7
-
8
- def initialize(target_class, attributes, keyword_params: false)
9
- @target_class = target_class
10
- @attributes = attributes
11
- @keyword_params = keyword_params
12
- end
13
-
14
- def run
15
- target_class.class_eval <<-RUBY
16
- def initialize(#{initializer_signature(attributes)})
17
- #{attr_assignments(attributes)}
18
- end
19
- RUBY
20
- end
21
-
22
- private
23
-
24
- def initializer_signature(attributes)
25
- return attributes.join(', ') unless keyword_params
26
-
27
- attributes.map { |attribute| "#{attribute}:" }.join(', ')
28
- end
29
-
30
- def attr_assignments(attributes)
31
- attributes.inject('') do |assignments, attribute|
32
- "#{assignments}@#{attribute} = #{attribute};"
33
- end
34
- end
35
- end
@@ -1,28 +0,0 @@
1
- describe Interactor::Initializer::Initialize do
2
- describe '.for' do
3
- subject { described_class.for(dummy_class, attributes, keyword_params: keyword_params) }
4
- let(:dummy_class) { Class.new }
5
- let(:attributes) { [:test1, :test2] }
6
- let(:keyword_params) { false }
7
-
8
- shared_examples 'object initializer' do
9
- it 'adds object initializer' do
10
- subject
11
- expect(dummy_instance.instance_variable_get(:@test1)).to eq 1
12
- expect(dummy_instance.instance_variable_get(:@test2)).to eq 2
13
- end
14
- end
15
-
16
- it_behaves_like 'object initializer' do
17
- let(:dummy_instance) { dummy_class.new(1, 2) }
18
- end
19
-
20
- context 'when keyword params' do
21
- let(:keyword_params) { true }
22
-
23
- it_behaves_like 'object initializer' do
24
- let(:dummy_instance) { dummy_class.new(test1: 1, test2: 2) }
25
- end
26
- end
27
- end
28
- end