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, object_type, sort) ⇒ InstanceIndex

Returns a new instance of InstanceIndex.



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

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

Instance Attribute Details

#object_typeObject (readonly)

Returns the value of attribute object_type.



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

def object_type
  @object_type
end

#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



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

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



120
121
122
# File 'lib/activefacts/api/instance_index.rb', line 120

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

#[]=(key, value) ⇒ Object

:nodoc:



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

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

#add_instance(instance, k) ⇒ Object



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

def add_instance(instance, k)
  self[k] = instance
end

#delete(k) ⇒ Object



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

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

#delete_instance(instance, k) ⇒ Object



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

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

#detect(&b) ⇒ Object



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

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

#form_key(key) ⇒ Object



130
131
132
133
134
135
136
137
138
139
# File 'lib/activefacts/api/instance_index.rb', line 130

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

#inspectObject



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

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

#refresh_key(old_key) ⇒ Object



124
125
126
127
128
# File 'lib/activefacts/api/instance_index.rb', line 124

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