attr_extras 2.3.0 → 3.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +5 -1
- data/lib/attr_extras.rb +11 -2
- data/lib/attr_extras/version.rb +1 -1
- data/spec/attr_implement_spec.rb +9 -6
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 984c86a8a64ba56792a8e78ce89452ca66c32b55
|
4
|
+
data.tar.gz: 3957fdf50be3ffee0ffec904176d6e3918676294
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1857358b5507acbbbd0aefe5266f1b0e5626ecb5415ffd5070a7f4bd327fcdf4e615917df393cbefe58b6b34d6c9ae869a32751549d119e1d2fc37aa8a4f2e46
|
7
|
+
data.tar.gz: 2f7c2155bfd5a96f9b31f88bf2153ef11048ce1117c03b402cb5ac90249bfe66240177e741ce4544c88e3933566ad6abafb7fe80082c6344172159c05bafff9e
|
data/README.md
CHANGED
@@ -143,7 +143,11 @@ Defines query methods like `foo?`, which is true if (and only if) `foo` is truth
|
|
143
143
|
|
144
144
|
### `attr_implement :foo, :bar`<br>
|
145
145
|
|
146
|
-
Defines methods `foo` and `bar` that raise e.g. `"Implement a 'foo' method"
|
146
|
+
Defines nullary (0-argument) methods `foo` and `bar` that raise e.g. `"Implement a 'foo()' method"`.
|
147
|
+
|
148
|
+
`attr_implement :foo, [:name, :age]` will define a binary (2-argument) method `foo` that raises `"Implement a 'foo(name, age)' method"`.
|
149
|
+
|
150
|
+
This is suitable for [abstract methods](http://en.wikipedia.org/wiki/Abstract_method#Abstract_methods) in base classes, e.g. when using the [template method pattern](http://en.wikipedia.org/wiki/Template_method_pattern).
|
147
151
|
|
148
152
|
|
149
153
|
## Philosophy
|
data/lib/attr_extras.rb
CHANGED
@@ -63,9 +63,18 @@ module AttrExtras
|
|
63
63
|
end
|
64
64
|
|
65
65
|
def attr_implement(*names)
|
66
|
+
arg_names = names.last.is_a?(Array) ? names.pop : []
|
67
|
+
arity = arg_names.length
|
68
|
+
|
66
69
|
names.each do |name|
|
67
|
-
define_method(name) do |*
|
68
|
-
|
70
|
+
define_method(name) do |*args|
|
71
|
+
provided_arity = args.length
|
72
|
+
|
73
|
+
if provided_arity != arity
|
74
|
+
raise ArgumentError, "wrong number of arguments (#{provided_arity} for #{arity})"
|
75
|
+
end
|
76
|
+
|
77
|
+
raise "Implement a '#{name}(#{arg_names.join(", ")})' method"
|
69
78
|
end
|
70
79
|
end
|
71
80
|
end
|
data/lib/attr_extras/version.rb
CHANGED
data/spec/attr_implement_spec.rb
CHANGED
@@ -1,23 +1,26 @@
|
|
1
1
|
require_relative "spec_helper"
|
2
2
|
|
3
3
|
describe Object, ".attr_implement" do
|
4
|
-
it "creates methods that raise" do
|
4
|
+
it "creates 0-argument methods that raise" do
|
5
5
|
klass = Class.new do
|
6
6
|
attr_implement :foo, :bar
|
7
7
|
end
|
8
8
|
|
9
9
|
example = klass.new
|
10
10
|
exception = lambda { example.foo }.must_raise RuntimeError
|
11
|
-
exception.message.must_equal "Implement a 'foo' method"
|
11
|
+
exception.message.must_equal "Implement a 'foo()' method"
|
12
12
|
end
|
13
13
|
|
14
|
-
it "
|
14
|
+
it "allows specifying arity and argument names" do
|
15
15
|
klass = Class.new do
|
16
|
-
attr_implement :foo
|
16
|
+
attr_implement :foo, [:name, :age]
|
17
17
|
end
|
18
18
|
|
19
19
|
example = klass.new
|
20
|
-
|
21
|
-
exception
|
20
|
+
|
21
|
+
exception = lambda { example.foo(1, 2) }.must_raise RuntimeError
|
22
|
+
exception.message.must_equal "Implement a 'foo(name, age)' method"
|
23
|
+
|
24
|
+
lambda { example.foo }.must_raise ArgumentError
|
22
25
|
end
|
23
26
|
end
|