Permalink
Browse files

Add residual key support.

  • Loading branch information...
kouno committed Sep 18, 2012
1 parent 8056c45 commit d734f22bf5c85e597715b218f8d4cd123fb72cae
@@ -19,12 +19,16 @@ module API
# # while keeping behaviors such as
# flat_hash['hello'] = 'world'
module FlatHash
+ def serialize_key(key)
+ ComparableHashKey.new(key)
+ end
+
def []=(key, value)
- @hash[ComparableHashKey.new(key)] = value
+ @hash[serialize_key(key)] = value
end
def [](key)
- @hash[ComparableHashKey.new(key)]
+ @hash[serialize_key(key)]
end
def keys
@@ -56,7 +56,7 @@ def detect &b
end
def delete(key)
- @hash.delete(ComparableHashKey.new(key))
+ @hash.delete(serialize_key(key))
end
end
end
@@ -157,10 +157,9 @@ def subtypes
# Every new role added or inherited comes through here:
def realise_role(role) #:nodoc:
- if (role.is_unary)
- # Unary role
+ if role.is_unary
define_unary_role_accessor(role)
- elsif (role.unique)
+ elsif role.unique
define_single_accessor(role)
else
define_many_to_one_accessor(role)
@@ -254,7 +253,7 @@ def define_many_to_one_accessor(role)
class_eval do
define_method role.getter do
unless (r = instance_variable_get(role_var = role.variable) rescue nil)
- r = instance_variable_set(role_var, RoleValues.new)
+ r = instance_variable_set(role_var, RoleValues.new(role, self))
end
r
end
@@ -16,8 +16,10 @@ class RoleValues #:nodoc:
def_delegators :@hash, :size, :empty?, :values
- def initialize
+ def initialize(role, entity)
@hash = RBTree.new
+ @role = role
+ @entity = entity
end
def +(object)
@@ -29,11 +31,7 @@ def +(object)
end
def -(object)
- clone = Hash.new(values)
- if self[object]
- clone.delete(ComparableHashKey.new(object))
- end
- clone.values
+ values - object
end
def single
@@ -50,7 +48,7 @@ def to_a
end
def include?(key)
- @hash.has_key?(ComparableHashKey.new(key))
+ @hash.has_key?(serialize_key(key))
end
def delete(value)
@@ -62,6 +60,19 @@ def delete(value)
def verbalise
"[#{@hash.values.map(&:verbalise).join(", ")}]"
end
+
+ def [](key)
+ @hash[serialize_key(rebuild_from_residual(key))]
+ end
+
+ def rebuild_from_residual(key)
+ if @role.counterpart.is_identifying
+ irv_position = @role.counterpart.object_type.identifying_roles.index(@role.counterpart)
+ key.insert(irv_position, @entity.identifying_role_values)
+ else
+ key
+ end
+ end
end
end
end
@@ -44,12 +44,10 @@ class Child
@p.all_child.size.should == 3
end
- it "should allow children to be found in the instance index by the residual key" do
- pending "RoleValues use the whole key, not the residual key" do
- @c.Child[[0]].should == @c0
- @c.Child[[1]].should == @c1
- @c.Child[[2]].should == @c2
- end
+ it "should allow children to be found in the RoleValues by the residual key" do
+ @p.all_child[[0]].should == @c0
+ @p.all_child[[1]].should == @c1
+ @p.all_child[[2]].should == @c2
end
it "should allow children to be found in the instance index by the whole key" do

0 comments on commit d734f22

Please sign in to comment.