smart_init 4.2.1 → 5.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 +3 -19
- data/lib/smart_init/main.rb +2 -25
- data/lib/smart_init/version.rb +1 -1
- data/test/test_hash_public_accessors.rb +7 -7
- metadata +6 -8
- data/test/test_args_api.rb +0 -79
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e98d279fac333ac355c8d854d724c567151dd9798d11cf248f50e75edc1ebdc0
|
4
|
+
data.tar.gz: 517a005cd94b3841e6f212c4368034c220aff25c1e73410e884445397bc5da34
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 98a3581a564a6ffc532c52540c4942d2b103b3d0bec9d5511e40e0fc57c6dd775bfb79ddcfa9d2e3acf0616ab8b4c724fc50ce173658cc63d14aff230667d3b0
|
7
|
+
data.tar.gz: f789ca9f6b424fc6b77f519ef90564d08ac85eb937ac11e77856cb2a41c1f41f12fc825f450d114ef8bf8d7c24f6dacd92ba9cbbad5e5c7944022a05ecc868c1
|
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
@@ -8,13 +8,12 @@ module SmartInit
|
|
8
8
|
:call
|
9
9
|
end
|
10
10
|
|
11
|
-
define_singleton_method method_name do
|
12
|
-
new(
|
11
|
+
define_singleton_method method_name do |**parameters|
|
12
|
+
new(**parameters).public_send(method_name)
|
13
13
|
end
|
14
14
|
end
|
15
15
|
|
16
16
|
def initialize_with_hash(*required_attrs, **attributes_and_options)
|
17
|
-
|
18
17
|
public_readers = attributes_and_options.delete(:public_readers) || []
|
19
18
|
public_accessors = attributes_and_options.delete(:public_accessors) || []
|
20
19
|
if public_readers == true || public_accessors == true
|
@@ -52,28 +51,6 @@ module SmartInit
|
|
52
51
|
end
|
53
52
|
|
54
53
|
alias initialize_with initialize_with_hash
|
55
|
-
|
56
|
-
def initialize_with_args *attributes
|
57
|
-
define_method :initialize do |*parameters|
|
58
|
-
if attributes.count != parameters.count
|
59
|
-
raise ArgumentError, "wrong number of arguments (given #{parameters.count}, expected #{attributes.count})"
|
60
|
-
end
|
61
|
-
|
62
|
-
attributes.zip(parameters).each do |pair|
|
63
|
-
name = pair[0]
|
64
|
-
value = pair[1]
|
65
|
-
instance_variable_set("@#{name}", value)
|
66
|
-
end
|
67
|
-
end
|
68
|
-
|
69
|
-
instance_eval do
|
70
|
-
attr_reader(*attributes)
|
71
|
-
|
72
|
-
attributes.each do |method_name|
|
73
|
-
private method_name
|
74
|
-
end
|
75
|
-
end
|
76
|
-
end
|
77
54
|
end
|
78
55
|
|
79
56
|
class SmartInit::Base
|
data/lib/smart_init/version.rb
CHANGED
@@ -4,7 +4,7 @@ require "byebug"
|
|
4
4
|
require "test/unit"
|
5
5
|
require_relative '../lib/smart_init/main'
|
6
6
|
|
7
|
-
class
|
7
|
+
class TestAllPublicAccessors
|
8
8
|
extend SmartInit
|
9
9
|
initialize_with :attribute_1, :attribute_2, public_accessors: true
|
10
10
|
is_callable
|
@@ -14,7 +14,7 @@ class TestAllPublic
|
|
14
14
|
end
|
15
15
|
end
|
16
16
|
|
17
|
-
class
|
17
|
+
class TestSomePublicAccessors
|
18
18
|
extend SmartInit
|
19
19
|
initialize_with :attribute_1, :attribute_2, public_accessors: [:attribute_1]
|
20
20
|
is_callable
|
@@ -24,7 +24,7 @@ class TestSomePublic
|
|
24
24
|
end
|
25
25
|
end
|
26
26
|
|
27
|
-
class
|
27
|
+
class TestDefaultPublicAccessors
|
28
28
|
extend SmartInit
|
29
29
|
initialize_with :attribute_1, attribute_2: 2, public_accessors: [:attribute_2]
|
30
30
|
|
@@ -33,9 +33,9 @@ class TestDefaultPublic
|
|
33
33
|
end
|
34
34
|
end
|
35
35
|
|
36
|
-
class
|
36
|
+
class HashApiPublicTestAccessors < Test::Unit::TestCase
|
37
37
|
def test_all_public
|
38
|
-
service =
|
38
|
+
service = TestAllPublicAccessors.new(attribute_1: "a", attribute_2: "b")
|
39
39
|
assert_nothing_raised do
|
40
40
|
service.attribute_1 = "c"
|
41
41
|
service.attribute_2 = "d"
|
@@ -45,7 +45,7 @@ class HashApiPublicTest < Test::Unit::TestCase
|
|
45
45
|
end
|
46
46
|
|
47
47
|
def test_some_public
|
48
|
-
service =
|
48
|
+
service = TestSomePublicAccessors.new(attribute_1: "a", attribute_2: "b")
|
49
49
|
assert_nothing_raised do
|
50
50
|
service.attribute_1 = "c"
|
51
51
|
end
|
@@ -59,7 +59,7 @@ class HashApiPublicTest < Test::Unit::TestCase
|
|
59
59
|
end
|
60
60
|
|
61
61
|
def test_default_public
|
62
|
-
service =
|
62
|
+
service = TestDefaultPublicAccessors.new(attribute_1: "a")
|
63
63
|
assert_nothing_raised do
|
64
64
|
service.attribute_2 = 3
|
65
65
|
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: 5.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- pawurb
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-08-10 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
|
@@ -78,7 +77,7 @@ homepage: http://github.com/pawurb/smart_init
|
|
78
77
|
licenses:
|
79
78
|
- MIT
|
80
79
|
metadata: {}
|
81
|
-
post_install_message:
|
80
|
+
post_install_message:
|
82
81
|
rdoc_options: []
|
83
82
|
require_paths:
|
84
83
|
- lib
|
@@ -93,12 +92,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
93
92
|
- !ruby/object:Gem::Version
|
94
93
|
version: '0'
|
95
94
|
requirements: []
|
96
|
-
rubygems_version: 3.
|
97
|
-
signing_key:
|
95
|
+
rubygems_version: 3.1.2
|
96
|
+
signing_key:
|
98
97
|
specification_version: 4
|
99
98
|
summary: Remove Ruby initializer boilerplate code
|
100
99
|
test_files:
|
101
|
-
- test/test_args_api.rb
|
102
100
|
- test/test_hash_api.rb
|
103
101
|
- test/test_hash_public_accessors.rb
|
104
102
|
- test/test_hash_public_mixed.rb
|
data/test/test_args_api.rb
DELETED
@@ -1,79 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require "test/unit"
|
4
|
-
require_relative '../lib/smart_init/main'
|
5
|
-
|
6
|
-
class TestClass
|
7
|
-
extend SmartInit
|
8
|
-
initialize_with_args :attribute_1, :attribute_2
|
9
|
-
is_callable
|
10
|
-
|
11
|
-
def call
|
12
|
-
[attribute_1, attribute_2]
|
13
|
-
end
|
14
|
-
end
|
15
|
-
|
16
|
-
class TestNoInit
|
17
|
-
extend SmartInit
|
18
|
-
is_callable
|
19
|
-
|
20
|
-
def call
|
21
|
-
'result'
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
class TestMethodName
|
26
|
-
extend SmartInit
|
27
|
-
|
28
|
-
is_callable method_name: :run!
|
29
|
-
|
30
|
-
def run!
|
31
|
-
true
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
def test_object
|
36
|
-
@_test_object ||= TestClass.new("attr_1_value", "attr_2_value")
|
37
|
-
end
|
38
|
-
|
39
|
-
class StandardApiTest < Test::Unit::TestCase
|
40
|
-
def test_number_of_attributes
|
41
|
-
assert_nothing_raised do
|
42
|
-
TestClass.new(
|
43
|
-
"attr_1_value",
|
44
|
-
"attr_2_value"
|
45
|
-
)
|
46
|
-
end
|
47
|
-
|
48
|
-
assert_raise ArgumentError do
|
49
|
-
TestClass.new(
|
50
|
-
"attr_1_value"
|
51
|
-
)
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
|
-
def test_instance_variables
|
56
|
-
assert_equal test_object.instance_variable_get("@attribute_1"), "attr_1_value"
|
57
|
-
end
|
58
|
-
|
59
|
-
def test_private_getters
|
60
|
-
error = assert_raise NoMethodError do
|
61
|
-
test_object.attribute_1
|
62
|
-
end
|
63
|
-
assert_match("private method", error.message)
|
64
|
-
|
65
|
-
assert_equal test_object.send(:attribute_1), "attr_1_value"
|
66
|
-
end
|
67
|
-
|
68
|
-
def test_is_callable
|
69
|
-
assert_equal TestClass.call("a", "b"), ["a", "b"]
|
70
|
-
end
|
71
|
-
|
72
|
-
def test_is_callable_no_initializers
|
73
|
-
assert_equal TestNoInit.call, 'result'
|
74
|
-
end
|
75
|
-
|
76
|
-
def test_is_callable_method_name
|
77
|
-
assert_equal TestMethodName.run!, true
|
78
|
-
end
|
79
|
-
end
|