Class: PgGraph::Data::MmTableValue
- 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
Attributes inherited from Node
Instance Method Summary collapse
-
#[](id) ⇒ Object
#[] returns the unique record for the given key.
-
#count(id) ⇒ Object
Return the number of (duplicate) records for the given ID.
-
#data ⇒ Object
Redefine #data to return a map from ID to array of (identical) records.
-
#empty? ⇒ Boolean
True if table is empty.
-
#id?(id) ⇒ Boolean
True if the table contains a record with the given ID.
-
#ids ⇒ Object
List of unique record IDs.
-
#initialize(table, records = []) ⇒ MmTableValue
constructor
A new instance of MmTableValue.
-
#records ⇒ Object
List of Record objects including duplicates.
-
#size ⇒ Object
Number of records including duplicates.
- #to_h ⇒ Object
-
#unique_records ⇒ Object
List of unique record objects.
-
#value ⇒ Object
Define #value to return a map from ID to the record with that ID.
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 |
#data ⇒ Object
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
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
64 |
# File 'lib/pg_graph/data/value.rb', line 64 def id?(id) @impl.key?(id) end |
#ids ⇒ Object
List of unique record IDs
67 |
# File 'lib/pg_graph/data/value.rb', line 67 def ids() @impl.keys end |
#records ⇒ Object
List of Record objects including duplicates
70 |
# File 'lib/pg_graph/data/value.rb', line 70 def records() @records ||= @impl.values.flatten end |
#size ⇒ Object
Number of records including duplicates
55 |
# File 'lib/pg_graph/data/value.rb', line 55 def size() records.size end |
#to_h ⇒ Object
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_records ⇒ Object
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 |
#value ⇒ Object
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 |