smart_init 3.3.0 → 3.4.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/.travis.yml +1 -1
- data/README.md +12 -3
- data/lib/smart_init/main.rb +1 -0
- data/lib/smart_init/version.rb +1 -1
- data/test/test_hash_api.rb +14 -0
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: '0892f8aa5ed6cba8c888e4a44e67ab3b7fd95fae4648c227593d3ce517fa07f4'
|
4
|
+
data.tar.gz: 927c461dfc531d9b21d365609b2f86be35a14102fde2bba092f672d8d55a9d16
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3de078331f718dc999244841c6e3b2e0321af77e43352fde4593c2b08f45e680f98685480fe7455192d9b2d44e804f610ef3ddd588b810700750d116a3c9b1b6
|
7
|
+
data.tar.gz: 6523291cb5c8b9a9af044b82ff85c6b2d3c0814a25d506ed2c1d8502b355ba15e33211cd0ebfc45bc859848b7e571a8e83668d5f4d30194970f2c3e452170836
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -15,7 +15,7 @@ end
|
|
15
15
|
|
16
16
|
Gem provides a simple DSL for getting rid of it. It offers an alternative to using `Struct.new` which does not check for number of parameters provided in initializer, exposes getters and instantiates unecessary class instances.
|
17
17
|
|
18
|
-
**Smart Init** offers a unified
|
18
|
+
**Smart Init** offers a unified API convention for stateless service objects, accepting values in initializer and exposing one public class method `call` which instantiates new objects and accepts arguments passed to initializer.
|
19
19
|
|
20
20
|
Check out [this blog post](https://pawelurbanek.com/2018/02/12/ruby-on-rails-service-objects-and-testing-in-isolation/) for my reasoning behind this approach to service object pattern.
|
21
21
|
|
@@ -64,6 +64,15 @@ object = ApiClient.new(network_provider: Faraday.new)
|
|
64
64
|
# ArgumentError (missing required attribute api_token)
|
65
65
|
```
|
66
66
|
|
67
|
+
Contrary to using Struct, reader methods are not publicly exposed:
|
68
|
+
|
69
|
+
```ruby
|
70
|
+
object = ApiClient.new(network_provider: Faraday.new, api_token: 'secret_token')
|
71
|
+
object.api_token
|
72
|
+
|
73
|
+
# NoMethodError (private method `api_token' called for #<ApiClient:0x00007fe911876a48>)
|
74
|
+
```
|
75
|
+
|
67
76
|
You can also use `is_callable` method:
|
68
77
|
|
69
78
|
```ruby
|
@@ -114,9 +123,9 @@ Adder.call(num_a: 2, num_b: 3) => 5
|
|
114
123
|
|
115
124
|
```
|
116
125
|
|
117
|
-
##
|
126
|
+
## Arguments API
|
118
127
|
|
119
|
-
Alternatively you can
|
128
|
+
Alternatively you can use an API without hash arguments and default values:
|
120
129
|
|
121
130
|
```ruby
|
122
131
|
class Calculator < SmartInit::Base
|
data/lib/smart_init/main.rb
CHANGED
@@ -16,6 +16,7 @@ module SmartInit
|
|
16
16
|
default_value_attrs = attributes.select { |attr| attr.is_a?(Hash) }.first || {}
|
17
17
|
|
18
18
|
define_method :initialize do |*parameters|
|
19
|
+
parameters = [{}] if parameters == []
|
19
20
|
unless parameters.first.is_a?(Hash)
|
20
21
|
raise ArgumentError, "invalid input, expected hash of attributes"
|
21
22
|
end
|
data/lib/smart_init/version.rb
CHANGED
data/test/test_hash_api.rb
CHANGED
@@ -21,6 +21,16 @@ class TestServiceDefaults
|
|
21
21
|
end
|
22
22
|
end
|
23
23
|
|
24
|
+
class TestServiceAllDefaults
|
25
|
+
extend SmartInit
|
26
|
+
initialize_with attribute_1: "default_value_1", attribute_2: "default_value_2", attribute_3: "default_value_3"
|
27
|
+
is_callable
|
28
|
+
|
29
|
+
def call
|
30
|
+
[attribute_1, attribute_2, attribute_3]
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
24
34
|
class TestHashIntegerDefaults
|
25
35
|
extend SmartInit
|
26
36
|
initialize_with :attribute_1, attribute_2: 2
|
@@ -62,4 +72,8 @@ class HashApiTest < Test::Unit::TestCase
|
|
62
72
|
TestService.call("invalid_input here")
|
63
73
|
end
|
64
74
|
end
|
75
|
+
|
76
|
+
def test_all_defaults
|
77
|
+
assert_equal TestServiceAllDefaults.call, ["default_value_1", "default_value_2", "default_value_3"]
|
78
|
+
end
|
65
79
|
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.
|
4
|
+
version: 3.4.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-
|
11
|
+
date: 2019-11-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -76,7 +76,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
76
76
|
- !ruby/object:Gem::Version
|
77
77
|
version: '0'
|
78
78
|
requirements: []
|
79
|
-
rubygems_version: 3.0.
|
79
|
+
rubygems_version: 3.0.3
|
80
80
|
signing_key:
|
81
81
|
specification_version: 4
|
82
82
|
summary: Remove Ruby initializer boilerplate code
|