smart_init 4.0.1 → 5.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/LICENSE.txt +2 -2
- data/README.md +70 -19
- data/lib/smart_init.rb +2 -0
- data/lib/smart_init/main.rb +23 -55
- data/lib/smart_init/version.rb +3 -1
- data/test/test_hash_api.rb +2 -0
- data/test/test_hash_public_accessors.rb +74 -0
- data/test/test_hash_public_mixed.rb +83 -0
- data/test/test_hash_public_readers.rb +21 -0
- metadata +9 -7
- data/test/test_args_api.rb +0 -77
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e98d279fac333ac355c8d854d724c567151dd9798d11cf248f50e75edc1ebdc0
|
4
|
+
data.tar.gz: 517a005cd94b3841e6f212c4368034c220aff25c1e73410e884445397bc5da34
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 98a3581a564a6ffc532c52540c4942d2b103b3d0bec9d5511e40e0fc57c6dd775bfb79ddcfa9d2e3acf0616ab8b4c724fc50ce173658cc63d14aff230667d3b0
|
7
|
+
data.tar.gz: f789ca9f6b424fc6b77f519ef90564d08ac85eb937ac11e77856cb2a41c1f41f12fc825f450d114ef8bf8d7c24f6dacd92ba9cbbad5e5c7944022a05ecc868c1
|
data/LICENSE.txt
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
Copyright (c)
|
1
|
+
Copyright (c) 2020 Paweł Urbanek
|
2
2
|
|
3
3
|
MIT License
|
4
4
|
|
@@ -19,4 +19,4 @@ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
19
19
|
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
20
|
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
21
|
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
-
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
|
-
# Smart Init [![Build Status](https://travis-ci.org/pawurb/smart_init.svg)](https://travis-ci.org/pawurb/smart_init) [![Gem Version](https://badge.fury.io/rb/smart_init.svg)](https://badge.fury.io/rb/smart_init)
|
1
|
+
# Smart Init - Simple service objects in Ruby [![Build Status](https://travis-ci.org/pawurb/smart_init.svg)](https://travis-ci.org/pawurb/smart_init) [![Gem Version](https://badge.fury.io/rb/smart_init.svg)](https://badge.fury.io/rb/smart_init)
|
2
2
|
|
3
|
-
Do you find yourself writing a lot of boilerplate code like
|
3
|
+
Do you find yourself writing a lot of boilerplate code like this?
|
4
4
|
|
5
5
|
```ruby
|
6
6
|
def initialize(network_provider, api_token)
|
@@ -13,7 +13,7 @@ def self.call(network_provider, api_token)
|
|
13
13
|
end
|
14
14
|
```
|
15
15
|
|
16
|
-
|
16
|
+
This gem provides a simple DSL for getting rid of it. It offers an alternative to using `Struct.new` which does not check for number of parameters provided in initializer, exposes getters and instantiates unecessary class instances.
|
17
17
|
|
18
18
|
**Smart Init** offers a unified API convention for stateless service objects, accepting values in initializer and exposing one public class method `call` which instantiates new objects and accepts arguments passed to initializer.
|
19
19
|
|
@@ -32,12 +32,13 @@ gem 'smart_init'
|
|
32
32
|
You can use it either by extending a module:
|
33
33
|
|
34
34
|
```ruby
|
35
|
+
require 'smart_init'
|
36
|
+
|
35
37
|
class ApiClient
|
36
38
|
extend SmartInit
|
37
39
|
|
38
40
|
initialize_with :network_provider, :api_token
|
39
41
|
end
|
40
|
-
|
41
42
|
```
|
42
43
|
|
43
44
|
or subclassing:
|
@@ -46,7 +47,6 @@ or subclassing:
|
|
46
47
|
class ApiClient < SmartInit::Base
|
47
48
|
initialize_with :network_provider, :api_token
|
48
49
|
end
|
49
|
-
|
50
50
|
```
|
51
51
|
|
52
52
|
Now you can just:
|
@@ -113,7 +113,6 @@ end
|
|
113
113
|
|
114
114
|
Adder.call(num_a: 2) => 4
|
115
115
|
Adder.call(num_a: 2, num_b: 3) => 5
|
116
|
-
|
117
116
|
```
|
118
117
|
|
119
118
|
### Readers access
|
@@ -123,7 +122,6 @@ Contrary to using Struct, by default the reader methods are not publicly exposed
|
|
123
122
|
```ruby
|
124
123
|
client = ApiClient.new(network_provider: Faraday.new, api_token: 'secret_token')
|
125
124
|
client.api_token => # NoMethodError (private method `api_token' called for #<ApiClient:0x000..>)
|
126
|
-
|
127
125
|
```
|
128
126
|
|
129
127
|
Optionally you can make all or subset of readers public using the `public_readers` config option. It accepts `true` or an array of method names as an argument.
|
@@ -133,7 +131,7 @@ class PublicApiClient < SmartInit::Base
|
|
133
131
|
initialize_with :network_provider, :api_token, public_readers: true
|
134
132
|
end
|
135
133
|
|
136
|
-
client =
|
134
|
+
client = PublicApiClient.new(network_provider: Faraday.new, api_token: 'secret_token')
|
137
135
|
client.network_provider => #<Faraday::Connection:0x000...>
|
138
136
|
client.api_token => 'secret_token'
|
139
137
|
```
|
@@ -143,25 +141,78 @@ class SemiPublicApiClient < SmartInit::Base
|
|
143
141
|
initialize_with :network_provider, :api_token, public_readers: [:network_provider]
|
144
142
|
end
|
145
143
|
|
144
|
+
client = SemiPublicApiClient.new(network_provider: Faraday.new, api_token: 'secret_token')
|
145
|
+
client.network_provider => #<Faraday::Connection:0x000...>
|
146
|
+
client.api_token => 'secret_token' => # NoMethodError (private method `api_token' called for #<SemiPublicApiClient:0x000...>)
|
147
|
+
```
|
148
|
+
|
149
|
+
### Accessors access
|
150
|
+
|
151
|
+
Similarly, this is how it would look if you tried to use a writer method:
|
152
|
+
|
153
|
+
```ruby
|
146
154
|
client = ApiClient.new(network_provider: Faraday.new, api_token: 'secret_token')
|
155
|
+
client.api_token = 'new_token' => # NoMethodError (private method `api_token=' called for #<ApiClient:0x000..>)
|
156
|
+
```
|
157
|
+
|
158
|
+
Optionally you can make all or subset of accessors public using the `public_accessors` config option. It accepts `true` or an array of method names as an argument. This will provide both reader and writer methods publicly.
|
159
|
+
|
160
|
+
```ruby
|
161
|
+
class PublicApiClient < SmartInit::Base
|
162
|
+
initialize_with :network_provider, :api_token, public_accessors: true
|
163
|
+
end
|
164
|
+
|
165
|
+
client = PublicApiClient.new(network_provider: Faraday.new, api_token: 'secret_token')
|
147
166
|
client.network_provider => #<Faraday::Connection:0x000...>
|
148
|
-
client.
|
167
|
+
client.network_provider = Typhoeus::Request.new(...) => #<Typhoeus::Request:0x000...>
|
168
|
+
client.api_token => 'secret_token'
|
169
|
+
client.api_token = 'new_token' => 'new_token'
|
149
170
|
```
|
150
171
|
|
151
|
-
|
172
|
+
```ruby
|
173
|
+
class SemiPublicApiClient < SmartInit::Base
|
174
|
+
initialize_with :network_provider, :api_token, public_accessors: [:network_provider]
|
175
|
+
end
|
176
|
+
|
177
|
+
client = SemiPublicApiClient.new(network_provider: Faraday.new, api_token: 'secret_token')
|
178
|
+
client.network_provider => #<Faraday::Connection:0x000...>
|
179
|
+
client.network_provider = Typhoeus::Request.new(...) => #<Typhoeus::Request:0x000...>
|
180
|
+
client.api_token => # NoMethodError (private method `api_token' called for #<SemiPublicApiClient:0x000...>)
|
181
|
+
client.api_token = 'new_token' => # NoMethodError (undefined method `api_token=' called for #<SemiPublicApiClient:0x000...>)
|
182
|
+
```
|
152
183
|
|
153
|
-
|
184
|
+
Finally, you can mix them together like this:
|
154
185
|
|
155
186
|
```ruby
|
156
|
-
class
|
157
|
-
|
158
|
-
|
187
|
+
class PublicReadersSemiPublicAccessorsApiClient < SmartInit::Base
|
188
|
+
initialize_with :network_provider, :api_token, :timeout,
|
189
|
+
public_readers: true, public_accessors: [:network_provider]
|
190
|
+
end
|
159
191
|
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
192
|
+
client = PublicReadersSemiPublicAccessorsApiClient.new(
|
193
|
+
network_provider: Faraday.new, api_token: 'secret_token', timeout_length: 100
|
194
|
+
)
|
195
|
+
client.network_provider => #<Faraday::Connection:0x000...>
|
196
|
+
client.network_provider = Typhoeus::Request.new(...) => #<Typhoeus::Request:0x000...>
|
197
|
+
client.api_token => 'secret_token'
|
198
|
+
client.api_token = 'new_token' => # NoMethodError (undefined method `api_token=' called for #<SemiPublicApiClient:0x000...>)
|
199
|
+
client.timeout_length => 100
|
200
|
+
client.timeout_length = 150 => # NoMethodError (undefined method `timeout_length=' called for #<SemiPublicApiClient:0x000...>)
|
201
|
+
```
|
202
|
+
|
203
|
+
```ruby
|
204
|
+
class SemiPublicReadersSemiPublicAccessorsApiClient < SmartInit::Base
|
205
|
+
initialize_with :network_provider, :api_token, :timeout,
|
206
|
+
public_readers: [:timeout], public_accessors: [:network_provider]
|
164
207
|
end
|
165
208
|
|
166
|
-
|
209
|
+
client = SemiPublicReadersSemiPublicAccessorsApiClient.new(
|
210
|
+
network_provider: Faraday.new, api_token: 'secret_token', timeout_length: 100
|
211
|
+
)
|
212
|
+
client.network_provider => #<Faraday::Connection:0x000...>
|
213
|
+
client.network_provider = Typhoeus::Request.new(...) => #<Typhoeus::Request:0x000...>
|
214
|
+
client.api_token => # NoMethodError (private method `api_token' called for #<SemiPublicReadersSemiPublicAccessorsApiClient:0x000...>)
|
215
|
+
client.api_token = 'new_token' => # NoMethodError (undefined method `api_token=' called for #<SemiPublicReadersSemiPublicAccessorsApiClient:0x000...>)
|
216
|
+
client.timeout_length => 100
|
217
|
+
client.timeout_length = 150 => # NoMethodError (undefined method `timeout_length=' called for #<SemiPublicReadersSemiPublicAccessorsApiClient:0x000...>)
|
167
218
|
```
|
data/lib/smart_init.rb
CHANGED
data/lib/smart_init/main.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module SmartInit
|
2
4
|
def is_callable(opts={})
|
3
5
|
method_name = if name_from_opts = opts[:method_name]
|
@@ -6,38 +8,32 @@ module SmartInit
|
|
6
8
|
:call
|
7
9
|
end
|
8
10
|
|
9
|
-
define_singleton_method method_name do
|
10
|
-
new(
|
11
|
+
define_singleton_method method_name do |**parameters|
|
12
|
+
new(**parameters).public_send(method_name)
|
11
13
|
end
|
12
14
|
end
|
13
15
|
|
14
|
-
def initialize_with_hash
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
define_method :initialize do |*parameters|
|
25
|
-
parameters = [{}] if parameters == []
|
26
|
-
unless parameters.first.is_a?(Hash)
|
27
|
-
raise ArgumentError, "invalid input, expected hash of attributes"
|
28
|
-
end
|
16
|
+
def initialize_with_hash(*required_attrs, **attributes_and_options)
|
17
|
+
public_readers = attributes_and_options.delete(:public_readers) || []
|
18
|
+
public_accessors = attributes_and_options.delete(:public_accessors) || []
|
19
|
+
if public_readers == true || public_accessors == true
|
20
|
+
public_readers = required_attrs
|
21
|
+
public_accessors = required_attrs if public_accessors == true
|
22
|
+
else
|
23
|
+
public_readers += public_accessors
|
24
|
+
end
|
29
25
|
|
26
|
+
define_method :initialize do |**parameters|
|
30
27
|
required_attrs.each do |required_attr|
|
31
|
-
unless parameters.
|
28
|
+
unless parameters.has_key?(required_attr)
|
32
29
|
raise ArgumentError, "missing required attribute #{required_attr}"
|
33
30
|
end
|
34
31
|
end
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
parameters.first.fetch(attribute)
|
32
|
+
(required_attrs + attributes_and_options.keys).each do |attribute|
|
33
|
+
value = if parameters.has_key?(attribute)
|
34
|
+
parameters.fetch(attribute)
|
39
35
|
else
|
40
|
-
|
36
|
+
attributes_and_options[attribute]
|
41
37
|
end
|
42
38
|
|
43
39
|
instance_variable_set("@#{attribute}", value)
|
@@ -45,44 +41,16 @@ module SmartInit
|
|
45
41
|
end
|
46
42
|
|
47
43
|
instance_eval do
|
48
|
-
all_readers = (required_attrs +
|
44
|
+
all_readers = (required_attrs + attributes_and_options.keys)
|
49
45
|
attr_reader(*all_readers)
|
50
|
-
|
51
|
-
|
52
|
-
all_readers.each do |method_name|
|
53
|
-
private method_name
|
54
|
-
end
|
55
|
-
elsif public_readers.first.fetch(:public_readers).is_a?(Array)
|
56
|
-
(all_readers - public_readers.first.fetch(:public_readers)).each do |method_name|
|
57
|
-
private method_name
|
58
|
-
end
|
46
|
+
(all_readers - public_readers).each do |reader|
|
47
|
+
private reader
|
59
48
|
end
|
49
|
+
attr_writer(*public_accessors)
|
60
50
|
end
|
61
51
|
end
|
62
52
|
|
63
53
|
alias initialize_with initialize_with_hash
|
64
|
-
|
65
|
-
def initialize_with_args *attributes
|
66
|
-
define_method :initialize do |*parameters|
|
67
|
-
if attributes.count != parameters.count
|
68
|
-
raise ArgumentError, "wrong number of arguments (given #{parameters.count}, expected #{attributes.count})"
|
69
|
-
end
|
70
|
-
|
71
|
-
attributes.zip(parameters).each do |pair|
|
72
|
-
name = pair[0]
|
73
|
-
value = pair[1]
|
74
|
-
instance_variable_set("@#{name}", value)
|
75
|
-
end
|
76
|
-
end
|
77
|
-
|
78
|
-
instance_eval do
|
79
|
-
attr_reader(*attributes)
|
80
|
-
|
81
|
-
attributes.each do |method_name|
|
82
|
-
private method_name
|
83
|
-
end
|
84
|
-
end
|
85
|
-
end
|
86
54
|
end
|
87
55
|
|
88
56
|
class SmartInit::Base
|
data/lib/smart_init/version.rb
CHANGED
data/test/test_hash_api.rb
CHANGED
@@ -0,0 +1,74 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "byebug"
|
4
|
+
require "test/unit"
|
5
|
+
require_relative '../lib/smart_init/main'
|
6
|
+
|
7
|
+
class TestAllPublicAccessors
|
8
|
+
extend SmartInit
|
9
|
+
initialize_with :attribute_1, :attribute_2, public_accessors: true
|
10
|
+
is_callable
|
11
|
+
|
12
|
+
def call
|
13
|
+
[attribute_1, attribute_2]
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
class TestSomePublicAccessors
|
18
|
+
extend SmartInit
|
19
|
+
initialize_with :attribute_1, :attribute_2, public_accessors: [:attribute_1]
|
20
|
+
is_callable
|
21
|
+
|
22
|
+
def call
|
23
|
+
[attribute_1, attribute_2]
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
class TestDefaultPublicAccessors
|
28
|
+
extend SmartInit
|
29
|
+
initialize_with :attribute_1, attribute_2: 2, public_accessors: [:attribute_2]
|
30
|
+
|
31
|
+
def call
|
32
|
+
[attribute_1, attribute_2]
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
class HashApiPublicTestAccessors < Test::Unit::TestCase
|
37
|
+
def test_all_public
|
38
|
+
service = TestAllPublicAccessors.new(attribute_1: "a", attribute_2: "b")
|
39
|
+
assert_nothing_raised do
|
40
|
+
service.attribute_1 = "c"
|
41
|
+
service.attribute_2 = "d"
|
42
|
+
end
|
43
|
+
assert_equal service.attribute_1, "c"
|
44
|
+
assert_equal service.attribute_2, "d"
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_some_public
|
48
|
+
service = TestSomePublicAccessors.new(attribute_1: "a", attribute_2: "b")
|
49
|
+
assert_nothing_raised do
|
50
|
+
service.attribute_1 = "c"
|
51
|
+
end
|
52
|
+
assert_equal service.attribute_1, "c"
|
53
|
+
assert_raise NoMethodError do
|
54
|
+
service.attribute_2
|
55
|
+
end
|
56
|
+
assert_raise NoMethodError do
|
57
|
+
service.attribute_2 = "d"
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def test_default_public
|
62
|
+
service = TestDefaultPublicAccessors.new(attribute_1: "a")
|
63
|
+
assert_nothing_raised do
|
64
|
+
service.attribute_2 = 3
|
65
|
+
end
|
66
|
+
assert_equal service.attribute_2, 3
|
67
|
+
assert_raise NoMethodError do
|
68
|
+
service.attribute_1 = "b"
|
69
|
+
end
|
70
|
+
assert_raise NoMethodError do
|
71
|
+
service.attribute_1
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
@@ -0,0 +1,83 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "byebug"
|
4
|
+
require "test/unit"
|
5
|
+
require_relative '../lib/smart_init/main'
|
6
|
+
|
7
|
+
class TestSomePublicMixed
|
8
|
+
extend SmartInit
|
9
|
+
initialize_with :attribute_1, :attribute_2, :attribute_3, :attribute_4,
|
10
|
+
public_readers: [:attribute_1],
|
11
|
+
public_accessors: [:attribute_2, :attribute_3]
|
12
|
+
is_callable
|
13
|
+
|
14
|
+
def call
|
15
|
+
[attribute_1, attribute_2, attribute_3, attribute_4]
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
class TestAllReadersSomeAccessorsPublic
|
20
|
+
extend SmartInit
|
21
|
+
initialize_with :attribute_1, :attribute_2, public_readers: true, public_accessors: [:attribute_2]
|
22
|
+
|
23
|
+
def call
|
24
|
+
[attribute_1, attribute_2]
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
class HashApiPublicTest < Test::Unit::TestCase
|
29
|
+
|
30
|
+
def test_readers_some_public_mixed
|
31
|
+
service = TestSomePublicMixed.new(
|
32
|
+
attribute_1: "a", attribute_2: "b",
|
33
|
+
attribute_3: "c", attribute_4: "d"
|
34
|
+
)
|
35
|
+
assert_nothing_raised do
|
36
|
+
service.attribute_1
|
37
|
+
service.attribute_2
|
38
|
+
service.attribute_3
|
39
|
+
end
|
40
|
+
assert_raise NoMethodError do
|
41
|
+
service.attribute_4
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_writers_some_public_mixed
|
46
|
+
service = TestSomePublicMixed.new(
|
47
|
+
attribute_1: "a", attribute_2: "b",
|
48
|
+
attribute_3: "c", attribute_4: "d"
|
49
|
+
)
|
50
|
+
assert_nothing_raised do
|
51
|
+
service.attribute_2 = "e"
|
52
|
+
service.attribute_3 = "f"
|
53
|
+
end
|
54
|
+
assert_equal service.attribute_2, "e"
|
55
|
+
assert_equal service.attribute_3, "f"
|
56
|
+
assert_raise NoMethodError do
|
57
|
+
service.attribute_4 = "g"
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def test_readers_all_readers_some_accessors_public
|
62
|
+
service = TestAllReadersSomeAccessorsPublic.new(
|
63
|
+
attribute_1: "a", attribute_2: "b"
|
64
|
+
)
|
65
|
+
assert_nothing_raised do
|
66
|
+
service.attribute_1
|
67
|
+
service.attribute_2
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def test_writers_all_readers_some_accessors_public
|
72
|
+
service = TestAllReadersSomeAccessorsPublic.new(
|
73
|
+
attribute_1: "a", attribute_2: "b"
|
74
|
+
)
|
75
|
+
assert_raise NoMethodError do
|
76
|
+
service.attribute_1 = "c"
|
77
|
+
end
|
78
|
+
assert_nothing_raised do
|
79
|
+
service.attribute_2 = "d"
|
80
|
+
end
|
81
|
+
assert_equal service.attribute_2, "d"
|
82
|
+
end
|
83
|
+
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require "byebug"
|
2
4
|
require "test/unit"
|
3
5
|
require_relative '../lib/smart_init/main'
|
@@ -22,6 +24,15 @@ class TestSomePublic
|
|
22
24
|
end
|
23
25
|
end
|
24
26
|
|
27
|
+
class TestDefaultPublic
|
28
|
+
extend SmartInit
|
29
|
+
initialize_with :attribute_1, attribute_2: 2, public_readers: [:attribute_2]
|
30
|
+
|
31
|
+
def call
|
32
|
+
[attribute_1, attribute_2]
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
25
36
|
class HashApiPublicTest < Test::Unit::TestCase
|
26
37
|
def test_all_public
|
27
38
|
service = TestAllPublic.new(attribute_1: "a", attribute_2: "b")
|
@@ -36,4 +47,14 @@ class HashApiPublicTest < Test::Unit::TestCase
|
|
36
47
|
service.attribute_2
|
37
48
|
end
|
38
49
|
end
|
50
|
+
|
51
|
+
def test_default_public
|
52
|
+
service = TestDefaultPublic.new(attribute_1: "a")
|
53
|
+
assert_equal service.attribute_2, 2
|
54
|
+
|
55
|
+
assert_raise NoMethodError do
|
56
|
+
service.attribute_1
|
57
|
+
end
|
58
|
+
end
|
39
59
|
end
|
60
|
+
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: smart_init
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 5.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- pawurb
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-08-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -69,14 +69,15 @@ files:
|
|
69
69
|
- lib/smart_init/main.rb
|
70
70
|
- lib/smart_init/version.rb
|
71
71
|
- smart_init.gemspec
|
72
|
-
- test/test_args_api.rb
|
73
72
|
- test/test_hash_api.rb
|
73
|
+
- test/test_hash_public_accessors.rb
|
74
|
+
- test/test_hash_public_mixed.rb
|
74
75
|
- test/test_hash_public_readers.rb
|
75
76
|
homepage: http://github.com/pawurb/smart_init
|
76
77
|
licenses:
|
77
78
|
- MIT
|
78
79
|
metadata: {}
|
79
|
-
post_install_message:
|
80
|
+
post_install_message:
|
80
81
|
rdoc_options: []
|
81
82
|
require_paths:
|
82
83
|
- lib
|
@@ -92,10 +93,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
92
93
|
version: '0'
|
93
94
|
requirements: []
|
94
95
|
rubygems_version: 3.1.2
|
95
|
-
signing_key:
|
96
|
+
signing_key:
|
96
97
|
specification_version: 4
|
97
98
|
summary: Remove Ruby initializer boilerplate code
|
98
99
|
test_files:
|
99
|
-
- test/test_args_api.rb
|
100
100
|
- test/test_hash_api.rb
|
101
|
+
- test/test_hash_public_accessors.rb
|
102
|
+
- test/test_hash_public_mixed.rb
|
101
103
|
- test/test_hash_public_readers.rb
|
data/test/test_args_api.rb
DELETED
@@ -1,77 +0,0 @@
|
|
1
|
-
require "test/unit"
|
2
|
-
require_relative '../lib/smart_init/main'
|
3
|
-
|
4
|
-
class TestClass
|
5
|
-
extend SmartInit
|
6
|
-
initialize_with_args :attribute_1, :attribute_2
|
7
|
-
is_callable
|
8
|
-
|
9
|
-
def call
|
10
|
-
[attribute_1, attribute_2]
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
|
-
class TestNoInit
|
15
|
-
extend SmartInit
|
16
|
-
is_callable
|
17
|
-
|
18
|
-
def call
|
19
|
-
'result'
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
class TestMethodName
|
24
|
-
extend SmartInit
|
25
|
-
|
26
|
-
is_callable method_name: :run!
|
27
|
-
|
28
|
-
def run!
|
29
|
-
true
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
def test_object
|
34
|
-
@_test_object ||= TestClass.new("attr_1_value", "attr_2_value")
|
35
|
-
end
|
36
|
-
|
37
|
-
class StandardApiTest < Test::Unit::TestCase
|
38
|
-
def test_number_of_attributes
|
39
|
-
assert_nothing_raised do
|
40
|
-
TestClass.new(
|
41
|
-
"attr_1_value",
|
42
|
-
"attr_2_value"
|
43
|
-
)
|
44
|
-
end
|
45
|
-
|
46
|
-
assert_raise ArgumentError do
|
47
|
-
TestClass.new(
|
48
|
-
"attr_1_value"
|
49
|
-
)
|
50
|
-
end
|
51
|
-
end
|
52
|
-
|
53
|
-
def test_instance_variables
|
54
|
-
assert_equal test_object.instance_variable_get("@attribute_1"), "attr_1_value"
|
55
|
-
end
|
56
|
-
|
57
|
-
def test_private_getters
|
58
|
-
error = assert_raise NoMethodError do
|
59
|
-
test_object.attribute_1
|
60
|
-
end
|
61
|
-
assert_match("private method", error.message)
|
62
|
-
|
63
|
-
assert_equal test_object.send(:attribute_1), "attr_1_value"
|
64
|
-
end
|
65
|
-
|
66
|
-
def test_is_callable
|
67
|
-
assert_equal TestClass.call("a", "b"), ["a", "b"]
|
68
|
-
end
|
69
|
-
|
70
|
-
def test_is_callable_no_initializers
|
71
|
-
assert_equal TestNoInit.call, 'result'
|
72
|
-
end
|
73
|
-
|
74
|
-
def test_is_callable_method_name
|
75
|
-
assert_equal TestMethodName.run!, true
|
76
|
-
end
|
77
|
-
end
|