smart_init 4.2.0 → 5.0.2
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/LICENSE.txt +2 -2
- data/README.md +3 -19
- data/lib/smart_init/main.rb +6 -27
- data/lib/smart_init/version.rb +3 -1
- data/lib/smart_init.rb +2 -0
- data/smart_init.gemspec +19 -15
- data/test/test_hash_api.rb +2 -0
- data/test/test_hash_public_accessors.rb +9 -7
- data/test/test_hash_public_mixed.rb +2 -0
- data/test/test_hash_public_readers.rb +17 -0
- metadata +5 -6
- data/test/test_args_api.rb +0 -77
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c940aeebc57ccfda08563113bada6dfb76719ab15de983801edeb532df79c022
|
4
|
+
data.tar.gz: ab037fe33f3536b862c66e9b66b3c372f96648e4d69bb4d542bc9533c5a22fe2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5afb3dd23c4b4ee6bbcf435e1708f1bc82799d4d918d589bb96397b71423f2338888a20bdf236e352acd76c4d82c2edf84f002830358e9f6b1d9faad8985d0c4
|
7
|
+
data.tar.gz: 276f69b1ec9b740f700e13c4153b68a377ad1f44bb35ce8efe33046dd9657c20c4ad50d6d90ce995df74597a6d590a6600f6fe021e8344bacb13cad747941600
|
data/LICENSE.txt
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
Copyright (c)
|
1
|
+
Copyright (c) 2021 Paweł Urbanek
|
2
2
|
|
3
3
|
MIT License
|
4
4
|
|
@@ -19,4 +19,4 @@ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
19
19
|
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
20
|
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
21
|
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
-
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# Smart Init [](https://travis-ci.org/pawurb/smart_init) [](https://badge.fury.io/rb/smart_init)
|
1
|
+
# Smart Init - Simple service objects in Ruby [](https://travis-ci.org/pawurb/smart_init) [](https://badge.fury.io/rb/smart_init)
|
2
2
|
|
3
3
|
Do you find yourself writing a lot of boilerplate code like this?
|
4
4
|
|
@@ -32,6 +32,8 @@ gem 'smart_init'
|
|
32
32
|
You can use it either by extending a module:
|
33
33
|
|
34
34
|
```ruby
|
35
|
+
require 'smart_init'
|
36
|
+
|
35
37
|
class ApiClient
|
36
38
|
extend SmartInit
|
37
39
|
|
@@ -214,21 +216,3 @@ client.api_token = 'new_token' => # NoMethodError (undefined method `api_token='
|
|
214
216
|
client.timeout_length => 100
|
215
217
|
client.timeout_length = 150 => # NoMethodError (undefined method `timeout_length=' called for #<SemiPublicReadersSemiPublicAccessorsApiClient:0x000...>)
|
216
218
|
```
|
217
|
-
|
218
|
-
## Arguments API
|
219
|
-
|
220
|
-
Alternatively you can use an API without hash arguments, default values, public readers, or public accessors support:
|
221
|
-
|
222
|
-
```ruby
|
223
|
-
class Calculator < SmartInit::Base
|
224
|
-
initialize_with_args :data
|
225
|
-
is_callable
|
226
|
-
|
227
|
-
def call
|
228
|
-
...
|
229
|
-
result
|
230
|
-
end
|
231
|
-
end
|
232
|
-
|
233
|
-
Calculator.call(data) => result
|
234
|
-
```
|
data/lib/smart_init/main.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module SmartInit
|
2
4
|
def is_callable(opts={})
|
3
5
|
method_name = if name_from_opts = opts[:method_name]
|
@@ -6,18 +8,17 @@ module SmartInit
|
|
6
8
|
:call
|
7
9
|
end
|
8
10
|
|
9
|
-
define_singleton_method method_name do
|
10
|
-
new(
|
11
|
+
define_singleton_method method_name do |**parameters|
|
12
|
+
new(**parameters).public_send(method_name)
|
11
13
|
end
|
12
14
|
end
|
13
15
|
|
14
16
|
def initialize_with_hash(*required_attrs, **attributes_and_options)
|
15
|
-
|
16
17
|
public_readers = attributes_and_options.delete(:public_readers) || []
|
17
18
|
public_accessors = attributes_and_options.delete(:public_accessors) || []
|
18
19
|
if public_readers == true || public_accessors == true
|
19
|
-
public_readers = required_attrs
|
20
|
-
public_accessors = required_attrs if public_accessors == true
|
20
|
+
public_readers = required_attrs + attributes_and_options.keys
|
21
|
+
public_accessors = required_attrs + attributes_and_options.keys if public_accessors == true
|
21
22
|
else
|
22
23
|
public_readers += public_accessors
|
23
24
|
end
|
@@ -50,28 +51,6 @@ module SmartInit
|
|
50
51
|
end
|
51
52
|
|
52
53
|
alias initialize_with initialize_with_hash
|
53
|
-
|
54
|
-
def initialize_with_args *attributes
|
55
|
-
define_method :initialize do |*parameters|
|
56
|
-
if attributes.count != parameters.count
|
57
|
-
raise ArgumentError, "wrong number of arguments (given #{parameters.count}, expected #{attributes.count})"
|
58
|
-
end
|
59
|
-
|
60
|
-
attributes.zip(parameters).each do |pair|
|
61
|
-
name = pair[0]
|
62
|
-
value = pair[1]
|
63
|
-
instance_variable_set("@#{name}", value)
|
64
|
-
end
|
65
|
-
end
|
66
|
-
|
67
|
-
instance_eval do
|
68
|
-
attr_reader(*attributes)
|
69
|
-
|
70
|
-
attributes.each do |method_name|
|
71
|
-
private method_name
|
72
|
-
end
|
73
|
-
end
|
74
|
-
end
|
75
54
|
end
|
76
55
|
|
77
56
|
class SmartInit::Base
|
data/lib/smart_init/version.rb
CHANGED
data/lib/smart_init.rb
CHANGED
data/smart_init.gemspec
CHANGED
@@ -3,19 +3,23 @@ lib = File.expand_path('../lib', __FILE__)
|
|
3
3
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
4
|
require 'smart_init/version'
|
5
5
|
|
6
|
-
Gem::Specification.new do |
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = "smart_init"
|
8
|
+
s.version = SmartInit::VERSION
|
9
|
+
s.authors = ["pawurb"]
|
10
|
+
s.email = ["p.urbanek89@gmail.com"]
|
11
|
+
s.summary = %q{ Remove Ruby initializer boilerplate code }
|
12
|
+
s.description = %q{ A smart DSL for ruby initializers boilerplate }
|
13
|
+
s.homepage = "http://github.com/pawurb/smart_init"
|
14
|
+
s.files = `git ls-files`.split("\n")
|
15
|
+
s.test_files = s.files.grep(%r{^(test)/})
|
16
|
+
s.require_paths = ["lib"]
|
17
|
+
s.license = "MIT"
|
18
|
+
s.add_development_dependency "rake"
|
19
|
+
s.add_development_dependency "byebug"
|
20
|
+
s.add_development_dependency "test-unit"
|
21
|
+
|
22
|
+
if s.respond_to?(:metadata=)
|
23
|
+
s.metadata = { "rubygems_mfa_required" => "true" }
|
24
|
+
end
|
21
25
|
end
|
data/test/test_hash_api.rb
CHANGED
@@ -1,8 +1,10 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require "byebug"
|
2
4
|
require "test/unit"
|
3
5
|
require_relative '../lib/smart_init/main'
|
4
6
|
|
5
|
-
class
|
7
|
+
class TestAllPublicAccessors
|
6
8
|
extend SmartInit
|
7
9
|
initialize_with :attribute_1, :attribute_2, public_accessors: true
|
8
10
|
is_callable
|
@@ -12,7 +14,7 @@ class TestAllPublic
|
|
12
14
|
end
|
13
15
|
end
|
14
16
|
|
15
|
-
class
|
17
|
+
class TestSomePublicAccessors
|
16
18
|
extend SmartInit
|
17
19
|
initialize_with :attribute_1, :attribute_2, public_accessors: [:attribute_1]
|
18
20
|
is_callable
|
@@ -22,7 +24,7 @@ class TestSomePublic
|
|
22
24
|
end
|
23
25
|
end
|
24
26
|
|
25
|
-
class
|
27
|
+
class TestDefaultPublicAccessors
|
26
28
|
extend SmartInit
|
27
29
|
initialize_with :attribute_1, attribute_2: 2, public_accessors: [:attribute_2]
|
28
30
|
|
@@ -31,9 +33,9 @@ class TestDefaultPublic
|
|
31
33
|
end
|
32
34
|
end
|
33
35
|
|
34
|
-
class
|
36
|
+
class HashApiPublicTestAccessors < Test::Unit::TestCase
|
35
37
|
def test_all_public
|
36
|
-
service =
|
38
|
+
service = TestAllPublicAccessors.new(attribute_1: "a", attribute_2: "b")
|
37
39
|
assert_nothing_raised do
|
38
40
|
service.attribute_1 = "c"
|
39
41
|
service.attribute_2 = "d"
|
@@ -43,7 +45,7 @@ class HashApiPublicTest < Test::Unit::TestCase
|
|
43
45
|
end
|
44
46
|
|
45
47
|
def test_some_public
|
46
|
-
service =
|
48
|
+
service = TestSomePublicAccessors.new(attribute_1: "a", attribute_2: "b")
|
47
49
|
assert_nothing_raised do
|
48
50
|
service.attribute_1 = "c"
|
49
51
|
end
|
@@ -57,7 +59,7 @@ class HashApiPublicTest < Test::Unit::TestCase
|
|
57
59
|
end
|
58
60
|
|
59
61
|
def test_default_public
|
60
|
-
service =
|
62
|
+
service = TestDefaultPublicAccessors.new(attribute_1: "a")
|
61
63
|
assert_nothing_raised do
|
62
64
|
service.attribute_2 = 3
|
63
65
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require "byebug"
|
2
4
|
require "test/unit"
|
3
5
|
require_relative '../lib/smart_init/main'
|
@@ -31,6 +33,15 @@ class TestDefaultPublic
|
|
31
33
|
end
|
32
34
|
end
|
33
35
|
|
36
|
+
class TestDefaultAllPublic
|
37
|
+
extend SmartInit
|
38
|
+
initialize_with :attribute_1, attribute_2: 2, public_readers: true
|
39
|
+
|
40
|
+
def call
|
41
|
+
[attribute_1, attribute_2]
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
34
45
|
class HashApiPublicTest < Test::Unit::TestCase
|
35
46
|
def test_all_public
|
36
47
|
service = TestAllPublic.new(attribute_1: "a", attribute_2: "b")
|
@@ -54,5 +65,11 @@ class HashApiPublicTest < Test::Unit::TestCase
|
|
54
65
|
service.attribute_1
|
55
66
|
end
|
56
67
|
end
|
68
|
+
|
69
|
+
def test_default_all_public
|
70
|
+
service = TestDefaultAllPublic.new(attribute_1: "a")
|
71
|
+
assert_equal service.attribute_1, "a"
|
72
|
+
assert_equal service.attribute_2, 2
|
73
|
+
end
|
57
74
|
end
|
58
75
|
|
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: 5.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- pawurb
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-02-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -69,7 +69,6 @@ files:
|
|
69
69
|
- lib/smart_init/main.rb
|
70
70
|
- lib/smart_init/version.rb
|
71
71
|
- smart_init.gemspec
|
72
|
-
- test/test_args_api.rb
|
73
72
|
- test/test_hash_api.rb
|
74
73
|
- test/test_hash_public_accessors.rb
|
75
74
|
- test/test_hash_public_mixed.rb
|
@@ -77,7 +76,8 @@ files:
|
|
77
76
|
homepage: http://github.com/pawurb/smart_init
|
78
77
|
licenses:
|
79
78
|
- MIT
|
80
|
-
metadata:
|
79
|
+
metadata:
|
80
|
+
rubygems_mfa_required: 'true'
|
81
81
|
post_install_message:
|
82
82
|
rdoc_options: []
|
83
83
|
require_paths:
|
@@ -93,12 +93,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
93
93
|
- !ruby/object:Gem::Version
|
94
94
|
version: '0'
|
95
95
|
requirements: []
|
96
|
-
rubygems_version: 3.
|
96
|
+
rubygems_version: 3.1.6
|
97
97
|
signing_key:
|
98
98
|
specification_version: 4
|
99
99
|
summary: Remove Ruby initializer boilerplate code
|
100
100
|
test_files:
|
101
|
-
- test/test_args_api.rb
|
102
101
|
- test/test_hash_api.rb
|
103
102
|
- test/test_hash_public_accessors.rb
|
104
103
|
- test/test_hash_public_mixed.rb
|
data/test/test_args_api.rb
DELETED
@@ -1,77 +0,0 @@
|
|
1
|
-
require "test/unit"
|
2
|
-
require_relative '../lib/smart_init/main'
|
3
|
-
|
4
|
-
class TestClass
|
5
|
-
extend SmartInit
|
6
|
-
initialize_with_args :attribute_1, :attribute_2
|
7
|
-
is_callable
|
8
|
-
|
9
|
-
def call
|
10
|
-
[attribute_1, attribute_2]
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
|
-
class TestNoInit
|
15
|
-
extend SmartInit
|
16
|
-
is_callable
|
17
|
-
|
18
|
-
def call
|
19
|
-
'result'
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
class TestMethodName
|
24
|
-
extend SmartInit
|
25
|
-
|
26
|
-
is_callable method_name: :run!
|
27
|
-
|
28
|
-
def run!
|
29
|
-
true
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
def test_object
|
34
|
-
@_test_object ||= TestClass.new("attr_1_value", "attr_2_value")
|
35
|
-
end
|
36
|
-
|
37
|
-
class StandardApiTest < Test::Unit::TestCase
|
38
|
-
def test_number_of_attributes
|
39
|
-
assert_nothing_raised do
|
40
|
-
TestClass.new(
|
41
|
-
"attr_1_value",
|
42
|
-
"attr_2_value"
|
43
|
-
)
|
44
|
-
end
|
45
|
-
|
46
|
-
assert_raise ArgumentError do
|
47
|
-
TestClass.new(
|
48
|
-
"attr_1_value"
|
49
|
-
)
|
50
|
-
end
|
51
|
-
end
|
52
|
-
|
53
|
-
def test_instance_variables
|
54
|
-
assert_equal test_object.instance_variable_get("@attribute_1"), "attr_1_value"
|
55
|
-
end
|
56
|
-
|
57
|
-
def test_private_getters
|
58
|
-
error = assert_raise NoMethodError do
|
59
|
-
test_object.attribute_1
|
60
|
-
end
|
61
|
-
assert_match("private method", error.message)
|
62
|
-
|
63
|
-
assert_equal test_object.send(:attribute_1), "attr_1_value"
|
64
|
-
end
|
65
|
-
|
66
|
-
def test_is_callable
|
67
|
-
assert_equal TestClass.call("a", "b"), ["a", "b"]
|
68
|
-
end
|
69
|
-
|
70
|
-
def test_is_callable_no_initializers
|
71
|
-
assert_equal TestNoInit.call, 'result'
|
72
|
-
end
|
73
|
-
|
74
|
-
def test_is_callable_method_name
|
75
|
-
assert_equal TestMethodName.run!, true
|
76
|
-
end
|
77
|
-
end
|