Class: PgGraph::Data::MmTableValue

Inherits:
Value
  • Object
show all
Defined in:
lib/pg_graph/data/value.rb

Overview

MmTableValue is implemented as a hash from integer ID to non-empty array of identical Record objects

Instance Attribute Summary

Attributes inherited from Value

#referenced_object

Attributes inherited from Node

#dimension, #type

Instance Method Summary collapse

Methods inherited from Node

#inspect, #object, #to_yaml, #value_type

Constructor Details

#initialize(table, records = []) ⇒ MmTableValue

Returns a new instance of MmTableValue.



47
48
49
50
51
52
# File 'lib/pg_graph/data/value.rb', line 47

def initialize(table, records = [])
  constrain records, [Record]
  super(table.type, table, dimension: 3)
  @impl = {}
  records.each { |record| (@impl[record.id] ||= []) << record }
end

Instance Method Details

#[](id) ⇒ Object

#[] returns the unique record for the given key



61
# File 'lib/pg_graph/data/value.rb', line 61

def [](id) @impl[id]&.first end

#count(id) ⇒ Object

Return the number of (duplicate) records for the given ID



82
# File 'lib/pg_graph/data/value.rb', line 82

def count(id) @imp[id]&.size || 0 end

#dataObject

Redefine #data to return a map from ID to array of (identical) records



76
# File 'lib/pg_graph/data/value.rb', line 76

def data() @impl.map { |k,records| [k, records.map(&:value)] }.to_h end

#empty?Boolean

True if table is empty

Returns:

  • (Boolean)


58
# File 'lib/pg_graph/data/value.rb', line 58

def empty?() @impl.empty? end

#id?(id) ⇒ Boolean

True if the table contains a record with the given ID

Returns:

  • (Boolean)


64
# File 'lib/pg_graph/data/value.rb', line 64

def id?(id) @impl.key?(id) end

#idsObject

List of unique record IDs



67
# File 'lib/pg_graph/data/value.rb', line 67

def ids() @impl.keys end

#recordsObject

List of Record objects including duplicates



70
# File 'lib/pg_graph/data/value.rb', line 70

def records() @records ||= @impl.values.flatten end

#sizeObject

Number of records including duplicates



55
# File 'lib/pg_graph/data/value.rb', line 55

def size() records.size end

#to_hObject



84
85
86
# File 'lib/pg_graph/data/value.rb', line 84

def to_h()
  @impl.map { |id, records| [id, records.map { |record| record.to_h }] }.to_h
end

#unique_recordsObject

List of unique record objects



73
# File 'lib/pg_graph/data/value.rb', line 73

def unique_records() @unique_records ||= @impl.values.map(&:first) end

#valueObject

Define #value to return a map from ID to the record with that ID



79
# File 'lib/pg_graph/data/value.rb', line 79

def value() @impl.map { |k,records| [k, records.first.value] }.to_h end