smart_init 4.0.0 → 4.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +1 -1
- data/README.md +35 -35
- data/lib/smart_init/main.rb +6 -5
- data/lib/smart_init/version.rb +1 -1
- data/test/test_args_api.rb +2 -1
- data/test/test_hash_api.rb +5 -3
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cd5270377f234cbd06c6e2fe3b720b960cfc51b87b81de97d4e08857d3204b72
|
4
|
+
data.tar.gz: a556d1a56de6b827ae421cc8a8994d88a32966d4c72ba9b27ea8f91bab61d63a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d2477b6ac9b6fc5dff757732cf96c76de0faf943485dee2c76f0eb50555fdce9b536dfb723e0a816b118d5a54fa506ac8850b5354e1344d5695fe0b4c3e60fd6
|
7
|
+
data.tar.gz: 5082e9640fdc6eefb1298a4fde60a25537d27e7c54737f075ad3fa144ce29e5cff7e39a0c3ce3ed5181d50ee70793fbfe5c018131494539cfb6484f3f89b668d
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
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)](
|
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)
|
2
2
|
|
3
3
|
Do you find yourself writing a lot of boilerplate code like that?
|
4
4
|
|
@@ -64,41 +64,9 @@ client = ApiClient.new(network_provider: Faraday.new)
|
|
64
64
|
# ArgumentError (missing required attribute api_token)
|
65
65
|
```
|
66
66
|
|
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
67
|
### Making the object callable
|
100
68
|
|
101
|
-
You can
|
69
|
+
You can use the `is_callable` method:
|
102
70
|
|
103
71
|
```ruby
|
104
72
|
class Calculator < SmartInit::Base
|
@@ -148,9 +116,41 @@ Adder.call(num_a: 2, num_b: 3) => 5
|
|
148
116
|
|
149
117
|
```
|
150
118
|
|
119
|
+
### Readers access
|
120
|
+
|
121
|
+
Contrary to using Struct, by default the reader methods are not publicly exposed:
|
122
|
+
|
123
|
+
```ruby
|
124
|
+
client = ApiClient.new(network_provider: Faraday.new, api_token: 'secret_token')
|
125
|
+
client.api_token => # NoMethodError (private method `api_token' called for #<ApiClient:0x000..>)
|
126
|
+
|
127
|
+
```
|
128
|
+
|
129
|
+
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.
|
130
|
+
|
131
|
+
```ruby
|
132
|
+
class PublicApiClient < SmartInit::Base
|
133
|
+
initialize_with :network_provider, :api_token, public_readers: true
|
134
|
+
end
|
135
|
+
|
136
|
+
client = ApiClient.new(network_provider: Faraday.new, api_token: 'secret_token')
|
137
|
+
client.network_provider => #<Faraday::Connection:0x000...>
|
138
|
+
client.api_token => 'secret_token'
|
139
|
+
```
|
140
|
+
|
141
|
+
```ruby
|
142
|
+
class SemiPublicApiClient < SmartInit::Base
|
143
|
+
initialize_with :network_provider, :api_token, public_readers: [:network_provider]
|
144
|
+
end
|
145
|
+
|
146
|
+
client = ApiClient.new(network_provider: Faraday.new, api_token: 'secret_token')
|
147
|
+
client.network_provider => #<Faraday::Connection:0x000...>
|
148
|
+
client.api_token => 'secret_token' => # NoMethodError (private method `api_token' called for #<ApiClient:0x000...>)
|
149
|
+
```
|
150
|
+
|
151
151
|
## Arguments API
|
152
152
|
|
153
|
-
Alternatively you can use an API without hash arguments and
|
153
|
+
Alternatively you can use an API without hash arguments, default values and public readers support:
|
154
154
|
|
155
155
|
```ruby
|
156
156
|
class Calculator < SmartInit::Base
|
data/lib/smart_init/main.rb
CHANGED
@@ -17,9 +17,9 @@ module SmartInit
|
|
17
17
|
}
|
18
18
|
public_readers = attributes.select(&public_readers_filter)
|
19
19
|
attributes.delete_if(&public_readers_filter)
|
20
|
-
required_attrs = attributes.select { |
|
20
|
+
required_attrs = attributes.select { |el| el.is_a?(Symbol) }
|
21
21
|
|
22
|
-
default_value_attrs = attributes.select { |
|
22
|
+
default_value_attrs = attributes.select { |el| el.is_a?(Hash) }.first || {}
|
23
23
|
|
24
24
|
define_method :initialize do |*parameters|
|
25
25
|
parameters = [{}] if parameters == []
|
@@ -27,7 +27,6 @@ module SmartInit
|
|
27
27
|
raise ArgumentError, "invalid input, expected hash of attributes"
|
28
28
|
end
|
29
29
|
|
30
|
-
|
31
30
|
required_attrs.each do |required_attr|
|
32
31
|
unless parameters.first.has_key?(required_attr)
|
33
32
|
raise ArgumentError, "missing required attribute #{required_attr}"
|
@@ -77,9 +76,11 @@ module SmartInit
|
|
77
76
|
end
|
78
77
|
|
79
78
|
instance_eval do
|
80
|
-
private
|
81
|
-
|
82
79
|
attr_reader(*attributes)
|
80
|
+
|
81
|
+
attributes.each do |method_name|
|
82
|
+
private method_name
|
83
|
+
end
|
83
84
|
end
|
84
85
|
end
|
85
86
|
end
|
data/lib/smart_init/version.rb
CHANGED
data/test/test_args_api.rb
CHANGED
@@ -55,9 +55,10 @@ class StandardApiTest < Test::Unit::TestCase
|
|
55
55
|
end
|
56
56
|
|
57
57
|
def test_private_getters
|
58
|
-
assert_raise NoMethodError do
|
58
|
+
error = assert_raise NoMethodError do
|
59
59
|
test_object.attribute_1
|
60
60
|
end
|
61
|
+
assert_match("private method", error.message)
|
61
62
|
|
62
63
|
assert_equal test_object.send(:attribute_1), "attr_1_value"
|
63
64
|
end
|
data/test/test_hash_api.rb
CHANGED
@@ -58,10 +58,12 @@ class HashApiTest < Test::Unit::TestCase
|
|
58
58
|
end
|
59
59
|
|
60
60
|
def test_private_readers
|
61
|
-
service = TestServiceDefaults.
|
62
|
-
assert_raise NoMethodError do
|
63
|
-
service.
|
61
|
+
service = TestServiceDefaults.new(attribute_1: "a")
|
62
|
+
error = assert_raise NoMethodError do
|
63
|
+
service.attribute_2
|
64
64
|
end
|
65
|
+
|
66
|
+
assert_match("private method", error.message)
|
65
67
|
end
|
66
68
|
|
67
69
|
def test_integer_defaults
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
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.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- pawurb
|
@@ -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.1.2
|
95
95
|
signing_key:
|
96
96
|
specification_version: 4
|
97
97
|
summary: Remove Ruby initializer boilerplate code
|