safety_check 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b69b29a68b53c6b95b6a3f9a5d7698648e48420e
4
- data.tar.gz: 962d6867ababa327165ec0a635fa670d57051202
3
+ metadata.gz: edbdc93bd92869005603ec859d9057d964734650
4
+ data.tar.gz: 6ecd8ba86c8544e48b8f8490f2590f6704b602fc
5
5
  SHA512:
6
- metadata.gz: e304f523ddfca6d916e3f1037bc51475d21aff4686559e1f765a0e58c7074ea6eb33dccbc1a5c37a31368d133f1daac307b09dda10367eb955eb12920ba65533
7
- data.tar.gz: 152629295a966098a9e985bcaf70bf1ff543d97be3785daa48381c4733ccd9669487470d13f2b65b9739e6d57d3f90d1e1634397d7758da744c0d1af17102c2b
6
+ metadata.gz: ba08ad29c688e332933173c37559719db769ee72eb84a8d4bb63298dd751cdb08b46f557ab235e304b3f139147d3b8ca628bde23ca9ff58552c43e2e426fc535
7
+ data.tar.gz: 82c4a016afeacd6c2d0604d3cfaa0e274cd89f1b411bd2ceab40d3bc1055f40c50c3058e2ea2eef9cce7af80b9192946b8a638f317d3148451e91566e1f5952f
data/README.md CHANGED
@@ -1,3 +1,16 @@
1
1
  #Safety Check
2
2
 
3
3
  Selectively add type safety to your methods.
4
+
5
+ # Usage
6
+
7
+ ```ruby
8
+ class Widget
9
+ include SafetyCheck
10
+
11
+ def foo(a, b, c)
12
+ # ...
13
+ end
14
+ safety_check :foo, String, Fixnum, Symbol
15
+ end
16
+ ```
data/lib/safety_check.rb CHANGED
@@ -21,7 +21,17 @@ module SafetyCheck
21
21
  raise ArgumentError, "wrong number of arguments for #{method_name.inspect} (#{arg_types.length} for #{required_arity})"
22
22
  end
23
23
 
24
- receiver.send(:alias_method, unsafe_method_name, method_name)
24
+ arg_types = arg_types.map do |arg_type|
25
+ if arg_type.is_a? Array
26
+ arg_type
27
+ else
28
+ [arg_type]
29
+ end
30
+ end
31
+
32
+ unless receiver.instance_methods.include?(unsafe_method_name)
33
+ receiver.send(:alias_method, unsafe_method_name, method_name)
34
+ end
25
35
 
26
36
  receiver.send(:define_method, method_name) do |*args, &block|
27
37
  if args.length < required_arity
@@ -29,7 +39,7 @@ module SafetyCheck
29
39
  end
30
40
 
31
41
  args.zip(arg_types).each do |arg, arg_type|
32
- unless arg.is_a? arg_type
42
+ unless arg_type.select { |arg_t| arg.is_a? arg_t }.length > 0
33
43
  raise ArgumentError, "expected #{arg.inspect} to be a #{arg_type} when calling #{method_name.inspect}"
34
44
  end
35
45
  end
data/lib/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module SafetyCheck
2
- VERSION = '0.0.2'
2
+ VERSION = '0.0.3'
3
3
  end
@@ -2,6 +2,15 @@ require 'spec_helper'
2
2
 
3
3
  describe SafetyCheck do
4
4
  describe 'safety_check' do
5
+ # When this test fails Ruby will segfault!
6
+ it 'should properly overwrite an existing call to safety_check' do
7
+ expect do
8
+ class Hello
9
+ safety_check :salut, [String, Symbol]
10
+ end
11
+ end.to_not raise_error
12
+ end
13
+
5
14
  it 'instance methods should not raise an error when the correct type of argument is used' do
6
15
  hi = Hello.new
7
16
 
@@ -58,6 +67,26 @@ describe SafetyCheck do
58
67
  end.to raise_error
59
68
  end
60
69
 
70
+ it 'instance methods should allow an argument\'s type constraint to be an array of types' do
71
+ hi = Hello.new
72
+
73
+ expect(
74
+ hi.salut('world')
75
+ ).to eq('Salut, world!')
76
+
77
+ expect(
78
+ hi.salut(:world)
79
+ ).to eq('Salut, world!')
80
+ end
81
+
82
+ it 'instance methods should raise an error when the wrong type of argument is used for arguments with an array of allowed types' do
83
+ hi = Hello.new
84
+
85
+ expect do
86
+ hi.salut(5)
87
+ end.to raise_error
88
+ end
89
+
61
90
  it 'class methods should not raise an error when the correct type of argument is used' do
62
91
  expect(Hello.salutations('world')).to eq('Good day, world!')
63
92
  end
@@ -97,5 +126,21 @@ describe SafetyCheck do
97
126
  end
98
127
  end.to raise_error
99
128
  end
129
+
130
+ it 'class methods should allow an argument\'s type constraint to be an array of types' do
131
+ expect(
132
+ Hello.bonjour('world')
133
+ ).to eq('Bonjour, world!')
134
+
135
+ expect(
136
+ Hello.bonjour(:world)
137
+ ).to eq('Bonjour, world!')
138
+ end
139
+
140
+ it 'class methods should raise an error when the wrong type of argument is used for arguments with an array of allowed types' do
141
+ expect do
142
+ Hello.bonjour(5)
143
+ end.to raise_error
144
+ end
100
145
  end
101
146
  end
@@ -22,6 +22,11 @@ class Hello
22
22
  end
23
23
  safety_check :hallo, String
24
24
 
25
+ def salut(subject)
26
+ "Salut, #{subject}!"
27
+ end
28
+ safety_check :salut, [String, Symbol]
29
+
25
30
  def self.salutations(subject)
26
31
  "Good day, #{subject}!"
27
32
  end
@@ -42,4 +47,9 @@ class Hello
42
47
  "Good morning, #{new_subject}!"
43
48
  end
44
49
  safety_check :guten_morgen, String
50
+
51
+ def self.bonjour(subject)
52
+ "Bonjour, #{subject}!"
53
+ end
54
+ safety_check :bonjour, [String, Symbol]
45
55
  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.2
4
+ version: 0.0.3
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-05 00:00:00.000000000 Z
11
+ date: 2014-07-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec