smart_init 4.0.1 → 4.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +2 -6
- data/lib/smart_init/main.rb +9 -1
- data/lib/smart_init/version.rb +1 -1
- data/test/test_hash_public_readers.rb +19 -0
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fedc150bbb8aeed53e1bcc08073e643175a5fbe49f909a20c17cac145a95f969
|
4
|
+
data.tar.gz: 71c028b8b52b4ef553fd0d6a5711b913bac1fc8d46e4ca41d067b5e4441a5fa1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e1ec743157b4a687d902d08023654604db4c9ec502448313e9eb79f3e621cb0f43fa293fcba5bcd31018e14cfeb8dd149f937ac89a1cadf1ff0bcb28e226751a
|
7
|
+
data.tar.gz: 681f6bd9c9b9e75580154bde00819894ff3a3f0bbe9598041e21123b4d63284d7d01deda8e374fc462f41d55245c731ee64a70f12380ef2f77f15d2a570ca64f
|
data/README.md
CHANGED
@@ -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:
|
@@ -113,7 +111,6 @@ end
|
|
113
111
|
|
114
112
|
Adder.call(num_a: 2) => 4
|
115
113
|
Adder.call(num_a: 2, num_b: 3) => 5
|
116
|
-
|
117
114
|
```
|
118
115
|
|
119
116
|
### Readers access
|
@@ -123,7 +120,6 @@ Contrary to using Struct, by default the reader methods are not publicly exposed
|
|
123
120
|
```ruby
|
124
121
|
client = ApiClient.new(network_provider: Faraday.new, api_token: 'secret_token')
|
125
122
|
client.api_token => # NoMethodError (private method `api_token' called for #<ApiClient:0x000..>)
|
126
|
-
|
127
123
|
```
|
128
124
|
|
129
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.
|
@@ -133,7 +129,7 @@ class PublicApiClient < SmartInit::Base
|
|
133
129
|
initialize_with :network_provider, :api_token, public_readers: true
|
134
130
|
end
|
135
131
|
|
136
|
-
client =
|
132
|
+
client = PublicApiClient.new(network_provider: Faraday.new, api_token: 'secret_token')
|
137
133
|
client.network_provider => #<Faraday::Connection:0x000...>
|
138
134
|
client.api_token => 'secret_token'
|
139
135
|
```
|
@@ -143,7 +139,7 @@ class SemiPublicApiClient < SmartInit::Base
|
|
143
139
|
initialize_with :network_provider, :api_token, public_readers: [:network_provider]
|
144
140
|
end
|
145
141
|
|
146
|
-
client =
|
142
|
+
client = SemiPublicApiClient.new(network_provider: Faraday.new, api_token: 'secret_token')
|
147
143
|
client.network_provider => #<Faraday::Connection:0x000...>
|
148
144
|
client.api_token => 'secret_token' => # NoMethodError (private method `api_token' called for #<ApiClient:0x000...>)
|
149
145
|
```
|
data/lib/smart_init/main.rb
CHANGED
@@ -15,11 +15,19 @@ module SmartInit
|
|
15
15
|
public_readers_filter = -> (el) {
|
16
16
|
el.is_a?(Hash) && el.keys == [:public_readers]
|
17
17
|
}
|
18
|
+
attributes = attributes.map do |el|
|
19
|
+
if el.is_a?(Hash)
|
20
|
+
el.map { |k, v| { k => v } }
|
21
|
+
else
|
22
|
+
el
|
23
|
+
end
|
24
|
+
end.flatten
|
25
|
+
|
18
26
|
public_readers = attributes.select(&public_readers_filter)
|
19
27
|
attributes.delete_if(&public_readers_filter)
|
20
28
|
required_attrs = attributes.select { |el| el.is_a?(Symbol) }
|
21
29
|
|
22
|
-
default_value_attrs = attributes.select { |el| el.is_a?(Hash) }.
|
30
|
+
default_value_attrs = attributes.select { |el| el.is_a?(Hash) }.reduce(Hash.new, :merge) || {}
|
23
31
|
|
24
32
|
define_method :initialize do |*parameters|
|
25
33
|
parameters = [{}] if parameters == []
|
data/lib/smart_init/version.rb
CHANGED
@@ -22,6 +22,15 @@ class TestSomePublic
|
|
22
22
|
end
|
23
23
|
end
|
24
24
|
|
25
|
+
class TestDefaultPublic
|
26
|
+
extend SmartInit
|
27
|
+
initialize_with :attribute_1, attribute_2: 2, public_readers: [:attribute_2]
|
28
|
+
|
29
|
+
def call
|
30
|
+
[attribute_1, attribute_2]
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
25
34
|
class HashApiPublicTest < Test::Unit::TestCase
|
26
35
|
def test_all_public
|
27
36
|
service = TestAllPublic.new(attribute_1: "a", attribute_2: "b")
|
@@ -36,4 +45,14 @@ class HashApiPublicTest < Test::Unit::TestCase
|
|
36
45
|
service.attribute_2
|
37
46
|
end
|
38
47
|
end
|
48
|
+
|
49
|
+
def test_default_public
|
50
|
+
service = TestDefaultPublic.new(attribute_1: "a")
|
51
|
+
assert_equal service.attribute_2, 2
|
52
|
+
|
53
|
+
assert_raise NoMethodError do
|
54
|
+
service.attribute_1
|
55
|
+
end
|
56
|
+
end
|
39
57
|
end
|
58
|
+
|
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.0
|
4
|
+
version: 4.1.0
|
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-03-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -91,7 +91,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
91
91
|
- !ruby/object:Gem::Version
|
92
92
|
version: '0'
|
93
93
|
requirements: []
|
94
|
-
rubygems_version: 3.
|
94
|
+
rubygems_version: 3.0.6
|
95
95
|
signing_key:
|
96
96
|
specification_version: 4
|
97
97
|
summary: Remove Ruby initializer boilerplate code
|