attr_extras 2.1.1 → 2.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,7 @@
1
1
  ---
2
- !binary "U0hBMQ==":
3
- metadata.gz: !binary |-
4
- ZDczYjI3ZTEyMzdmMWM3NjMwYmIzZGFhODRjMDJmNTAxOGY4NmRhNg==
5
- data.tar.gz: !binary |-
6
- MDY2ZjIwMWM4MzVjZWYwYWE2ZGFhZGYxMDRmMWU3MzBlMTFiOTFkOQ==
2
+ SHA1:
3
+ metadata.gz: d34aacc5f3feb712ffe95b8bf2a52aa72062beb4
4
+ data.tar.gz: 1fac1c2c1dd6039bb6f6a0846b74bb80989bcfa6
7
5
  SHA512:
8
- metadata.gz: !binary |-
9
- NTNkOTlhMWJhODA4M2IxMjlkNjgxNDc0MTBiN2I2NDdhZWE0YmE2M2NkODQw
10
- NTk3MjA5MGRjNDg5NjllNWYwNGZlNTUzZTYyYmQ0MGI3ZGI0MTFhOWY1Nzg1
11
- OTUxMzlhODkxOGRjNDQwOTM0YzlkZWJiZWNiZTEyODg0ZDdhMjk=
12
- data.tar.gz: !binary |-
13
- ODc1ODQ2YmQwNzdkNWVlZWZmZTA3NDllNGIyZDFlNmQyYjNkNWRiODEyMTI3
14
- NmQ3N2Y0NTc3ZDMxOWM2YzAyMmYxZDJkZmFlM2MxZjA4MTUxODk2ZTQ3ODcx
15
- YzcwNWEwZGM2N2Q4NDU5ZjI1YWM4NTk5YWIxMmIzY2Y0MGY1ZDM=
6
+ metadata.gz: bd82d64a1cb6f42556a659e03dd8160ada1b64cccbce889ece550bfbf8afb63f5aad6e7b1d848f3de70d3f753f9148859f4a33ade5ceba0f0531fcc76e11b445
7
+ data.tar.gz: eef168267a504e30f5740c54c7b54dcfc476dab70a372cc3d3e3fd5889dbf5e88316782d19a95345571430412adb0a89d47e26b844ed0c7cfcd5813fdc18710a
data/README.md CHANGED
@@ -52,7 +52,7 @@ Defines private readers for `@foo` and `@bar`.
52
52
 
53
53
  ### `attr_value :foo, :bar`
54
54
 
55
- Defines public readers. Does not define writers, as value objects are typically immutable.
55
+ Defines public readers. Does not define writers, as [value objects](http://en.wikipedia.org/wiki/Value_object) are typically immutable.
56
56
 
57
57
  Defines object equality: two value objects of the same class with the same values are equal.
58
58
 
@@ -66,7 +66,7 @@ attr_initialize :foo, :bar
66
66
  attr_private :foo, :bar
67
67
  ```
68
68
 
69
- The `attr_initialize` notation notation for hash arguments is also supported: `pattr_initialize :foo, [:bar, :baz!]`
69
+ The `attr_initialize` notation for hash arguments is also supported: `pattr_initialize :foo, [:bar, :baz!]`
70
70
 
71
71
 
72
72
  ### `vattr_initialize :foo, :bar`
@@ -78,7 +78,7 @@ attr_initialize :foo, :bar
78
78
  attr_value :foo, :bar
79
79
  ```
80
80
 
81
- The `attr_initialize` notation notation for hash arguments is also supported: `vattr_initialize :foo, [:bar, :baz!]`
81
+ The `attr_initialize` notation for hash arguments is also supported: `vattr_initialize :foo, [:bar, :baz!]`
82
82
 
83
83
 
84
84
  ### `method_object :fooable?, :foo`<br>
@@ -127,7 +127,7 @@ def self.fooable?(foo)
127
127
  end
128
128
  ```
129
129
 
130
- The `attr_initialize` notation notation for hash arguments is also supported: `method_object :fooable?, :foo, [:bar, :baz!]`
130
+ The `attr_initialize` notation for hash arguments is also supported: `method_object :fooable?, :foo, [:bar, :baz!]`
131
131
 
132
132
  You don't have to specify readers if you don't want them: `method_object :fooable?` is also valid.
133
133
 
@@ -180,7 +180,7 @@ Or install it yourself as:
180
180
 
181
181
  ## License
182
182
 
183
- Copyright (c) 2012 [Barsoom AB](http://barsoom.se)
183
+ Copyright (c) 2012-2014 [Barsoom AB](http://barsoom.se)
184
184
 
185
185
  MIT License
186
186
 
data/lib/attr_extras.rb CHANGED
@@ -22,6 +22,14 @@ module AttrExtras
22
22
 
23
23
  names.all? { |attr| self.public_send(attr) == other.public_send(attr) }
24
24
  end
25
+
26
+ # Both #eql? and #hash are required for hash identity.
27
+
28
+ alias_method :eql?, :==
29
+
30
+ define_method(:hash) do
31
+ [self.class.name, *names.map { |attr| public_send(attr) }].hash
32
+ end
25
33
  end
26
34
 
27
35
  def pattr_initialize(*names)
@@ -1,3 +1,3 @@
1
1
  module AttrExtras
2
- VERSION = "2.1.1"
2
+ VERSION = "2.2.0"
3
3
  end
@@ -213,6 +213,30 @@ describe Object, ".attr_value" do
213
213
 
214
214
  assert instance != "a string"
215
215
  end
216
+
217
+ it "hashes objects the same if they have the same attributes" do
218
+ klass = Class.new do
219
+ attr_initialize :foo
220
+ attr_value :foo
221
+ end
222
+
223
+ example1 = klass.new("Foo")
224
+ example2 = klass.new("Foo")
225
+ example3 = klass.new("Bar")
226
+
227
+ example1.hash.must_equal example2.hash
228
+ example1.hash.wont_equal example3.hash
229
+
230
+ assert example1.eql?(example2), "Examples should be 'eql?'"
231
+ refute example2.eql?(example3), "Examples should not be 'eql?'"
232
+
233
+ Set[example1, example2, example3].length.must_equal 2
234
+
235
+ hash = {}
236
+ hash[example1] = :awyeah
237
+ hash[example3] = :wat
238
+ hash[example2].must_equal :awyeah
239
+ end
216
240
  end
217
241
 
218
242
  describe Object, ".attr_id_query" do
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.1.1
4
+ version: 2.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Henrik Nyh
@@ -16,14 +16,14 @@ dependencies:
16
16
  name: rake
17
17
  requirement: !ruby/object:Gem::Requirement
18
18
  requirements:
19
- - - ! '>='
19
+ - - '>='
20
20
  - !ruby/object:Gem::Version
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
24
  version_requirements: !ruby/object:Gem::Requirement
25
25
  requirements:
26
- - - ! '>='
26
+ - - '>='
27
27
  - !ruby/object:Gem::Version
28
28
  version: '0'
29
29
  description:
@@ -57,17 +57,17 @@ require_paths:
57
57
  - lib
58
58
  required_ruby_version: !ruby/object:Gem::Requirement
59
59
  requirements:
60
- - - ! '>='
60
+ - - '>='
61
61
  - !ruby/object:Gem::Version
62
62
  version: '0'
63
63
  required_rubygems_version: !ruby/object:Gem::Requirement
64
64
  requirements:
65
- - - ! '>='
65
+ - - '>='
66
66
  - !ruby/object:Gem::Version
67
67
  version: '0'
68
68
  requirements: []
69
69
  rubyforge_project:
70
- rubygems_version: 2.2.1
70
+ rubygems_version: 2.2.0
71
71
  signing_key:
72
72
  specification_version: 4
73
73
  summary: Takes some boilerplate out of Ruby with methods like attr_initialize.