Class: ActiveFacts::API::InstanceIndex

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

Overview

Each Constellation maintains an InstanceIndex for each ObjectType in its Vocabulary. The InstanceIndex object is returned when you call @constellation.ObjectType with no arguments (where ObjectType is the object_type name you’re interested in)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(constellation, klass, sort) ⇒ InstanceIndex

Returns a new instance of InstanceIndex.



87
88
89
90
91
92
# File 'lib/activefacts/api/instance_index.rb', line 87

def initialize(constellation, klass, sort)
  @constellation = constellation
  @klass = klass
  @sort = sort
  @hash = sort ? RBTree.new : {}
end

Instance Attribute Details

#sortObject (readonly)

Returns the value of attribute sort.



64
65
66
# File 'lib/activefacts/api/instance_index.rb', line 64

def sort
  @sort
end

Class Method Details

.def_single_delegator(accessor, method, *expected_arities) ⇒ Object

Should be in module ForwardableWithArityChecking



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/activefacts/api/instance_index.rb', line 67

def self.def_single_delegator(accessor, method, *expected_arities)
  str = %{
    def #{method}(*args, &block)
      if #{expected_arities.size == 0 ? "block" : "!block || !#{expected_arities.inspect}.include?(block.arity)" }
        raise ArgumentError.new("Arity mismatch on #{name}\##{method}, got \#{block ? block.arity : 'none'} want #{expected_arities.inspect} at \#{caller*"\n\t"})")
      end
      #{accessor}.__send__(:#{method}, *args, &block)
    end
  }
  eval(str)
end

Instance Method Details

#[](key) ⇒ Object



111
112
113
# File 'lib/activefacts/api/instance_index.rb', line 111

def [](key)
  @hash[@sort ? form_key(key) : key]
end

#[]=(key, value) ⇒ Object

:nodoc:



107
108
109
# File 'lib/activefacts/api/instance_index.rb', line 107

def []=(key, value)   #:nodoc:
  @hash[@sort ? form_key(key) : key] = value
end

#delete(k) ⇒ Object



98
99
100
# File 'lib/activefacts/api/instance_index.rb', line 98

def delete(k)
  @hash.delete(@sort ? form_key(k) : k)
end

#detect(&b) ⇒ Object



102
103
104
105
# File 'lib/activefacts/api/instance_index.rb', line 102

def detect &b
  r = @hash.detect &b
  r ? r[1] : nil
end

#form_key(key) ⇒ Object



121
122
123
124
125
126
127
128
129
130
# File 'lib/activefacts/api/instance_index.rb', line 121

def form_key key
  case key
  when Array
    KeyArray.new(key)
  when nil
    []
  else
    key
  end
end

#inspectObject



94
95
96
# File 'lib/activefacts/api/instance_index.rb', line 94

def inspect
  "<InstanceIndex for #{@klass.name} in #{@constellation.inspect}>"
end

#refresh_key(old_key) ⇒ Object



115
116
117
118
119
# File 'lib/activefacts/api/instance_index.rb', line 115

def refresh_key(old_key)
  value = @hash.delete(@sort ? form_key(old_key) : old_key)
  new_key = value.identifying_role_values(@klass)
  @hash[@sort ? form_key(new_key) : new_key] = value if value
end