interactor-initializer 0.3.1 → 1.0.1

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: e3ea58c9b012de5b156139019d173263e679ce434a7acd02ff519ddb9a870ff6
4
- data.tar.gz: 819740461e55ae27e97c59babe2ef3030feccb3bf28b0c022ffa627e8b994764
3
+ metadata.gz: 48ae4cdc95136e2d4634abb54b9af7ab13816df90cb147c39bc59d7948f470cf
4
+ data.tar.gz: 4361edbf7cfc0d182c3a4efa2f0a98f7ed76cbc5283976e4cf7ff1019cc67021
5
5
  SHA512:
6
- metadata.gz: 0c2e0049fa3c92b586fa67c8c0bc6c6199c92207b2b2eb312af4708866e078b86fe0b3ae804be70754f1b726423b21a8f250c273ab04c2f5c93b4513b6163996
7
- data.tar.gz: 711c376811a586fc39f01bcd65cb6c0fde86890585d205dd4e01b9835f01085ad53ffacf3c44c314966ee75e1563e5b443315471b75ea9893d317cbb176284ef
6
+ metadata.gz: 1782e56969744186ba4edd1f0f14dcbc182897c4a4e087866c01110e643e33fc4d9f16cfc2e080420584fce1da0426032ddd415a140921f0cbc4301817d68ef9
7
+ data.tar.gz: d462d0249546d8eb8b4da751bd35b223db77b38e10194a3f40912ec8464de2e979863e09450ac022cf8ea732a6e156bf6dacb5be2ec94311169bb54e467f146e
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.0.1)
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
@@ -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.3.1'.freeze
3
+ VERSION = '1.0.1'.freeze
4
4
  end
5
5
  end
@@ -1,26 +1,29 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'interactor/initializer/version'
2
- require 'interactor/initializer/attr_readers'
3
- require 'interactor/initializer/call_methods'
4
- require 'interactor/initializer/initialize'
4
+ require 'interactor/initializer/helper'
5
5
 
6
6
  module Interactor
7
7
  module Initializer
8
8
  def self.included(target_class)
9
9
  target_class.extend ClassMethods
10
- Interactor::Initializer::CallMethods.for(target_class)
11
10
  end
12
11
 
13
12
  module ClassMethods
14
13
  module_function
15
14
 
16
15
  def initialize_with(*attributes)
17
- Interactor::Initializer::Initialize.for(self, attributes)
18
- 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)
19
20
  end
20
21
 
21
22
  def initialize_with_keyword_params(*attributes)
22
- Interactor::Initializer::Initialize.for(self, attributes, keyword_params: true)
23
- 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)
24
27
  end
25
28
  end
26
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
@@ -0,0 +1,109 @@
1
+ # frozen_string_literal: true
2
+
3
+ describe Interactor::Initializer do
4
+ let(:interactor) do
5
+ Class.new do
6
+ include Interactor::Initializer
7
+
8
+ initialize_with :arg1, :arg2
9
+
10
+ def run
11
+ :result
12
+ end
13
+ end
14
+ end
15
+ let(:params) { %w[value1 value2] }
16
+
17
+ describe '.for' do
18
+ subject { interactor.for(*params) }
19
+
20
+ it { is_expected.to eq(:result) }
21
+ end
22
+
23
+ describe '.with' do
24
+ subject { interactor.with(*params) }
25
+
26
+ it { is_expected.to eq(:result) }
27
+ end
28
+
29
+ describe '.run' do
30
+ subject { interactor.run(*params) }
31
+
32
+ it { is_expected.to eq(:result) }
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
+ 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.3.1
4
+ version: 1.0.1
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-04-09 00:00:00.000000000 Z
11
+ date: 2022-03-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -63,19 +63,18 @@ 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/call_methods.rb
74
- - lib/interactor/initializer/initialize.rb
74
+ - lib/interactor/initializer/helper.rb
75
75
  - lib/interactor/initializer/version.rb
76
- - spec/initializer/attr_readers_spec.rb
77
- - spec/initializer/call_methods_spec.rb
78
- - spec/initializer/initialize_spec.rb
76
+ - spec/interactor/initializer/helper_spec.rb
77
+ - spec/interactor/initializer_spec.rb
79
78
  - spec/spec_helper.rb
80
79
  homepage: https://github.com/vinted/interactor-initializer
81
80
  licenses:
@@ -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,9 +0,0 @@
1
- class Interactor::Initializer::CallMethods
2
- METHOD_NAMES = %i(for with run).freeze
3
-
4
- def self.for(target_class)
5
- METHOD_NAMES.each do |method_name|
6
- target_class.define_singleton_method(method_name) { |*args| new(*args).run }
7
- end
8
- end
9
- 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,13 +0,0 @@
1
- describe Interactor::Initializer::CallMethods do
2
- describe '.for' do
3
- subject { described_class.for(dummy_class) }
4
- let(:dummy_class) { Class.new }
5
-
6
- it 'adds .for .with and .run class methods' do
7
- subject
8
- expect(dummy_class).to respond_to :for
9
- expect(dummy_class).to respond_to :with
10
- expect(dummy_class).to respond_to :run
11
- end
12
- end
13
- 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