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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8f5bd5f6cfd9709e9378b01463ba8473b77aa3c1b17caf8fdbf5743ec26d506a
4
- data.tar.gz: 756791cbea14b145fddb9b2b3d96a7c4b595bc3dea91c8998e78f26802b6d323
3
+ metadata.gz: cd5270377f234cbd06c6e2fe3b720b960cfc51b87b81de97d4e08857d3204b72
4
+ data.tar.gz: a556d1a56de6b827ae421cc8a8994d88a32966d4c72ba9b27ea8f91bab61d63a
5
5
  SHA512:
6
- metadata.gz: 4b5e72ebb4d1eb6299db75ccd64ef4e8189ad0047ceb64106baddfff678fc80ec33da75adb7dd8a92553df62aaa5005825715f2e89cbae630d6f65230fe88a92
7
- data.tar.gz: f4286bc008ec50a1519c0bb3b33bedd67b44db78bb4e27e72592a3843db2163a345af4bcf38d8261fe492961fa0f48d8b36f1f5cd3c3a32595171666b4e74054
6
+ metadata.gz: d2477b6ac9b6fc5dff757732cf96c76de0faf943485dee2c76f0eb50555fdce9b536dfb723e0a816b118d5a54fa506ac8850b5354e1344d5695fe0b4c3e60fd6
7
+ data.tar.gz: 5082e9640fdc6eefb1298a4fde60a25537d27e7c54737f075ad3fa144ce29e5cff7e39a0c3ce3ed5181d50ee70793fbfe5c018131494539cfb6484f3f89b668d
@@ -1,7 +1,7 @@
1
1
  rvm:
2
- - 2.4.4
3
2
  - 2.5.3
4
3
  - 2.6.5
4
+ - 2.7.0
5
5
  notifications:
6
6
  email: false
7
7
 
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)](http://badge.fury.io/rb/smart_init)
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 also use `is_callable` method:
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 default values:
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
@@ -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 { |attr| attr.is_a?(Symbol) }
20
+ required_attrs = attributes.select { |el| el.is_a?(Symbol) }
21
21
 
22
- default_value_attrs = attributes.select { |attr| attr.is_a?(Hash) }.first || {}
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
@@ -1,3 +1,3 @@
1
1
  module SmartInit
2
- VERSION = "4.0.0"
2
+ VERSION = "4.0.1"
3
3
  end
@@ -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
@@ -58,10 +58,12 @@ class HashApiTest < Test::Unit::TestCase
58
58
  end
59
59
 
60
60
  def test_private_readers
61
- service = TestServiceDefaults.call(attribute_1: "a")
62
- assert_raise NoMethodError do
63
- service.attribute_1
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.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.0.6
94
+ rubygems_version: 3.1.2
95
95
  signing_key:
96
96
  specification_version: 4
97
97
  summary: Remove Ruby initializer boilerplate code