safety_check 0.0.1 → 0.0.2
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/lib/safety_check.rb +8 -6
- data/lib/version.rb +1 -1
- data/spec/safety_check/safety_check_spec.rb +68 -2
- data/spec/test_models/hello.rb +32 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b69b29a68b53c6b95b6a3f9a5d7698648e48420e
|
4
|
+
data.tar.gz: 962d6867ababa327165ec0a635fa670d57051202
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e304f523ddfca6d916e3f1037bc51475d21aff4686559e1f765a0e58c7074ea6eb33dccbc1a5c37a31368d133f1daac307b09dda10367eb955eb12920ba65533
|
7
|
+
data.tar.gz: 152629295a966098a9e985bcaf70bf1ff543d97be3785daa48381c4733ccd9669487470d13f2b65b9739e6d57d3f90d1e1634397d7758da744c0d1af17102c2b
|
data/lib/safety_check.rb
CHANGED
@@ -15,24 +15,26 @@ module SafetyCheck
|
|
15
15
|
|
16
16
|
unsafe_method_name = "#{method_name.to_s}_without_type_safety".to_sym
|
17
17
|
|
18
|
-
|
19
|
-
|
18
|
+
required_arity = receiver.instance_method(method_name).parameters.select { |type, name| type == :req }.length
|
19
|
+
|
20
|
+
if arg_types.length < required_arity
|
21
|
+
raise ArgumentError, "wrong number of arguments for #{method_name.inspect} (#{arg_types.length} for #{required_arity})"
|
20
22
|
end
|
21
23
|
|
22
24
|
receiver.send(:alias_method, unsafe_method_name, method_name)
|
23
25
|
|
24
|
-
receiver.send(:define_method, method_name) do |*args|
|
25
|
-
if args.length
|
26
|
+
receiver.send(:define_method, method_name) do |*args, &block|
|
27
|
+
if args.length < required_arity
|
26
28
|
raise ArgumentError, "wrong number of arguments (#{args.length} for #{arg_types.length}) when calling #{method_name.inspect}"
|
27
29
|
end
|
28
30
|
|
29
|
-
|
31
|
+
args.zip(arg_types).each do |arg, arg_type|
|
30
32
|
unless arg.is_a? arg_type
|
31
33
|
raise ArgumentError, "expected #{arg.inspect} to be a #{arg_type} when calling #{method_name.inspect}"
|
32
34
|
end
|
33
35
|
end
|
34
36
|
|
35
|
-
self.send(unsafe_method_name, *args)
|
37
|
+
self.send(unsafe_method_name, *args, &block)
|
36
38
|
end
|
37
39
|
end
|
38
40
|
end
|
data/lib/version.rb
CHANGED
@@ -5,7 +5,7 @@ describe SafetyCheck do
|
|
5
5
|
it 'instance methods should not raise an error when the correct type of argument is used' do
|
6
6
|
hi = Hello.new
|
7
7
|
|
8
|
-
expect(hi.greetings('world')).to eq(
|
8
|
+
expect(hi.greetings('world')).to eq('Hello, world!')
|
9
9
|
end
|
10
10
|
|
11
11
|
it 'instance methods should raise an error when the wrong type of argument is used' do
|
@@ -20,8 +20,46 @@ describe SafetyCheck do
|
|
20
20
|
expect { hi.greetings('world', 'lol') }.to raise_error
|
21
21
|
end
|
22
22
|
|
23
|
+
it 'instance methods should not raise an error if optional arguments are ommited' do
|
24
|
+
hi = Hello.new
|
25
|
+
|
26
|
+
expect(hi.moshi_moshi('world')).to eq('Hello from Japan, world!')
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'instance methods should not raise an error when the correct type of argument is used for optional arguments' do
|
30
|
+
hi = Hello.new
|
31
|
+
|
32
|
+
expect(hi.moshi_moshi('world', 'Sweden')).to eq('Hello from Sweden, world!')
|
33
|
+
end
|
34
|
+
|
35
|
+
it 'instance methods should raise an error when the wrong type of argument is used for optional arguments' do
|
36
|
+
hi = Hello.new
|
37
|
+
|
38
|
+
expect { hi.moshi_moshi('world', 5) }.to raise_error
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'instance methods should not raise an error when the correct type of argument is used and a block is passed' do
|
42
|
+
hi = Hello.new
|
43
|
+
|
44
|
+
expect(
|
45
|
+
hi.hallo('world') do |subject|
|
46
|
+
"what a wonderful #{subject}"
|
47
|
+
end
|
48
|
+
).to eq('Hi, what a wonderful world!')
|
49
|
+
end
|
50
|
+
|
51
|
+
it 'instance methods should raise an error when the wrong type of argument is used and a block is passed' do
|
52
|
+
hi = Hello.new
|
53
|
+
|
54
|
+
expect do
|
55
|
+
hi.hallo(5) do |subject|
|
56
|
+
"what a wonderful #{subject}"
|
57
|
+
end
|
58
|
+
end.to raise_error
|
59
|
+
end
|
60
|
+
|
23
61
|
it 'class methods should not raise an error when the correct type of argument is used' do
|
24
|
-
expect(Hello.salutations('world')).to eq(
|
62
|
+
expect(Hello.salutations('world')).to eq('Good day, world!')
|
25
63
|
end
|
26
64
|
|
27
65
|
it 'class methods should raise an error when the wrong type of argument is used' do
|
@@ -31,5 +69,33 @@ describe SafetyCheck do
|
|
31
69
|
it 'class methods should raise an error when the wrong number of argument is used' do
|
32
70
|
expect { Hello.salutations('world', 'lol') }.to raise_error
|
33
71
|
end
|
72
|
+
|
73
|
+
it 'class methods should not raise an error if optional arguments are ommited' do
|
74
|
+
expect(Hello.ohayou_gozaimasu('world')).to eq('Good morning from Japan, world!')
|
75
|
+
end
|
76
|
+
|
77
|
+
it 'class methods should not raise an error when the correct type of argument is used for optional arguments' do
|
78
|
+
expect(Hello.ohayou_gozaimasu('world', 'Sweden')).to eq('Good morning from Sweden, world!')
|
79
|
+
end
|
80
|
+
|
81
|
+
it 'class methods should raise an error when the wrong type of argument is used for optional arguments' do
|
82
|
+
expect { Hello.ohayou_gozaimasu('world', 5) }.to raise_error
|
83
|
+
end
|
84
|
+
|
85
|
+
it 'class methods should not raise an error when the correct type of argument is used and a block is passed' do
|
86
|
+
expect(
|
87
|
+
Hello.guten_morgen('world') do |subject|
|
88
|
+
"from a distant #{subject}"
|
89
|
+
end
|
90
|
+
).to eq('Good morning, from a distant world!')
|
91
|
+
end
|
92
|
+
|
93
|
+
it 'class methods should raise an error when the wrong type of argument is used and a block is passed' do
|
94
|
+
expect do
|
95
|
+
Hello.guten_morgen(5) do |subject|
|
96
|
+
"from a distant #{subject}"
|
97
|
+
end
|
98
|
+
end.to raise_error
|
99
|
+
end
|
34
100
|
end
|
35
101
|
end
|
data/spec/test_models/hello.rb
CHANGED
@@ -6,8 +6,40 @@ class Hello
|
|
6
6
|
end
|
7
7
|
safety_check :greetings, String
|
8
8
|
|
9
|
+
def moshi_moshi(subject, location = '')
|
10
|
+
if location == ''
|
11
|
+
"Hello from Japan, #{subject}!"
|
12
|
+
else
|
13
|
+
"Hello from #{location}, #{subject}!"
|
14
|
+
end
|
15
|
+
end
|
16
|
+
safety_check :moshi_moshi, String, String
|
17
|
+
|
18
|
+
def hallo(subject)
|
19
|
+
new_subject = yield subject
|
20
|
+
|
21
|
+
"Hi, #{new_subject}!"
|
22
|
+
end
|
23
|
+
safety_check :hallo, String
|
24
|
+
|
9
25
|
def self.salutations(subject)
|
10
26
|
"Good day, #{subject}!"
|
11
27
|
end
|
12
28
|
safety_check :salutations, String
|
29
|
+
|
30
|
+
def self.ohayou_gozaimasu(subject, location = '')
|
31
|
+
if location == ''
|
32
|
+
"Good morning from Japan, #{subject}!"
|
33
|
+
else
|
34
|
+
"Good morning from #{location}, #{subject}!"
|
35
|
+
end
|
36
|
+
end
|
37
|
+
safety_check :ohayou_gozaimasu, String, String
|
38
|
+
|
39
|
+
def self.guten_morgen(subject)
|
40
|
+
new_subject = yield subject
|
41
|
+
|
42
|
+
"Good morning, #{new_subject}!"
|
43
|
+
end
|
44
|
+
safety_check :guten_morgen, String
|
13
45
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: safety_check
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sean Eshbaugh
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-07-
|
11
|
+
date: 2014-07-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|