smart_init 3.5.0 → 4.0.0
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/README.md +30 -5
- data/lib/smart_init/main.rb +18 -2
- data/lib/smart_init/version.rb +1 -1
- data/smart_init.gemspec +1 -0
- data/test/test_hash_api.rb +7 -0
- data/test/test_hash_public_readers.rb +39 -0
- metadata +19 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8f5bd5f6cfd9709e9378b01463ba8473b77aa3c1b17caf8fdbf5743ec26d506a
|
4
|
+
data.tar.gz: 756791cbea14b145fddb9b2b3d96a7c4b595bc3dea91c8998e78f26802b6d323
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4b5e72ebb4d1eb6299db75ccd64ef4e8189ad0047ceb64106baddfff678fc80ec33da75adb7dd8a92553df62aaa5005825715f2e89cbae630d6f65230fe88a92
|
7
|
+
data.tar.gz: f4286bc008ec50a1519c0bb3b33bedd67b44db78bb4e27e72592a3843db2163a345af4bcf38d8261fe492961fa0f48d8b36f1f5cd3c3a32595171666b4e74054
|
data/README.md
CHANGED
@@ -59,20 +59,45 @@ object = ApiClient.new(network_provider: Faraday.new, api_token: 'secret_token')
|
|
59
59
|
If you omit a required attribute an `ArgumentError` will be thrown:
|
60
60
|
|
61
61
|
```ruby
|
62
|
-
|
62
|
+
client = ApiClient.new(network_provider: Faraday.new)
|
63
63
|
|
64
64
|
# ArgumentError (missing required attribute api_token)
|
65
65
|
```
|
66
66
|
|
67
|
-
|
67
|
+
### Readers access
|
68
|
+
|
69
|
+
Contrary to using Struct, by default the reader methods are not publicly exposed:
|
68
70
|
|
69
71
|
```ruby
|
70
|
-
|
71
|
-
|
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
|
72
83
|
|
73
|
-
|
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'
|
74
87
|
```
|
75
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
|
+
### Making the object callable
|
100
|
+
|
76
101
|
You can also use `is_callable` method:
|
77
102
|
|
78
103
|
```ruby
|
data/lib/smart_init/main.rb
CHANGED
@@ -12,7 +12,13 @@ module SmartInit
|
|
12
12
|
end
|
13
13
|
|
14
14
|
def initialize_with_hash *attributes
|
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)
|
15
20
|
required_attrs = attributes.select { |attr| attr.is_a?(Symbol) }
|
21
|
+
|
16
22
|
default_value_attrs = attributes.select { |attr| attr.is_a?(Hash) }.first || {}
|
17
23
|
|
18
24
|
define_method :initialize do |*parameters|
|
@@ -21,6 +27,7 @@ module SmartInit
|
|
21
27
|
raise ArgumentError, "invalid input, expected hash of attributes"
|
22
28
|
end
|
23
29
|
|
30
|
+
|
24
31
|
required_attrs.each do |required_attr|
|
25
32
|
unless parameters.first.has_key?(required_attr)
|
26
33
|
raise ArgumentError, "missing required attribute #{required_attr}"
|
@@ -39,9 +46,18 @@ module SmartInit
|
|
39
46
|
end
|
40
47
|
|
41
48
|
instance_eval do
|
42
|
-
|
49
|
+
all_readers = (required_attrs + default_value_attrs.keys).compact
|
50
|
+
attr_reader(*all_readers)
|
43
51
|
|
44
|
-
|
52
|
+
if public_readers.count == 0
|
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
|
60
|
+
end
|
45
61
|
end
|
46
62
|
end
|
47
63
|
|
data/lib/smart_init/version.rb
CHANGED
data/smart_init.gemspec
CHANGED
data/test/test_hash_api.rb
CHANGED
@@ -57,6 +57,13 @@ class HashApiTest < Test::Unit::TestCase
|
|
57
57
|
assert_equal TestServiceDefaults.call(attribute_1: "a", attribute_2: "b"), ["a", "b", "default_value_3"]
|
58
58
|
end
|
59
59
|
|
60
|
+
def test_private_readers
|
61
|
+
service = TestServiceDefaults.call(attribute_1: "a")
|
62
|
+
assert_raise NoMethodError do
|
63
|
+
service.attribute_1
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
60
67
|
def test_integer_defaults
|
61
68
|
assert_equal TestHashIntegerDefaults.call(attribute_1: 1), [1, 2]
|
62
69
|
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require "byebug"
|
2
|
+
require "test/unit"
|
3
|
+
require_relative '../lib/smart_init/main'
|
4
|
+
|
5
|
+
class TestAllPublic
|
6
|
+
extend SmartInit
|
7
|
+
initialize_with :attribute_1, :attribute_2, public_readers: true
|
8
|
+
is_callable
|
9
|
+
|
10
|
+
def call
|
11
|
+
[attribute_1, attribute_2]
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
class TestSomePublic
|
16
|
+
extend SmartInit
|
17
|
+
initialize_with :attribute_1, :attribute_2, public_readers: [:attribute_1]
|
18
|
+
is_callable
|
19
|
+
|
20
|
+
def call
|
21
|
+
[attribute_1, attribute_2]
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
class HashApiPublicTest < Test::Unit::TestCase
|
26
|
+
def test_all_public
|
27
|
+
service = TestAllPublic.new(attribute_1: "a", attribute_2: "b")
|
28
|
+
assert_equal service.attribute_1, "a"
|
29
|
+
assert_equal service.attribute_2, "b"
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_some_public
|
33
|
+
service = TestSomePublic.new(attribute_1: "a", attribute_2: "b")
|
34
|
+
assert_equal service.attribute_1, "a"
|
35
|
+
assert_raise NoMethodError do
|
36
|
+
service.attribute_2
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
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: 4.0.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-
|
11
|
+
date: 2019-12-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -24,6 +24,20 @@ dependencies:
|
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: byebug
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
27
41
|
- !ruby/object:Gem::Dependency
|
28
42
|
name: test-unit
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -57,6 +71,7 @@ files:
|
|
57
71
|
- smart_init.gemspec
|
58
72
|
- test/test_args_api.rb
|
59
73
|
- test/test_hash_api.rb
|
74
|
+
- test/test_hash_public_readers.rb
|
60
75
|
homepage: http://github.com/pawurb/smart_init
|
61
76
|
licenses:
|
62
77
|
- MIT
|
@@ -76,10 +91,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
76
91
|
- !ruby/object:Gem::Version
|
77
92
|
version: '0'
|
78
93
|
requirements: []
|
79
|
-
rubygems_version: 3.0.
|
94
|
+
rubygems_version: 3.0.6
|
80
95
|
signing_key:
|
81
96
|
specification_version: 4
|
82
97
|
summary: Remove Ruby initializer boilerplate code
|
83
98
|
test_files:
|
84
99
|
- test/test_args_api.rb
|
85
100
|
- test/test_hash_api.rb
|
101
|
+
- test/test_hash_public_readers.rb
|