smart_init 3.2.0 → 3.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +21 -0
- data/lib/smart_init/main.rb +9 -3
- data/lib/smart_init/version.rb +1 -1
- data/test/test_args_api.rb +14 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6348b5da71d03671ec74c8a43d2400e84076b4510fae3cb61f1db3da92752ba9
|
4
|
+
data.tar.gz: 95879658e1ff5b439206ad61c8472659d4331e058e1841d83bef56eaea3eb254
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3239515e6cfee5cfc1a1be8b12d6b4799ede9e8ca020f4d7c8ee38d8ab6f2e61d80c9d1f86563c5302779f47d232dda0cd44db8a5164439522dfbd33abc450b2
|
7
|
+
data.tar.gz: 60abdec0ed7227d03bdc6d1c93d955202ea64ce31a6ade006a0b0c9a3f25c547bda222d0c8a349242789be5e8d1b70609f9a29ea11855cbb8b3981dce1d57d6c
|
data/README.md
CHANGED
@@ -7,12 +7,18 @@ def initialize(network_provider, api_token)
|
|
7
7
|
@network_provider = network_provider
|
8
8
|
@api_token = api_token
|
9
9
|
end
|
10
|
+
|
11
|
+
def self.call(network_provider, api_token)
|
12
|
+
new(network_provider, api_token).call
|
13
|
+
end
|
10
14
|
```
|
11
15
|
|
12
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.
|
13
17
|
|
14
18
|
**Smart Init** offers a unified api 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.
|
15
19
|
|
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
|
+
|
16
22
|
## Installation
|
17
23
|
|
18
24
|
In your Gemfile
|
@@ -74,6 +80,21 @@ end
|
|
74
80
|
Calculator.call(data: data) => result
|
75
81
|
```
|
76
82
|
|
83
|
+
Optionally you can customize a callable method name:
|
84
|
+
|
85
|
+
```ruby
|
86
|
+
class Routine < SmartInit::Base
|
87
|
+
initialize_with :params
|
88
|
+
is_callable method_name: :run!
|
89
|
+
|
90
|
+
def run!
|
91
|
+
...
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
Routine.run!(params: params)
|
96
|
+
```
|
97
|
+
|
77
98
|
### Default arguments
|
78
99
|
|
79
100
|
You can use hash based, default argument values:
|
data/lib/smart_init/main.rb
CHANGED
@@ -1,7 +1,13 @@
|
|
1
1
|
module SmartInit
|
2
|
-
def is_callable
|
3
|
-
|
4
|
-
|
2
|
+
def is_callable(opts={})
|
3
|
+
method_name = if name_from_opts = opts[:method_name]
|
4
|
+
name_from_opts.to_sym
|
5
|
+
else
|
6
|
+
:call
|
7
|
+
end
|
8
|
+
|
9
|
+
define_singleton_method method_name do |*parameters|
|
10
|
+
new(*parameters).public_send(method_name)
|
5
11
|
end
|
6
12
|
end
|
7
13
|
|
data/lib/smart_init/version.rb
CHANGED
data/test/test_args_api.rb
CHANGED
@@ -20,6 +20,16 @@ class TestNoInit
|
|
20
20
|
end
|
21
21
|
end
|
22
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
|
+
|
23
33
|
def test_object
|
24
34
|
@_test_object ||= TestClass.new("attr_1_value", "attr_2_value")
|
25
35
|
end
|
@@ -59,4 +69,8 @@ class StandardApiTest < Test::Unit::TestCase
|
|
59
69
|
def test_is_callable_no_initializers
|
60
70
|
assert_equal TestNoInit.call, 'result'
|
61
71
|
end
|
72
|
+
|
73
|
+
def test_is_callable_method_name
|
74
|
+
assert_equal TestMethodName.run!, true
|
75
|
+
end
|
62
76
|
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.3.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-01-
|
11
|
+
date: 2019-01-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|