Class: ActiveFacts::API::KeyArray

Inherits:
Array
  • Object
show all
Defined in:
lib/activefacts/api/instance_index.rb

Instance Method Summary collapse

Constructor Details

#initialize(a) ⇒ KeyArray

Returns a new instance of KeyArray.



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/activefacts/api/instance_index.rb', line 12

def initialize(a)
  super(
    a.map do |e|
      if e.is_a?(Array) && e.class != self.class
        KeyArray.new(e)
      elsif e.eql?(nil)
        []
      else
        e
      end
    end
  )
end

Instance Method Details

#<=>(other) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/activefacts/api/instance_index.rb', line 35

def <=>(other)
  unless other.is_a?(Array)       # Any kind of Array, not just KeyArray
    return 1
  end

  0.upto(size-1) do |i|
    diff = ((s = self[i]) <=> (o = other[i]) rescue nil)
    case diff
    when 0        # Same value, whether exactly the same class or not
      next
    when nil      # Non-comparable values
      return -1 if s == nil       # Ensure that nil values come before other values
      return 1 if o == nil
      diff = s.class.name <=> o.class.name  # Otherwise just ensure stable sorting
      return diff if diff != 0
    else
      return diff
    end
  end
  0
end

#==(other) ⇒ Object

This is used by RBTree for searching, and we need it to use eql? semantics to be like a Hash



31
32
33
# File 'lib/activefacts/api/instance_index.rb', line 31

def ==(other)
  self.eql? other
end

#inspectObject



26
27
28
# File 'lib/activefacts/api/instance_index.rb', line 26

def inspect
  "KeyArray"+super
end