Class: Necropsy::DefinitionIndex

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/necropsy/graph/definition_index.rb

Defined Under Namespace

Classes: AmbiguousDefinitionError, Lookup

Instance Method Summary collapse

Constructor Details

#initialize(definitions = []) ⇒ DefinitionIndex

Returns a new instance of DefinitionIndex.



41
42
43
44
45
# File 'lib/necropsy/graph/definition_index.rb', line 41

def initialize(definitions = [])
  @by_definition_id = {}
  @by_symbol_id = Hash.new { |hash, key| hash[key] = [] }
  definitions.each { |definition| add(definition) }
end

Instance Method Details

#[](identifier) ⇒ Object



78
79
80
81
82
83
# File 'lib/necropsy/graph/definition_index.rb', line 78

def [](identifier)
  result = lookup(identifier)
  raise AmbiguousDefinitionError, result if result.ambiguous?

  result.node
end

#add(definition) ⇒ Object

Raises:

  • (ArgumentError)


47
48
49
50
51
52
53
54
55
# File 'lib/necropsy/graph/definition_index.rb', line 47

def add(definition)
  existing = @by_definition_id[definition.graph_id]
  return existing if existing == definition
  raise ArgumentError, "duplicate physical definition ID: #{definition.graph_id}" if existing

  @by_definition_id[definition.graph_id] = definition
  @by_symbol_id[definition.symbol_id] << definition
  definition
end

#definitions_for(symbol_id) ⇒ Object



61
62
63
# File 'lib/necropsy/graph/definition_index.rb', line 61

def definitions_for(symbol_id)
  sorted(@by_symbol_id.fetch(symbol_id, [])).freeze
end

#eachObject



103
104
105
106
107
108
# File 'lib/necropsy/graph/definition_index.rb', line 103

def each
  return enum_for(:each) unless block_given?

  values.each { |definition| yield(definition.graph_id, definition) }
  self
end

#each_keyObject



110
111
112
113
114
115
# File 'lib/necropsy/graph/definition_index.rb', line 110

def each_key
  return enum_for(:each_key) unless block_given?

  values.each { |definition| yield definition.graph_id }
  self
end

#each_value(&block) ⇒ Object



117
118
119
120
121
122
# File 'lib/necropsy/graph/definition_index.rb', line 117

def each_value(&block)
  return enum_for(:each_value) unless block

  values.each(&block)
  self
end

#empty?Boolean

Returns:

  • (Boolean)


137
138
139
# File 'lib/necropsy/graph/definition_index.rb', line 137

def empty?
  @by_definition_id.empty?
end

#exact(definition_id) ⇒ Object



57
58
59
# File 'lib/necropsy/graph/definition_index.rb', line 57

def exact(definition_id)
  @by_definition_id[definition_id]
end

#fetch(identifier, default = UNDEFINED) ⇒ Object



89
90
91
92
93
94
95
96
97
# File 'lib/necropsy/graph/definition_index.rb', line 89

def fetch(identifier, default = UNDEFINED)
  result = lookup(identifier)
  raise AmbiguousDefinitionError, result if result.ambiguous?
  return result.node unless result.missing?
  return yield(identifier) if block_given?
  return default unless default.equal?(UNDEFINED)

  raise KeyError, "key not found: #{identifier.inspect}"
end

#key?(identifier) ⇒ Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/necropsy/graph/definition_index.rb', line 99

def key?(identifier)
  !lookup(identifier).missing?
end

#keysObject



124
125
126
# File 'lib/necropsy/graph/definition_index.rb', line 124

def keys
  values.map(&:graph_id)
end

#lengthObject Also known as: size



132
133
134
# File 'lib/necropsy/graph/definition_index.rb', line 132

def length
  @by_definition_id.length
end

#lookup(identifier) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/necropsy/graph/definition_index.rb', line 65

def lookup(identifier)
  definitions = definitions_for(identifier)
  unless definitions.empty?
    status = definitions.one? ? :unique : :ambiguous
    return Lookup.new(identifier: identifier, status: status, definitions: definitions)
  end

  exact_match = exact(identifier)
  return Lookup.new(identifier: identifier, status: :exact, definitions: [exact_match].freeze) if exact_match

  Lookup.new(identifier: identifier, status: :missing, definitions: definitions)
end

#resolve(identifier) ⇒ Object



85
86
87
# File 'lib/necropsy/graph/definition_index.rb', line 85

def resolve(identifier)
  lookup(identifier).definitions
end

#valuesObject



128
129
130
# File 'lib/necropsy/graph/definition_index.rb', line 128

def values
  sorted(@by_definition_id.values)
end