attr_extras 4.2.0 → 4.3.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 +1 -0
- data/lib/attr_extras.rb +6 -16
- data/lib/attr_extras/attr_value.rb +42 -0
- data/lib/attr_extras/version.rb +1 -1
- data/spec/attr_implement_spec.rb +2 -2
- metadata +3 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 7a2e11ff4aab1d128893a0145fb72ebc026ac4b5
|
|
4
|
+
data.tar.gz: 2bf233446caf0aefb3f7e16db7e388bc216cde34
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 2d61715715969959cf37ee1e7c6ba1a553675e14bd516cfddd76ea5c0758cb7518af05e3a437407392152e7960c5b3e389350a52c57f3b4297caa29b4c9a36c6
|
|
7
|
+
data.tar.gz: ac7e05c8f8cb05e554b59b644e5d0115f195b0bb9555e06d5918c1c0f46995c2c96224b94ccb52b203410df7696a04ec931fe2c5622f4b5a5d228ba0e558f664
|
data/README.md
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
[](https://travis-ci.org/#!/barsoom/attr_extras/builds)
|
|
2
|
+
[](https://codeclimate.com/github/barsoom/attr_extras)
|
|
2
3
|
|
|
3
4
|
# attr\_extras
|
|
4
5
|
|
data/lib/attr_extras.rb
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
1
|
require "attr_extras/version"
|
|
2
2
|
require "attr_extras/attr_initialize"
|
|
3
|
+
require "attr_extras/attr_value"
|
|
3
4
|
require "attr_extras/attr_query"
|
|
4
5
|
require "attr_extras/utils"
|
|
5
6
|
|
|
6
7
|
module AttrExtras
|
|
8
|
+
# To avoid masking coding errors, we don't inherit from StandardError (which would be implicitly rescued). Forgetting to define a requisite method isn't just some runtime error.
|
|
9
|
+
class MethodNotImplementedError < Exception; end
|
|
10
|
+
|
|
7
11
|
module ModuleMethods
|
|
8
12
|
def attr_initialize(*names, &block)
|
|
9
13
|
AttrInitialize.new(self, names, block).apply
|
|
@@ -19,21 +23,7 @@ module AttrExtras
|
|
|
19
23
|
end
|
|
20
24
|
|
|
21
25
|
def attr_value(*names)
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
define_method(:==) do |other|
|
|
25
|
-
return false unless other.is_a?(self.class)
|
|
26
|
-
|
|
27
|
-
names.all? { |attr| self.public_send(attr) == other.public_send(attr) }
|
|
28
|
-
end
|
|
29
|
-
|
|
30
|
-
# Both #eql? and #hash are required for hash identity.
|
|
31
|
-
|
|
32
|
-
alias_method :eql?, :==
|
|
33
|
-
|
|
34
|
-
define_method(:hash) do
|
|
35
|
-
[ self.class, *names.map { |attr| public_send(attr) } ].hash
|
|
36
|
-
end
|
|
26
|
+
AttrValue.new(self, *names).apply
|
|
37
27
|
end
|
|
38
28
|
|
|
39
29
|
def pattr_initialize(*names, &block)
|
|
@@ -90,7 +80,7 @@ module AttrExtras
|
|
|
90
80
|
raise ArgumentError, "wrong number of arguments (#{provided_arity} for #{arity})"
|
|
91
81
|
end
|
|
92
82
|
|
|
93
|
-
raise "Implement a '#{name}(#{arg_names.join(", ")})' method"
|
|
83
|
+
raise MethodNotImplementedError, "Implement a '#{name}(#{arg_names.join(", ")})' method"
|
|
94
84
|
else
|
|
95
85
|
super(name, *args)
|
|
96
86
|
end
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
class AttrExtras::AttrValue
|
|
2
|
+
def initialize(klass, *names)
|
|
3
|
+
@klass, @names = klass, names
|
|
4
|
+
end
|
|
5
|
+
|
|
6
|
+
attr_reader :klass, :names
|
|
7
|
+
private :klass, :names
|
|
8
|
+
|
|
9
|
+
def apply
|
|
10
|
+
define_readers
|
|
11
|
+
define_equals
|
|
12
|
+
define_hash_identity
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
private
|
|
16
|
+
|
|
17
|
+
def define_readers
|
|
18
|
+
klass.send(:attr_reader, *names)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def define_equals
|
|
22
|
+
names = @names # Make available within block.
|
|
23
|
+
|
|
24
|
+
klass.send(:define_method, :==) do |other|
|
|
25
|
+
return false unless other.is_a?(self.class)
|
|
26
|
+
|
|
27
|
+
names.all? { |attr| self.public_send(attr) == other.public_send(attr) }
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def define_hash_identity
|
|
32
|
+
names = @names # Make available within block.
|
|
33
|
+
|
|
34
|
+
# Both #eql? and #hash are required for hash identity.
|
|
35
|
+
|
|
36
|
+
klass.send(:alias_method, :eql?, :==)
|
|
37
|
+
|
|
38
|
+
klass.send(:define_method, :hash) do
|
|
39
|
+
[ self.class, *names.map { |attr| public_send(attr) } ].hash
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
data/lib/attr_extras/version.rb
CHANGED
data/spec/attr_implement_spec.rb
CHANGED
|
@@ -7,7 +7,7 @@ describe Object, ".attr_implement" do
|
|
|
7
7
|
end
|
|
8
8
|
|
|
9
9
|
example = klass.new
|
|
10
|
-
exception = lambda { example.foo }.must_raise
|
|
10
|
+
exception = lambda { example.foo }.must_raise AttrExtras::MethodNotImplementedError
|
|
11
11
|
exception.message.must_equal "Implement a 'foo()' method"
|
|
12
12
|
end
|
|
13
13
|
|
|
@@ -18,7 +18,7 @@ describe Object, ".attr_implement" do
|
|
|
18
18
|
|
|
19
19
|
example = klass.new
|
|
20
20
|
|
|
21
|
-
exception = lambda { example.foo(1, 2) }.must_raise
|
|
21
|
+
exception = lambda { example.foo(1, 2) }.must_raise AttrExtras::MethodNotImplementedError
|
|
22
22
|
exception.message.must_equal "Implement a 'foo(name, age)' method"
|
|
23
23
|
|
|
24
24
|
lambda { example.foo }.must_raise ArgumentError
|
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: 4.
|
|
4
|
+
version: 4.3.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Henrik Nyh
|
|
@@ -10,7 +10,7 @@ authors:
|
|
|
10
10
|
autorequire:
|
|
11
11
|
bindir: bin
|
|
12
12
|
cert_chain: []
|
|
13
|
-
date: 2015-02-
|
|
13
|
+
date: 2015-02-25 00:00:00.000000000 Z
|
|
14
14
|
dependencies:
|
|
15
15
|
- !ruby/object:Gem::Dependency
|
|
16
16
|
name: minitest
|
|
@@ -58,6 +58,7 @@ files:
|
|
|
58
58
|
- lib/attr_extras.rb
|
|
59
59
|
- lib/attr_extras/attr_initialize.rb
|
|
60
60
|
- lib/attr_extras/attr_query.rb
|
|
61
|
+
- lib/attr_extras/attr_value.rb
|
|
61
62
|
- lib/attr_extras/utils.rb
|
|
62
63
|
- lib/attr_extras/version.rb
|
|
63
64
|
- script/test
|