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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bfeb7ee3f02c3e7a249a3a76041fa4faf1604b36
4
- data.tar.gz: f976af63159f1beaa0f951652a8ea15220c9b650
3
+ metadata.gz: 984c86a8a64ba56792a8e78ce89452ca66c32b55
4
+ data.tar.gz: 3957fdf50be3ffee0ffec904176d6e3918676294
5
5
  SHA512:
6
- metadata.gz: 49df99f90b8667b4926a30af53e23e50a2327e0305cbf0c81956fba1223773c46e3714e3c29e70cf8a049c21398edd6adb18abe562048346e72baa1a3c7eb7e9
7
- data.tar.gz: c3875dd5d15e08ebaca8941a7624839dca8fcbb4c0acaf607108aae231303204c17d076b9ff0bf2beb12477d78fc0cb30b81e70820723211d3f61e8ac2915b7b
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"`, suitable for abstract base classes.
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
- raise "Implement a '#{name}' method"
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
@@ -1,3 +1,3 @@
1
1
  module AttrExtras
2
- VERSION = "2.3.0"
2
+ VERSION = "3.0.0"
3
3
  end
@@ -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 "accepts any number of arguments" do
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
- exception = lambda { example.foo(1, 2, 3) }.must_raise RuntimeError
21
- exception.message.must_equal "Implement a 'foo' method"
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: attr_extras
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.3.0
4
+ version: 3.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Henrik Nyh