smart_init 3.5.0 → 4.0.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: 51e20a618133427922cb386a3469a77420e95eaf2ad9debaf3f7684a1a0f3e35
4
- data.tar.gz: 7441d3d6e5eec084365e041d1b33885489ffe4126ec6e16d8eefa04a28caab7a
3
+ metadata.gz: 8f5bd5f6cfd9709e9378b01463ba8473b77aa3c1b17caf8fdbf5743ec26d506a
4
+ data.tar.gz: 756791cbea14b145fddb9b2b3d96a7c4b595bc3dea91c8998e78f26802b6d323
5
5
  SHA512:
6
- metadata.gz: 48c99e64ba5e5325b3ea9087be3eff565add93252a7a52efddd2810f6b37d255d98b475e97614d7201009636fe5a1d6070309f0cf2e761e5da8b4de72b0ee313
7
- data.tar.gz: 2118838699bd2fcd42227041515033d01d790a1d6bcabe44437a0ce95cf0653fbc3c49d42d65a8bc65a708070302df621f79c62e7935d2d1eabd6552cb55f9ec
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
- object = ApiClient.new(network_provider: Faraday.new)
62
+ client = ApiClient.new(network_provider: Faraday.new)
63
63
 
64
64
  # ArgumentError (missing required attribute api_token)
65
65
  ```
66
66
 
67
- Contrary to using Struct, reader methods are not publicly exposed:
67
+ ### Readers access
68
+
69
+ Contrary to using Struct, by default the reader methods are not publicly exposed:
68
70
 
69
71
  ```ruby
70
- object = ApiClient.new(network_provider: Faraday.new, api_token: 'secret_token')
71
- object.api_token
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
- # NoMethodError (private method `api_token' called for #<ApiClient:0x00007fe911876a48>)
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
@@ -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
- private
49
+ all_readers = (required_attrs + default_value_attrs.keys).compact
50
+ attr_reader(*all_readers)
43
51
 
44
- attr_reader(*(required_attrs + default_value_attrs.keys).compact)
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
 
@@ -1,3 +1,3 @@
1
1
  module SmartInit
2
- VERSION = "3.5.0"
2
+ VERSION = "4.0.0"
3
3
  end
data/smart_init.gemspec CHANGED
@@ -16,5 +16,6 @@ Gem::Specification.new do |gem|
16
16
  gem.require_paths = ["lib"]
17
17
  gem.license = "MIT"
18
18
  gem.add_development_dependency "rake"
19
+ gem.add_development_dependency "byebug"
19
20
  gem.add_development_dependency "test-unit"
20
21
  end
@@ -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: 3.5.0
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-03 00:00:00.000000000 Z
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.3
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