smart_init 3.0.0 → 3.1.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 +2 -0
- data/lib/smart_init/main.rb +6 -5
- data/lib/smart_init/version.rb +1 -1
- data/test/{test_keywords_api.rb → test_api.rb} +13 -13
- 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: 7c160db7dd46c5dc77e00e49404e2354519b12ef5200265160586a57ae9fc711
|
4
|
+
data.tar.gz: e378b890b6cc8c3c86301d227405ac80f14176b3d3a7e1b673578e4ba7cdcca4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8d6ad48780eac79e681a956709af68dcda5f51c15796c74df575fe233df25f9f5c1817c2feea9a16b17f131d6228d0f7e3d3394676c4a73e891e0e823848d82d
|
7
|
+
data.tar.gz: a47ab03b8918c0b34e7efec5200dc2f3831a47571ca888b642cd709db52ad13831ebd9af8beecb55cc29fa8f94a6024ab886c3589e4cb6bc4156a4645d231866
|
data/README.md
CHANGED
data/lib/smart_init/main.rb
CHANGED
@@ -10,8 +10,10 @@ module SmartInit
|
|
10
10
|
default_value_attrs = attributes.select { |attr| attr.is_a?(Hash) }.first || {}
|
11
11
|
|
12
12
|
define_method :initialize do |*parameters|
|
13
|
-
|
14
|
-
|
13
|
+
required_attrs.each do |required_attr|
|
14
|
+
unless parameters.first.has_key?(required_attr)
|
15
|
+
raise ArgumentError, "missing required attribute #{required_attr}"
|
16
|
+
end
|
15
17
|
end
|
16
18
|
|
17
19
|
(required_attrs + default_value_attrs.keys).each do |attribute|
|
@@ -23,7 +25,7 @@ module SmartInit
|
|
23
25
|
instance_eval do
|
24
26
|
private
|
25
27
|
|
26
|
-
attr_reader
|
28
|
+
attr_reader(*(required_attrs + default_value_attrs.keys).compact)
|
27
29
|
end
|
28
30
|
end
|
29
31
|
|
@@ -43,10 +45,9 @@ module SmartInit
|
|
43
45
|
instance_eval do
|
44
46
|
private
|
45
47
|
|
46
|
-
attr_reader
|
48
|
+
attr_reader(*attributes)
|
47
49
|
end
|
48
50
|
end
|
49
|
-
|
50
51
|
end
|
51
52
|
|
52
53
|
class SmartInit::Base
|
data/lib/smart_init/version.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
require "test/unit"
|
2
2
|
require_relative '../lib/smart_init/main'
|
3
3
|
|
4
|
-
class
|
4
|
+
class TestService
|
5
5
|
extend SmartInit
|
6
6
|
initialize_with :attribute_1, :attribute_2
|
7
7
|
is_callable
|
@@ -11,7 +11,7 @@ class TestKeywords
|
|
11
11
|
end
|
12
12
|
end
|
13
13
|
|
14
|
-
class
|
14
|
+
class TestServiceDefaults
|
15
15
|
extend SmartInit
|
16
16
|
initialize_with :attribute_1, attribute_2: "default_value_2", attribute_3: "default_value_3"
|
17
17
|
is_callable
|
@@ -21,7 +21,7 @@ class TestKeywordsDefaults
|
|
21
21
|
end
|
22
22
|
end
|
23
23
|
|
24
|
-
class
|
24
|
+
class TestHashIntegerDefaults
|
25
25
|
extend SmartInit
|
26
26
|
initialize_with :attribute_1, attribute_2: 2
|
27
27
|
is_callable
|
@@ -31,29 +31,29 @@ class TestKeywordsIntegerDefaults
|
|
31
31
|
end
|
32
32
|
end
|
33
33
|
|
34
|
-
class
|
34
|
+
class HashApiTest < Test::Unit::TestCase
|
35
35
|
def test_keywords
|
36
|
-
assert_equal
|
36
|
+
assert_equal TestService.call(attribute_1: "a", attribute_2: "b"), ["a", "b"]
|
37
37
|
|
38
38
|
assert_raise ArgumentError do
|
39
|
-
|
39
|
+
TestService.new(
|
40
40
|
attribute_1: "a"
|
41
41
|
)
|
42
42
|
end
|
43
43
|
end
|
44
44
|
|
45
45
|
def test_keywords_defaults
|
46
|
-
assert_equal
|
47
|
-
assert_equal
|
46
|
+
assert_equal TestServiceDefaults.call(attribute_1: "a"), ["a", "default_value_2", "default_value_3"]
|
47
|
+
assert_equal TestServiceDefaults.call(attribute_1: "a", attribute_2: "b"), ["a", "b", "default_value_3"]
|
48
48
|
end
|
49
49
|
|
50
50
|
def test_integer_defaults
|
51
|
-
assert_equal
|
51
|
+
assert_equal TestHashIntegerDefaults.call(attribute_1: 1), [1, 2]
|
52
52
|
end
|
53
53
|
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
54
|
+
def test_missing_attributes
|
55
|
+
assert_raise ArgumentError do
|
56
|
+
TestService.call(attribute_1: "a", invalid_attribute: "b")
|
57
|
+
end
|
58
58
|
end
|
59
59
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: smart_init
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.
|
4
|
+
version: 3.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- pawurb
|
@@ -55,7 +55,7 @@ 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_api.rb
|
59
59
|
- test/test_v1_api.rb
|
60
60
|
homepage: http://github.com/pawurb/smart_init
|
61
61
|
licenses:
|