smart_init 2.0.3 → 3.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.travis.yml +3 -4
- data/README.md +7 -11
- data/lib/smart_init/main.rb +21 -35
- data/lib/smart_init/version.rb +1 -1
- data/test/test_keywords_api.rb +16 -2
- data/test/{test_standard_api.rb → test_v1_api.rb} +1 -1
- metadata +4 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 8dee28a188d21cf07e256ca0fd7ef9be6200e7397226b4b4e26b7f995b94acf5
|
4
|
+
data.tar.gz: fef3f5bad20e6cf3df03dd27a69df4aa429d304098b1acc60426a00c0a8eafd0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d47fbddd944636439c176a32d5d62871669d5c9ffb505f7b6180e28bc52ac7f91bb9e7699c14378e89b811671ef3626f888ea84916e17ccde3daff96538e1255
|
7
|
+
data.tar.gz: 32751fb0cda8bc3713f57da5368ae8dde9dbf90ab7b95a1671b61eec17ed69de249d58fa116e4127260291bdeb34c62e962a690a9ee2970929bf1a7733376332
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -21,7 +21,7 @@ In your Gemfile
|
|
21
21
|
gem 'smart_init'
|
22
22
|
```
|
23
23
|
|
24
|
-
##
|
24
|
+
## API
|
25
25
|
|
26
26
|
You can use it either by extending a module:
|
27
27
|
|
@@ -29,7 +29,7 @@ You can use it either by extending a module:
|
|
29
29
|
class ApiClient
|
30
30
|
extend SmartInit
|
31
31
|
|
32
|
-
initialize_with :network_provider, api_token
|
32
|
+
initialize_with :network_provider, :api_token
|
33
33
|
end
|
34
34
|
|
35
35
|
```
|
@@ -38,7 +38,7 @@ or subclassing:
|
|
38
38
|
|
39
39
|
```ruby
|
40
40
|
class ApiClient < SmartInit::Base
|
41
|
-
initialize_with :network_provider, api_token
|
41
|
+
initialize_with :network_provider, :api_token
|
42
42
|
end
|
43
43
|
|
44
44
|
```
|
@@ -52,7 +52,6 @@ object = ApiClient.new(network_provider: Faraday.new, api_token: 'secret_token')
|
|
52
52
|
|
53
53
|
You can also use `is_callable` method:
|
54
54
|
|
55
|
-
|
56
55
|
```ruby
|
57
56
|
class Calculator < SmartInit::Base
|
58
57
|
initialize_with :data
|
@@ -69,10 +68,10 @@ Calculator.call(data: data) => result
|
|
69
68
|
|
70
69
|
### Default arguments
|
71
70
|
|
72
|
-
You can use
|
71
|
+
You can use hash based, default argument values:
|
73
72
|
|
74
73
|
```ruby
|
75
|
-
class
|
74
|
+
class Adder < SmartInit::Base
|
76
75
|
initialize_with :num_a, num_b: 2
|
77
76
|
is_callable
|
78
77
|
|
@@ -88,11 +87,11 @@ Adder.call(num_a: 2, num_b: 3) => 5
|
|
88
87
|
|
89
88
|
## Legacy API
|
90
89
|
|
91
|
-
Alternatively you can
|
90
|
+
Alternatively you can a legacy API without hash arguments and default values:
|
92
91
|
|
93
92
|
```ruby
|
94
93
|
class Calculator < SmartInit::Base
|
95
|
-
|
94
|
+
initialize_with_v1 :data
|
96
95
|
is_callable
|
97
96
|
|
98
97
|
def call
|
@@ -103,6 +102,3 @@ end
|
|
103
102
|
|
104
103
|
Calculator.call(data) => result
|
105
104
|
```
|
106
|
-
|
107
|
-
It does not support default argument values though.
|
108
|
-
|
data/lib/smart_init/main.rb
CHANGED
@@ -6,61 +6,47 @@ module SmartInit
|
|
6
6
|
end
|
7
7
|
|
8
8
|
def initialize_with *attributes
|
9
|
+
required_attrs = attributes.select { |attr| attr.is_a?(Symbol) }
|
10
|
+
default_value_attrs = attributes.select { |attr| attr.is_a?(Hash) }.first || {}
|
11
|
+
|
9
12
|
define_method :initialize do |*parameters|
|
10
|
-
if
|
11
|
-
raise ArgumentError, "wrong number of arguments (given #{parameters.count}, expected #{
|
13
|
+
if required_attrs.count > parameters.first.count
|
14
|
+
raise ArgumentError, "wrong number of arguments (given #{parameters.count}, expected at least #{required_attrs.count})"
|
12
15
|
end
|
13
16
|
|
14
|
-
|
15
|
-
|
16
|
-
value
|
17
|
-
instance_variable_set("@#{name}", value)
|
17
|
+
(required_attrs + default_value_attrs.keys).each do |attribute|
|
18
|
+
value = parameters.first[attribute] || default_value_attrs[attribute]
|
19
|
+
instance_variable_set("@#{attribute}", value)
|
18
20
|
end
|
19
21
|
end
|
20
22
|
|
21
23
|
instance_eval do
|
22
24
|
private
|
23
25
|
|
24
|
-
attr_reader *
|
26
|
+
attr_reader *(required_attrs + default_value_attrs.keys)
|
25
27
|
end
|
26
28
|
end
|
27
29
|
|
28
|
-
def
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
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
|
30
|
+
def initialize_with_v1 *attributes
|
31
|
+
define_method :initialize do |*parameters|
|
32
|
+
if attributes.count != parameters.count
|
33
|
+
raise ArgumentError, "wrong number of arguments (given #{parameters.count}, expected #{attributes.count})"
|
34
|
+
end
|
48
35
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
)
|
54
|
-
end
|
36
|
+
attributes.zip(parameters).each do |pair|
|
37
|
+
name = pair[0]
|
38
|
+
value = pair[1]
|
39
|
+
instance_variable_set("@#{name}", value)
|
55
40
|
end
|
56
|
-
|
41
|
+
end
|
57
42
|
|
58
43
|
instance_eval do
|
59
44
|
private
|
60
45
|
|
61
|
-
attr_reader *
|
46
|
+
attr_reader *attributes
|
62
47
|
end
|
63
48
|
end
|
49
|
+
|
64
50
|
end
|
65
51
|
|
66
52
|
class SmartInit::Base
|
data/lib/smart_init/version.rb
CHANGED
data/test/test_keywords_api.rb
CHANGED
@@ -3,7 +3,7 @@ require_relative '../lib/smart_init/main'
|
|
3
3
|
|
4
4
|
class TestKeywords
|
5
5
|
extend SmartInit
|
6
|
-
|
6
|
+
initialize_with :attribute_1, :attribute_2
|
7
7
|
is_callable
|
8
8
|
|
9
9
|
def call
|
@@ -13,7 +13,7 @@ end
|
|
13
13
|
|
14
14
|
class TestKeywordsDefaults
|
15
15
|
extend SmartInit
|
16
|
-
|
16
|
+
initialize_with :attribute_1, attribute_2: "default_value_2", attribute_3: "default_value_3"
|
17
17
|
is_callable
|
18
18
|
|
19
19
|
def call
|
@@ -21,6 +21,16 @@ class TestKeywordsDefaults
|
|
21
21
|
end
|
22
22
|
end
|
23
23
|
|
24
|
+
class TestKeywordsIntegerDefaults
|
25
|
+
extend SmartInit
|
26
|
+
initialize_with :attribute_1, attribute_2: 2
|
27
|
+
is_callable
|
28
|
+
|
29
|
+
def call
|
30
|
+
[attribute_1, attribute_2]
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
24
34
|
class KeywordsApiTest < Test::Unit::TestCase
|
25
35
|
def test_keywords
|
26
36
|
assert_equal TestKeywords.call(attribute_1: "a", attribute_2: "b"), ["a", "b"]
|
@@ -37,6 +47,10 @@ class KeywordsApiTest < Test::Unit::TestCase
|
|
37
47
|
assert_equal TestKeywordsDefaults.call(attribute_1: "a", attribute_2: "b"), ["a", "b", "default_value_3"]
|
38
48
|
end
|
39
49
|
|
50
|
+
def test_integer_defaults
|
51
|
+
assert_equal TestKeywordsIntegerDefaults.call(attribute_1: 1), [1, 2]
|
52
|
+
end
|
53
|
+
|
40
54
|
private
|
41
55
|
|
42
56
|
def test_object
|
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: 3.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: 2019-01-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -56,7 +56,7 @@ files:
|
|
56
56
|
- lib/smart_init/version.rb
|
57
57
|
- smart_init.gemspec
|
58
58
|
- test/test_keywords_api.rb
|
59
|
-
- test/
|
59
|
+
- test/test_v1_api.rb
|
60
60
|
homepage: http://github.com/pawurb/smart_init
|
61
61
|
licenses:
|
62
62
|
- MIT
|
@@ -76,8 +76,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
76
76
|
- !ruby/object:Gem::Version
|
77
77
|
version: '0'
|
78
78
|
requirements: []
|
79
|
-
|
80
|
-
rubygems_version: 2.6.14
|
79
|
+
rubygems_version: 3.0.0
|
81
80
|
signing_key:
|
82
81
|
specification_version: 4
|
83
82
|
summary: Remove Ruby initializer boilerplate code
|