smart_init 4.0.0 → 4.2.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 +4 -4
- data/.travis.yml +1 -1
- data/LICENSE.txt +2 -2
- data/README.md +106 -39
- data/lib/smart_init.rb +2 -0
- data/lib/smart_init/main.rb +25 -33
- data/lib/smart_init/version.rb +3 -1
- data/test/test_args_api.rb +4 -1
- data/test/test_hash_api.rb +7 -3
- 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 +6 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 87c97f355f94cafbe2dc57c0c4894ecc2dbc916795d6f9789841a94f88cccd08
|
4
|
+
data.tar.gz: 1fae85e561e47465fee2b2c34520987d6d4517c8466061adc4da85093baca94f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b35c0abc26085a6008d0d34d878dfc4c1da3285cae60c052428a243af0bb68fe3c0f17fd660cf5e4cadb20ea3cf3adfbfe26771fcf76d025ecb037f483bbfdfa
|
7
|
+
data.tar.gz: ee1805b175baacf1476a8ef6a2ced3313279eae99b56f82dbf8ffc2f090a822fcd6af7f9870e8f3c11074371e2f5e7ef81a4605ba875c40b0c94147be25740bf
|
data/.travis.yml
CHANGED
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 [](https://travis-ci.org/pawurb/smart_init) [](
|
1
|
+
# Smart Init [](https://travis-ci.org/pawurb/smart_init) [](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
|
|
@@ -37,7 +37,6 @@ class ApiClient
|
|
37
37
|
|
38
38
|
initialize_with :network_provider, :api_token
|
39
39
|
end
|
40
|
-
|
41
40
|
```
|
42
41
|
|
43
42
|
or subclassing:
|
@@ -46,7 +45,6 @@ or subclassing:
|
|
46
45
|
class ApiClient < SmartInit::Base
|
47
46
|
initialize_with :network_provider, :api_token
|
48
47
|
end
|
49
|
-
|
50
48
|
```
|
51
49
|
|
52
50
|
Now you can just:
|
@@ -64,41 +62,9 @@ client = ApiClient.new(network_provider: Faraday.new)
|
|
64
62
|
# ArgumentError (missing required attribute api_token)
|
65
63
|
```
|
66
64
|
|
67
|
-
### Readers access
|
68
|
-
|
69
|
-
Contrary to using Struct, by default the reader methods are not publicly exposed:
|
70
|
-
|
71
|
-
```ruby
|
72
|
-
client = ApiClient.new(network_provider: Faraday.new, api_token: 'secret_token')
|
73
|
-
client.api_token => # NoMethodError (private method `api_token' called for #<ApiClient:0x000..>)
|
74
|
-
|
75
|
-
```
|
76
|
-
|
77
|
-
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.
|
78
|
-
|
79
|
-
```ruby
|
80
|
-
class PublicApiClient < SmartInit::Base
|
81
|
-
initialize_with :network_provider, :api_token, public_readers: true
|
82
|
-
end
|
83
|
-
|
84
|
-
client = ApiClient.new(network_provider: Faraday.new, api_token: 'secret_token')
|
85
|
-
client.network_provider => #<Faraday::Connection:0x000...>
|
86
|
-
client.api_token => 'secret_token'
|
87
|
-
```
|
88
|
-
|
89
|
-
```ruby
|
90
|
-
class SemiPublicApiClient < SmartInit::Base
|
91
|
-
initialize_with :network_provider, :api_token, public_readers: [:network_provider]
|
92
|
-
end
|
93
|
-
|
94
|
-
client = ApiClient.new(network_provider: Faraday.new, api_token: 'secret_token')
|
95
|
-
client.network_provider => #<Faraday::Connection:0x000...>
|
96
|
-
client.api_token => 'secret_token' => # NoMethodError (private method `api_token' called for #<ApiClient:0x000...>)
|
97
|
-
```
|
98
|
-
|
99
65
|
### Making the object callable
|
100
66
|
|
101
|
-
You can
|
67
|
+
You can use the `is_callable` method:
|
102
68
|
|
103
69
|
```ruby
|
104
70
|
class Calculator < SmartInit::Base
|
@@ -145,12 +111,113 @@ end
|
|
145
111
|
|
146
112
|
Adder.call(num_a: 2) => 4
|
147
113
|
Adder.call(num_a: 2, num_b: 3) => 5
|
114
|
+
```
|
115
|
+
|
116
|
+
### Readers access
|
117
|
+
|
118
|
+
Contrary to using Struct, by default the reader methods are not publicly exposed:
|
119
|
+
|
120
|
+
```ruby
|
121
|
+
client = ApiClient.new(network_provider: Faraday.new, api_token: 'secret_token')
|
122
|
+
client.api_token => # NoMethodError (private method `api_token' called for #<ApiClient:0x000..>)
|
123
|
+
```
|
124
|
+
|
125
|
+
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.
|
126
|
+
|
127
|
+
```ruby
|
128
|
+
class PublicApiClient < SmartInit::Base
|
129
|
+
initialize_with :network_provider, :api_token, public_readers: true
|
130
|
+
end
|
131
|
+
|
132
|
+
client = PublicApiClient.new(network_provider: Faraday.new, api_token: 'secret_token')
|
133
|
+
client.network_provider => #<Faraday::Connection:0x000...>
|
134
|
+
client.api_token => 'secret_token'
|
135
|
+
```
|
136
|
+
|
137
|
+
```ruby
|
138
|
+
class SemiPublicApiClient < SmartInit::Base
|
139
|
+
initialize_with :network_provider, :api_token, public_readers: [:network_provider]
|
140
|
+
end
|
141
|
+
|
142
|
+
client = SemiPublicApiClient.new(network_provider: Faraday.new, api_token: 'secret_token')
|
143
|
+
client.network_provider => #<Faraday::Connection:0x000...>
|
144
|
+
client.api_token => 'secret_token' => # NoMethodError (private method `api_token' called for #<SemiPublicApiClient:0x000...>)
|
145
|
+
```
|
146
|
+
|
147
|
+
### Accessors access
|
148
|
+
|
149
|
+
Similarly, this is how it would look if you tried to use a writer method:
|
150
|
+
|
151
|
+
```ruby
|
152
|
+
client = ApiClient.new(network_provider: Faraday.new, api_token: 'secret_token')
|
153
|
+
client.api_token = 'new_token' => # NoMethodError (private method `api_token=' called for #<ApiClient:0x000..>)
|
154
|
+
```
|
155
|
+
|
156
|
+
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.
|
157
|
+
|
158
|
+
```ruby
|
159
|
+
class PublicApiClient < SmartInit::Base
|
160
|
+
initialize_with :network_provider, :api_token, public_accessors: true
|
161
|
+
end
|
162
|
+
|
163
|
+
client = PublicApiClient.new(network_provider: Faraday.new, api_token: 'secret_token')
|
164
|
+
client.network_provider => #<Faraday::Connection:0x000...>
|
165
|
+
client.network_provider = Typhoeus::Request.new(...) => #<Typhoeus::Request:0x000...>
|
166
|
+
client.api_token => 'secret_token'
|
167
|
+
client.api_token = 'new_token' => 'new_token'
|
168
|
+
```
|
169
|
+
|
170
|
+
```ruby
|
171
|
+
class SemiPublicApiClient < SmartInit::Base
|
172
|
+
initialize_with :network_provider, :api_token, public_accessors: [:network_provider]
|
173
|
+
end
|
174
|
+
|
175
|
+
client = SemiPublicApiClient.new(network_provider: Faraday.new, api_token: 'secret_token')
|
176
|
+
client.network_provider => #<Faraday::Connection:0x000...>
|
177
|
+
client.network_provider = Typhoeus::Request.new(...) => #<Typhoeus::Request:0x000...>
|
178
|
+
client.api_token => # NoMethodError (private method `api_token' called for #<SemiPublicApiClient:0x000...>)
|
179
|
+
client.api_token = 'new_token' => # NoMethodError (undefined method `api_token=' called for #<SemiPublicApiClient:0x000...>)
|
180
|
+
```
|
148
181
|
|
182
|
+
Finally, you can mix them together like this:
|
183
|
+
|
184
|
+
```ruby
|
185
|
+
class PublicReadersSemiPublicAccessorsApiClient < SmartInit::Base
|
186
|
+
initialize_with :network_provider, :api_token, :timeout,
|
187
|
+
public_readers: true, public_accessors: [:network_provider]
|
188
|
+
end
|
189
|
+
|
190
|
+
client = PublicReadersSemiPublicAccessorsApiClient.new(
|
191
|
+
network_provider: Faraday.new, api_token: 'secret_token', timeout_length: 100
|
192
|
+
)
|
193
|
+
client.network_provider => #<Faraday::Connection:0x000...>
|
194
|
+
client.network_provider = Typhoeus::Request.new(...) => #<Typhoeus::Request:0x000...>
|
195
|
+
client.api_token => 'secret_token'
|
196
|
+
client.api_token = 'new_token' => # NoMethodError (undefined method `api_token=' called for #<SemiPublicApiClient:0x000...>)
|
197
|
+
client.timeout_length => 100
|
198
|
+
client.timeout_length = 150 => # NoMethodError (undefined method `timeout_length=' called for #<SemiPublicApiClient:0x000...>)
|
199
|
+
```
|
200
|
+
|
201
|
+
```ruby
|
202
|
+
class SemiPublicReadersSemiPublicAccessorsApiClient < SmartInit::Base
|
203
|
+
initialize_with :network_provider, :api_token, :timeout,
|
204
|
+
public_readers: [:timeout], public_accessors: [:network_provider]
|
205
|
+
end
|
206
|
+
|
207
|
+
client = SemiPublicReadersSemiPublicAccessorsApiClient.new(
|
208
|
+
network_provider: Faraday.new, api_token: 'secret_token', timeout_length: 100
|
209
|
+
)
|
210
|
+
client.network_provider => #<Faraday::Connection:0x000...>
|
211
|
+
client.network_provider = Typhoeus::Request.new(...) => #<Typhoeus::Request:0x000...>
|
212
|
+
client.api_token => # NoMethodError (private method `api_token' called for #<SemiPublicReadersSemiPublicAccessorsApiClient:0x000...>)
|
213
|
+
client.api_token = 'new_token' => # NoMethodError (undefined method `api_token=' called for #<SemiPublicReadersSemiPublicAccessorsApiClient:0x000...>)
|
214
|
+
client.timeout_length => 100
|
215
|
+
client.timeout_length = 150 => # NoMethodError (undefined method `timeout_length=' called for #<SemiPublicReadersSemiPublicAccessorsApiClient:0x000...>)
|
149
216
|
```
|
150
217
|
|
151
218
|
## Arguments API
|
152
219
|
|
153
|
-
Alternatively you can use an API without hash arguments
|
220
|
+
Alternatively you can use an API without hash arguments, default values, public readers, or public accessors support:
|
154
221
|
|
155
222
|
```ruby
|
156
223
|
class Calculator < SmartInit::Base
|
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]
|
@@ -11,34 +13,28 @@ module SmartInit
|
|
11
13
|
end
|
12
14
|
end
|
13
15
|
|
14
|
-
def initialize_with_hash
|
15
|
-
public_readers_filter = -> (el) {
|
16
|
-
el.is_a?(Hash) && el.keys == [:public_readers]
|
17
|
-
}
|
18
|
-
public_readers = attributes.select(&public_readers_filter)
|
19
|
-
attributes.delete_if(&public_readers_filter)
|
20
|
-
required_attrs = attributes.select { |attr| attr.is_a?(Symbol) }
|
21
|
-
|
22
|
-
default_value_attrs = attributes.select { |attr| attr.is_a?(Hash) }.first || {}
|
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)
|
29
17
|
|
18
|
+
public_readers = attributes_and_options.delete(:public_readers) || []
|
19
|
+
public_accessors = attributes_and_options.delete(:public_accessors) || []
|
20
|
+
if public_readers == true || public_accessors == true
|
21
|
+
public_readers = required_attrs
|
22
|
+
public_accessors = required_attrs if public_accessors == true
|
23
|
+
else
|
24
|
+
public_readers += public_accessors
|
25
|
+
end
|
30
26
|
|
27
|
+
define_method :initialize do |**parameters|
|
31
28
|
required_attrs.each do |required_attr|
|
32
|
-
unless parameters.
|
29
|
+
unless parameters.has_key?(required_attr)
|
33
30
|
raise ArgumentError, "missing required attribute #{required_attr}"
|
34
31
|
end
|
35
32
|
end
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
parameters.first.fetch(attribute)
|
33
|
+
(required_attrs + attributes_and_options.keys).each do |attribute|
|
34
|
+
value = if parameters.has_key?(attribute)
|
35
|
+
parameters.fetch(attribute)
|
40
36
|
else
|
41
|
-
|
37
|
+
attributes_and_options[attribute]
|
42
38
|
end
|
43
39
|
|
44
40
|
instance_variable_set("@#{attribute}", value)
|
@@ -46,18 +42,12 @@ module SmartInit
|
|
46
42
|
end
|
47
43
|
|
48
44
|
instance_eval do
|
49
|
-
all_readers = (required_attrs +
|
45
|
+
all_readers = (required_attrs + attributes_and_options.keys)
|
50
46
|
attr_reader(*all_readers)
|
51
|
-
|
52
|
-
|
53
|
-
all_readers.each do |method_name|
|
54
|
-
private method_name
|
55
|
-
end
|
56
|
-
elsif public_readers.first.fetch(:public_readers).is_a?(Array)
|
57
|
-
(all_readers - public_readers.first.fetch(:public_readers)).each do |method_name|
|
58
|
-
private method_name
|
59
|
-
end
|
47
|
+
(all_readers - public_readers).each do |reader|
|
48
|
+
private reader
|
60
49
|
end
|
50
|
+
attr_writer(*public_accessors)
|
61
51
|
end
|
62
52
|
end
|
63
53
|
|
@@ -77,9 +67,11 @@ module SmartInit
|
|
77
67
|
end
|
78
68
|
|
79
69
|
instance_eval do
|
80
|
-
private
|
81
|
-
|
82
70
|
attr_reader(*attributes)
|
71
|
+
|
72
|
+
attributes.each do |method_name|
|
73
|
+
private method_name
|
74
|
+
end
|
83
75
|
end
|
84
76
|
end
|
85
77
|
end
|
data/lib/smart_init/version.rb
CHANGED
data/test/test_args_api.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require "test/unit"
|
2
4
|
require_relative '../lib/smart_init/main'
|
3
5
|
|
@@ -55,9 +57,10 @@ class StandardApiTest < Test::Unit::TestCase
|
|
55
57
|
end
|
56
58
|
|
57
59
|
def test_private_getters
|
58
|
-
assert_raise NoMethodError do
|
60
|
+
error = assert_raise NoMethodError do
|
59
61
|
test_object.attribute_1
|
60
62
|
end
|
63
|
+
assert_match("private method", error.message)
|
61
64
|
|
62
65
|
assert_equal test_object.send(:attribute_1), "attr_1_value"
|
63
66
|
end
|
data/test/test_hash_api.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require "test/unit"
|
2
4
|
require_relative '../lib/smart_init/main'
|
3
5
|
|
@@ -58,10 +60,12 @@ class HashApiTest < Test::Unit::TestCase
|
|
58
60
|
end
|
59
61
|
|
60
62
|
def test_private_readers
|
61
|
-
service = TestServiceDefaults.
|
62
|
-
assert_raise NoMethodError do
|
63
|
-
service.
|
63
|
+
service = TestServiceDefaults.new(attribute_1: "a")
|
64
|
+
error = assert_raise NoMethodError do
|
65
|
+
service.attribute_2
|
64
66
|
end
|
67
|
+
|
68
|
+
assert_match("private method", error.message)
|
65
69
|
end
|
66
70
|
|
67
71
|
def test_integer_defaults
|
@@ -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 TestAllPublic
|
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 TestSomePublic
|
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 TestDefaultPublic
|
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 HashApiPublicTest < Test::Unit::TestCase
|
37
|
+
def test_all_public
|
38
|
+
service = TestAllPublic.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 = TestSomePublic.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 = TestDefaultPublic.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.
|
4
|
+
version: 4.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- pawurb
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-06-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -71,6 +71,8 @@ files:
|
|
71
71
|
- smart_init.gemspec
|
72
72
|
- test/test_args_api.rb
|
73
73
|
- test/test_hash_api.rb
|
74
|
+
- test/test_hash_public_accessors.rb
|
75
|
+
- test/test_hash_public_mixed.rb
|
74
76
|
- test/test_hash_public_readers.rb
|
75
77
|
homepage: http://github.com/pawurb/smart_init
|
76
78
|
licenses:
|
@@ -98,4 +100,6 @@ summary: Remove Ruby initializer boilerplate code
|
|
98
100
|
test_files:
|
99
101
|
- test/test_args_api.rb
|
100
102
|
- test/test_hash_api.rb
|
103
|
+
- test/test_hash_public_accessors.rb
|
104
|
+
- test/test_hash_public_mixed.rb
|
101
105
|
- test/test_hash_public_readers.rb
|