smart_init 1.1.0 → 2.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/.gitignore +2 -0
- data/.travis.yml +1 -1
- data/README.md +47 -5
- data/lib/smart_init/main.rb +37 -0
- data/lib/smart_init/version.rb +1 -1
- data/test/test_keywords_api.rb +46 -0
- data/test/{test_smart_init.rb → test_standard_api.rb} +7 -10
- metadata +5 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b2c532785dd60c1023dcf402b5ae50bc61bdbb3d
|
4
|
+
data.tar.gz: f5df63f9c507164ac2d01065982a8b95743ff63e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 158fcaf311d75218bfe9ce6c551c3c18708527c38fc134baabd992af737ec30f4059663abac00b32689c603025f33a03b362c300a50a2a1e683016f7951b8f67
|
7
|
+
data.tar.gz: ba253f3ecccd16e07ecca148fc05bf0a51616aed0821e05aaf67d51ef63cd453f9cae9fe76245ff59aeeb4b6b5178c24b3c6532666d03dcba97350403c2cc754
|
data/.gitignore
CHANGED
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
Do you find yourself writing a lot of boilerplate code like that?
|
4
4
|
|
5
|
-
```
|
5
|
+
```ruby
|
6
6
|
def initialize(network_provider, api_token)
|
7
7
|
@network_provider = network_provider
|
8
8
|
@api_token = api_token
|
@@ -11,6 +11,8 @@ end
|
|
11
11
|
|
12
12
|
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
13
|
|
14
|
+
**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
|
+
|
14
16
|
## Installation
|
15
17
|
|
16
18
|
In your Gemfile
|
@@ -19,7 +21,9 @@ In your Gemfile
|
|
19
21
|
gem 'smart_init'
|
20
22
|
```
|
21
23
|
|
22
|
-
|
24
|
+
## Keyword arguments API
|
25
|
+
|
26
|
+
You can use it either by extending a module:
|
23
27
|
|
24
28
|
```ruby
|
25
29
|
class ApiClient
|
@@ -42,7 +46,7 @@ end
|
|
42
46
|
Now you can just:
|
43
47
|
|
44
48
|
```ruby
|
45
|
-
object = ApiClient.new(Faraday.new, 'secret_token')
|
49
|
+
object = ApiClient.new(network_provider: Faraday.new, api_token: 'secret_token')
|
46
50
|
# <ApiClient:0x007fa16684ec20 @network_provider=Faraday<...>, @api_token="secret_token">
|
47
51
|
```
|
48
52
|
|
@@ -60,8 +64,46 @@ class Calculator < SmartInit::Base
|
|
60
64
|
end
|
61
65
|
end
|
62
66
|
|
63
|
-
Calculator.call(data) =>
|
67
|
+
Calculator.call(data: data) => result
|
68
|
+
```
|
69
|
+
|
70
|
+
### Default arguments
|
71
|
+
|
72
|
+
You can use keyword based, default argument values:
|
73
|
+
|
74
|
+
```ruby
|
75
|
+
class Added < SmartInit::Base
|
76
|
+
initialize_with :num_a, num_b: 2
|
77
|
+
is_callable
|
78
|
+
|
79
|
+
def call
|
80
|
+
num_a + num_b
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
Adder.call(num_a: 2) => 4
|
85
|
+
Adder.call(num_a: 2, num_b: 3) => 5
|
86
|
+
|
64
87
|
```
|
65
88
|
|
66
|
-
|
89
|
+
## Legacy API
|
90
|
+
|
91
|
+
Alternatively you can use standard API without keyword arguments:
|
92
|
+
|
93
|
+
```ruby
|
94
|
+
class Calculator < SmartInit::Base
|
95
|
+
initialize_with :data
|
96
|
+
is_callable
|
97
|
+
|
98
|
+
def call
|
99
|
+
...
|
100
|
+
result
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
Calculator.call(data) => result
|
105
|
+
```
|
106
|
+
|
107
|
+
This API does not support default argument values though.
|
108
|
+
|
67
109
|
|
data/lib/smart_init/main.rb
CHANGED
@@ -24,6 +24,43 @@ module SmartInit
|
|
24
24
|
attr_reader *attributes
|
25
25
|
end
|
26
26
|
end
|
27
|
+
|
28
|
+
def initialize_with_keywords *attributes
|
29
|
+
class_variable_set(:@@_attributes, attributes)
|
30
|
+
@@_attributes = attributes
|
31
|
+
|
32
|
+
required_attrs = attributes.select { |attr| attr.is_a?(Symbol) }
|
33
|
+
default_value_attrs = attributes.select { |attr| attr.is_a?(Hash) }.first || {}
|
34
|
+
|
35
|
+
class_variable_set(:@@_required_attrs, required_attrs)
|
36
|
+
class_variable_set(:@@_default_value_attrs, default_value_attrs)
|
37
|
+
@@_required_attrs = required_attrs
|
38
|
+
@@_default_value_attrs = default_value_attrs
|
39
|
+
|
40
|
+
class_eval <<-METHOD
|
41
|
+
def initialize(#{(@@_required_attrs + @@_default_value_attrs.keys).compact.map { |a| @@_default_value_attrs[a] ? "#{a.to_s}: '#{@@_default_value_attrs.fetch(a)}'" : "#{a}:" }.join(', ')})
|
42
|
+
@@_required_attrs&.each do |attribute|
|
43
|
+
instance_variable_set(
|
44
|
+
"@"+ attribute.to_s,
|
45
|
+
eval(attribute.to_s)
|
46
|
+
)
|
47
|
+
end
|
48
|
+
|
49
|
+
@@_default_value_attrs&.keys.each do |attribute|
|
50
|
+
instance_variable_set(
|
51
|
+
"@"+ attribute.to_s,
|
52
|
+
eval(attribute.to_s)
|
53
|
+
)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
METHOD
|
57
|
+
|
58
|
+
instance_eval do
|
59
|
+
private
|
60
|
+
|
61
|
+
attr_reader *(required_attrs + default_value_attrs&.keys)
|
62
|
+
end
|
63
|
+
end
|
27
64
|
end
|
28
65
|
|
29
66
|
class SmartInit::Base
|
data/lib/smart_init/version.rb
CHANGED
@@ -0,0 +1,46 @@
|
|
1
|
+
require "test/unit"
|
2
|
+
require "byebug"
|
3
|
+
require_relative '../lib/smart_init/main'
|
4
|
+
|
5
|
+
class TestKeywords
|
6
|
+
extend SmartInit
|
7
|
+
initialize_with_keywords :attribute_1, :attribute_2
|
8
|
+
is_callable
|
9
|
+
|
10
|
+
def call
|
11
|
+
[attribute_1, attribute_2]
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
class TestKeywordsDefaults
|
16
|
+
extend SmartInit
|
17
|
+
initialize_with_keywords :attribute_1, attribute_2: "default_value_2", attribute_3: "default_value_3"
|
18
|
+
is_callable
|
19
|
+
|
20
|
+
def call
|
21
|
+
[attribute_1, attribute_2, attribute_3]
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
class KeywordsApiTest < Test::Unit::TestCase
|
26
|
+
def test_keywords
|
27
|
+
assert_equal TestKeywords.call(attribute_1: "a", attribute_2: "b"), ["a", "b"]
|
28
|
+
|
29
|
+
assert_raise ArgumentError do
|
30
|
+
TestKeywords.new(
|
31
|
+
attribute_1: "a"
|
32
|
+
)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_keywords_defaults
|
37
|
+
assert_equal TestKeywordsDefaults.call(attribute_1: "a"), ["a", "default_value_2", "default_value_3"]
|
38
|
+
assert_equal TestKeywordsDefaults.call(attribute_1: "a", attribute_2: "b"), ["a", "b", "default_value_3"]
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
|
43
|
+
def test_object
|
44
|
+
@_test_object ||= TestClass.new("attr_1_value", "attr_2_value")
|
45
|
+
end
|
46
|
+
end
|
@@ -1,5 +1,6 @@
|
|
1
1
|
require "test/unit"
|
2
|
-
require
|
2
|
+
require "byebug"
|
3
|
+
require_relative '../lib/smart_init/main'
|
3
4
|
|
4
5
|
class TestClass
|
5
6
|
extend SmartInit
|
@@ -20,7 +21,11 @@ class TestNoInit
|
|
20
21
|
end
|
21
22
|
end
|
22
23
|
|
23
|
-
|
24
|
+
def test_object
|
25
|
+
@_test_object ||= TestClass.new("attr_1_value", "attr_2_value")
|
26
|
+
end
|
27
|
+
|
28
|
+
class StandardApiTest < Test::Unit::TestCase
|
24
29
|
def test_number_of_attributes
|
25
30
|
assert_nothing_raised do
|
26
31
|
TestClass.new(
|
@@ -55,12 +60,4 @@ class SmartInitTest < Test::Unit::TestCase
|
|
55
60
|
def test_is_callable_no_initializers
|
56
61
|
assert_equal TestNoInit.call, 'result'
|
57
62
|
end
|
58
|
-
|
59
|
-
private
|
60
|
-
|
61
|
-
def test_object
|
62
|
-
@_test_object ||= TestClass.new("attr_1_value", "attr_2_value")
|
63
|
-
end
|
64
63
|
end
|
65
|
-
|
66
|
-
|
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: 2.0.0
|
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: 2018-09-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -55,7 +55,8 @@ files:
|
|
55
55
|
- lib/smart_init/main.rb
|
56
56
|
- lib/smart_init/version.rb
|
57
57
|
- smart_init.gemspec
|
58
|
-
- test/
|
58
|
+
- test/test_keywords_api.rb
|
59
|
+
- test/test_standard_api.rb
|
59
60
|
homepage: http://github.com/pawurb/smart_init
|
60
61
|
licenses:
|
61
62
|
- MIT
|
@@ -76,7 +77,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
76
77
|
version: '0'
|
77
78
|
requirements: []
|
78
79
|
rubyforge_project:
|
79
|
-
rubygems_version: 2.6.
|
80
|
+
rubygems_version: 2.6.14
|
80
81
|
signing_key:
|
81
82
|
specification_version: 4
|
82
83
|
summary: Remove Ruby initializer boilerplate code
|