Permalink
Browse files

Fix hash eql?, ==, hash().

  • Loading branch information...
1 parent 0baf69c commit 403908165549098c31ed2a80f0268949b09cd7bf @kouno committed Aug 11, 2012
View
2 lib/activefacts/api/instance_index.rb
@@ -64,7 +64,7 @@ def [](key)
end
def keys
- @hash.keys.map { |key| key.to_hash }
+ @hash.keys.map { |key| key.value }
end
def delete(key)
View
24 lib/activefacts/api/instance_index_key.rb
@@ -1,21 +1,33 @@
class InstanceIndexKey
- attr_reader :hash
+ attr_reader :value
def initialize(hash)
- @hash = flatten_key(hash)
+ @value = flatten_key(hash)
end
def <=>(other)
- result = @hash <=> other.hash
+ result = @value <=> other.value
if result.nil?
- @hash.to_s <=> other.hash.to_s
+ @value.to_s <=> other.value.to_s
else
result
end
end
- def to_hash
- @hash
+ def ==(other)
+ @value == other.value
+ end
+
+ def eql?(other)
+ if self.class == other.class
+ self == other
+ else
+ false
+ end
+ end
+
+ def hash
+ @value.hash
end
private
View
8 lib/activefacts/api/numeric.rb
@@ -185,6 +185,14 @@ def hash #:nodoc:
end
end
+ def <=>(other)
+ to_s <=> other.to_s
+ end
+
+ def ==(other)
+ to_s == other.to_s
+ end
+
def eql?(o) #:nodoc:
to_s.eql?(o.to_s)
end
View
6 spec/constellation/instance_index_key_spec.rb
@@ -1,13 +1,17 @@
describe InstanceIndexKey do
- it "should be comparable" do
+ it "should be negative for ['a'] <=> ['b']" do
vt1 = InstanceIndexKey.new(['a'])
vt2 = InstanceIndexKey.new(['b'])
vt1.<=>(vt2).should == -1
+ end
+ it "should be equal for ['a'] <=> ['a']" do
vt1 = InstanceIndexKey.new(['a'])
vt2 = InstanceIndexKey.new(['a'])
vt1.<=>(vt2).should == 0
+ end
+ it "should be negative for ['b'] <=> ['a']" do
vt1 = InstanceIndexKey.new(['b'])
vt2 = InstanceIndexKey.new(['a'])
vt1.<=>(vt2).should == 1

0 comments on commit 4039081

Please sign in to comment.