Permalink
Browse files

Add comments and fix namespace.

InstanceIndexKey was in global scope.
  • Loading branch information...
1 parent 3cd2dee commit 8ab0484f435ce3445ced20bf700dae58a0c1112d @kouno committed Aug 19, 2012
View
@@ -30,10 +30,10 @@
# of the counterpart ObjectType. There are no raw values, only instances of ValueType
# classes.
-require 'rbtree'
require 'activefacts/api/support' # General support code and core patches
require 'activefacts/api/vocabulary' # A Ruby module may become a Vocabulary
require 'activefacts/api/instance_index' # The index used by a constellation to record every instance
+require 'activefacts/api/instance_index_key' # The keys for instance index which are comparable
require 'activefacts/api/constellation' # A Constellation is a query result or fact population
require 'activefacts/api/object_type' # A Ruby class may become a ObjectType in a Vocabulary
require 'activefacts/api/role' # A ObjectType has a collection of Roles
@@ -43,5 +43,4 @@
require 'activefacts/api/entity' # An Entity class is an Instance not of a value class
require 'activefacts/api/standard_types' # Value classes are augmented so their subclasses may become Value Types
require 'activefacts/api/exceptions' # Relevant exceptions
-require 'activefacts/api/instance_index_key'
require 'activefacts/tracer'
@@ -6,6 +6,7 @@
#
require 'forwardable'
+require 'rbtree'
module ActiveFacts
module API
@@ -1,56 +1,79 @@
-class InstanceIndexKey
- attr_reader :value
+#
+# ActiveFacts Runtime API
+# InstanceIndex class
+#
+# Copyright (c) 2009 Clifford Heath. Read the LICENSE file.
+#
- def initialize(hash)
- @value = flatten_key(hash)
- end
+module ActiveFacts
+ module API
- def <=>(other)
- if contains_nil?(@value) || contains_nil?(other.value)
- @value.inspect <=> other.value.inspect
- else
- @value <=> other.value
- end
- end
+ # Instance Index Key provides a way to compare hashes with nil values.
+ class InstanceIndexKey
+
+ # Key value
+ attr_reader :value
+
+ # Initialize an instance index key base on a hash.
+ def initialize(hash)
+ @value = flatten_key(hash)
+ end
- def contains_nil?(arr)
- if arr.class.ancestors.include?(Array)
- arr.any? do |el|
- if el.nil?
- true
+ # Compare an instance index key with another.
+ #
+ # Keys containing nil values will be compared using their string
+ # representation. (see inspect)
+ def <=>(other)
+ if contains_nil?(@value) || contains_nil?(other.value)
+ @value.inspect <=> other.value.inspect
else
- contains_nil?(el)
+ @value <=> other.value
end
end
- else
- arr.nil?
- end
- end
- def ==(other)
- @value == other.value
- end
+ # Checks if arr contains a nil value.
+ def contains_nil?(arr)
+ if arr.class.ancestors.include?(Array)
+ arr.any? do |el|
+ if el.nil?
+ true
+ else
+ contains_nil?(el)
+ end
+ end
+ else
+ arr.nil?
+ end
+ end
- def eql?(other)
- if self.class == other.class
- self == other
- else
- false
- end
- end
+ def ==(other)
+ @value == other.value
+ end
- def hash
- @value.hash
- end
+ def eql?(other)
+ if self.class == other.class
+ self == other
+ else
+ false
+ end
+ end
+
+ def hash
+ @value.hash
+ end
- private
- def flatten_key(key)
- if key.is_a?(Array)
- key.map { |identifier| flatten_key(identifier) }
- elsif key.respond_to?(:identifying_role_values)
- key.identifying_role_values
- else
- key
+ private
+ # Any entity contained in `key` will be changed into its identifying role
+ # values.
+ def flatten_key(key)
+ if key.is_a?(Array)
+ key.map { |identifier| flatten_key(identifier) }
+ elsif key.respond_to?(:identifying_role_values)
+ key.identifying_role_values
+ else
+ key
+ end
+ end
end
end
end
@@ -1,29 +1,29 @@
-describe InstanceIndexKey do
+describe ActiveFacts::API::InstanceIndexKey do
it "should be negative for ['a'] <=> ['b']" do
- vt1 = InstanceIndexKey.new(['a'])
- vt2 = InstanceIndexKey.new(['b'])
+ vt1 = ActiveFacts::API::InstanceIndexKey.new(['a'])
+ vt2 = ActiveFacts::API::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 = ActiveFacts::API::InstanceIndexKey.new(['a'])
+ vt2 = ActiveFacts::API::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 = ActiveFacts::API::InstanceIndexKey.new(['b'])
+ vt2 = ActiveFacts::API::InstanceIndexKey.new(['a'])
vt1.<=>(vt2).should == 1
end
it "should fall back on string comparison with nil values" do
- vt1 = InstanceIndexKey.new([nil])
- vt2 = InstanceIndexKey.new(['a'])
+ vt1 = ActiveFacts::API::InstanceIndexKey.new([nil])
+ vt2 = ActiveFacts::API::InstanceIndexKey.new(['a'])
vt1.<=>(vt2).should == 1
- vt1 = InstanceIndexKey.new([nil])
- vt2 = InstanceIndexKey.new([1])
+ vt1 = ActiveFacts::API::InstanceIndexKey.new([nil])
+ vt2 = ActiveFacts::API::InstanceIndexKey.new([1])
vt1.<=>(vt2).should == 1
end
end

0 comments on commit 8ab0484

Please sign in to comment.