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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cd5270377f234cbd06c6e2fe3b720b960cfc51b87b81de97d4e08857d3204b72
4
- data.tar.gz: a556d1a56de6b827ae421cc8a8994d88a32966d4c72ba9b27ea8f91bab61d63a
3
+ metadata.gz: fedc150bbb8aeed53e1bcc08073e643175a5fbe49f909a20c17cac145a95f969
4
+ data.tar.gz: 71c028b8b52b4ef553fd0d6a5711b913bac1fc8d46e4ca41d067b5e4441a5fa1
5
5
  SHA512:
6
- metadata.gz: d2477b6ac9b6fc5dff757732cf96c76de0faf943485dee2c76f0eb50555fdce9b536dfb723e0a816b118d5a54fa506ac8850b5354e1344d5695fe0b4c3e60fd6
7
- data.tar.gz: 5082e9640fdc6eefb1298a4fde60a25537d27e7c54737f075ad3fa144ce29e5cff7e39a0c3ce3ed5181d50ee70793fbfe5c018131494539cfb6484f3f89b668d
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 = ApiClient.new(network_provider: Faraday.new, api_token: 'secret_token')
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 = ApiClient.new(network_provider: Faraday.new, api_token: 'secret_token')
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
  ```
@@ -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) }.first || {}
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 == []
@@ -1,3 +1,3 @@
1
1
  module SmartInit
2
- VERSION = "4.0.1"
2
+ VERSION = "4.1.0"
3
3
  end
@@ -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.1
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: 2019-12-27 00:00:00.000000000 Z
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.1.2
94
+ rubygems_version: 3.0.6
95
95
  signing_key:
96
96
  specification_version: 4
97
97
  summary: Remove Ruby initializer boilerplate code