interactor-initializer 0.4.0 → 1.0

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: 5af4b74086ee54c325a0978227a41cba1449b7369ae4d77d8144cb93e4392a36
4
- data.tar.gz: cc5551943cffcf530daf69fe18c29bee0fcb46a4c07cb411bb13074a5f74772d
3
+ metadata.gz: 2a7524207f17039e7eda3b4e2ceb0a3f406c70da99c7776be2a3d042dd6dc474
4
+ data.tar.gz: 23377afbee95423b6cab13e9d58137c1319e24bcefb7754b32a225308b28c09f
5
5
  SHA512:
6
- metadata.gz: f7cf6987a066b3649813d0c38da664c6c6f49aac8541e34055c636e5f182c03c30a3d55987cce42b36c299bc8b49003e92a3a52780e9ea8dec45a7fb9fc2bc9b
7
- data.tar.gz: 5ee5a3989601d9c70eec1245c1fab592ff78b45aa4a2efe614f5036382bbcf4986e692581dda139470f29ee1d205e8c5f9c29b8be46ea363fdaabed156b9c6f9
6
+ metadata.gz: 9533cbdd913a94086ec11affcb130704ac4f7603468aa5881f2dd1c0c2aa87132dda21e13f2ceedd8fccf993d006cf6fd8af0d89b34331ae691e467b4201b8ba
7
+ data.tar.gz: 26f239fb44ac1a598cfe05eff32a0ba53ff8ac25c4dd1b06398ace36e38613e4d239c0fa800fa02d74158912d835639b51399d0a8b2687deb4b0db1226a860d1
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 3.0.2
@@ -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.0'.freeze
4
4
  end
5
5
  end
@@ -1,6 +1,7 @@
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
@@ -9,28 +10,20 @@ module Interactor
9
10
  end
10
11
 
11
12
  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
13
  module_function
25
14
 
26
15
  def initialize_with(*attributes)
27
- Interactor::Initializer::Initialize.for(self, attributes)
28
- Interactor::Initializer::AttrReaders.for(self, attributes)
16
+ signature = attributes.join(', ')
17
+ class_methods = Interactor::Initializer::Helper.methods_with_params
18
+
19
+ Interactor::Initializer::Helper.modify_class(self, signature, attributes, class_methods)
29
20
  end
30
21
 
31
22
  def initialize_with_keyword_params(*attributes)
32
- Interactor::Initializer::Initialize.for(self, attributes, keyword_params: true)
33
- Interactor::Initializer::AttrReaders.for(self, attributes)
23
+ signature = attributes.map { |attr| "#{attr}:" }.join(', ')
24
+ class_methods = Interactor::Initializer::Helper.methods_with_keywords
25
+
26
+ Interactor::Initializer::Helper.modify_class(self, signature, attributes, class_methods)
34
27
  end
35
28
  end
36
29
  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,109 @@
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
29
109
  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.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-01-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -63,17 +63,16 @@ files:
63
63
  - ".gitignore"
64
64
  - ".rspec"
65
65
  - ".rubocop.yml"
66
+ - ".ruby-version"
66
67
  - Gemfile
67
68
  - LICENSE
68
69
  - README.md
69
70
  - Rakefile
70
71
  - interactor-initializer.gemspec
71
72
  - lib/interactor/initializer.rb
72
- - lib/interactor/initializer/attr_readers.rb
73
- - lib/interactor/initializer/initialize.rb
73
+ - lib/interactor/initializer/helper.rb
74
74
  - lib/interactor/initializer/version.rb
75
- - spec/interactor/initializer/attr_readers_spec.rb
76
- - spec/interactor/initializer/initialize_spec.rb
75
+ - spec/interactor/initializer/helper_spec.rb
77
76
  - spec/interactor/initializer_spec.rb
78
77
  - spec/spec_helper.rb
79
78
  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