Class: PgGraph::Data::Record
- Inherits:
-
DatabaseObject
- Object
- Node
- DatabaseObject
- PgGraph::Data::Record
- Defined in:
- lib/pg_graph/data/data.rb
Instance Attribute Summary collapse
-
#table ⇒ Object
readonly
Table of record.
Attributes inherited from Node
Class Method Summary collapse
Instance Method Summary collapse
-
#[](name) ⇒ Object
Return Field object with the given name.
-
#associations ⇒ Object
List of association objects in the record.
-
#columns ⇒ Object
List of Column objects in the record.
-
#data ⇒ Object
(also: #to_h)
def data() @impl.select { |k,v| v.is_a?(Column) }.map { |k,v| [k, v.data] }.to_h end.
-
#database ⇒ Object
Redefine DatabaseObject#database.
-
#each(&block) ⇒ Object
Iterate fields.
-
#field?(name) ⇒ Boolean
True if the record contains a field with the given name.
-
#fields ⇒ Object
List of Field objects in the record.
-
#guid ⇒ Object
Redefine GUID to include ID of record.
-
#id ⇒ Object
ID of record (Integer).
-
#initialize(table, columns) ⇒ Record
constructor
def id() @id end.
- #inspect_inner ⇒ Object
-
#name ⇒ Object
Redefine name to be the record ID“.
-
#names ⇒ Object
List of field names.
-
#to_yaml ⇒ Object
def to_sql() “(” + type.columns.map { |column_type| field?(column_type.name) ? self&.to_sql || ‘NULL’ : ‘DEFAULT’ }.join(“, ”) + “)” end.
-
#uid ⇒ Object
Redefine UID to include ID of record.
-
#value_columns ⇒ Object
List of columns excluding generated columns.
Methods inherited from DatabaseObject
Methods inherited from Node
#inspect, #object, #value, #value_type
Constructor Details
#initialize(table, columns) ⇒ Record
def id() @id end
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 |
# File 'lib/pg_graph/data/data.rb', line 368 def initialize(table, columns) constrain table, Table constrain columns, { Symbol => PgGraph::RUBY_CLASSES } constrain columns, lambda { |h| h.key?(:id) }, "No :id field" @table = table # has to go before super super(table.type.record_type, dimension: 1) @impl = columns.map { |k,v| [k, Column.new(self, k, v)] }.to_h for name, association in table.associations if association.is_a?(KindAssociation) #&& @impl.key?(name) next if !@impl.key?(name) column = @impl.delete(name) @impl[name] = KindRecordField.new(self, name, association, column) else !@impl.key?(name) or raise "Duplicate field: #{name}" field_class = Dimension.field_class(association.dimension) @impl[name] = field_class.new(self, name, association) end end # puts "Record#initialize(#{table.uid.inspect}, #{columns.inspect})" # puts " columns: #{self.columns}" # puts " fields: #{fields}" # puts " names: #{names}" # puts " value_columns: #{value_columns}" table.send(:add_record, self) end |
Instance Attribute Details
#table ⇒ Object (readonly)
Table of record
362 363 364 |
# File 'lib/pg_graph/data/data.rb', line 362 def table @table end |
Class Method Details
.split_uid(uid) ⇒ Object
347 348 349 350 |
# File 'lib/pg_graph/data/data.rb', line 347 def self.split_uid(uid) uid =~ /^(.*?)\[(\d+)\]$/ [$1, $2.to_i] end |
Instance Method Details
#[](name) ⇒ Object
Return Field object with the given name
395 |
# File 'lib/pg_graph/data/data.rb', line 395 def [](name) @impl[name.to_sym] end |
#associations ⇒ Object
List of association objects in the record
415 |
# File 'lib/pg_graph/data/data.rb', line 415 def associations() @impl.values.select { |field| field.is_a?(AssociationField) } end |
#columns ⇒ Object
List of Column objects in the record
407 |
# File 'lib/pg_graph/data/data.rb', line 407 def columns() @impl.values.select { |field| field.is_a?(Column) } end |
#data ⇒ Object Also known as: to_h
def data() @impl.select { |k,v| v.is_a?(Column) }.map { |k,v| [k, v.data] }.to_h end
424 425 426 |
# File 'lib/pg_graph/data/data.rb', line 424 def data() @impl.select { |k,v| v.is_a?(Column) || v.is_a?(KindRecordField) }.map { |k,v| [k, v.data] }.to_h end |
#database ⇒ Object
Redefine DatabaseObject#database
359 |
# File 'lib/pg_graph/data/data.rb', line 359 def database() table.database end |
#each(&block) ⇒ Object
Iterate fields
418 |
# File 'lib/pg_graph/data/data.rb', line 418 def each(&block) fields.each { |field| yield field } end |
#field?(name) ⇒ Boolean
True if the record contains a field with the given name
398 |
# File 'lib/pg_graph/data/data.rb', line 398 def field?(name) @impl.key?(name.to_sym) end |
#fields ⇒ Object
List of Field objects in the record
404 |
# File 'lib/pg_graph/data/data.rb', line 404 def fields() @impl.values end |
#guid ⇒ Object
Redefine GUID to include ID of record
353 |
# File 'lib/pg_graph/data/data.rb', line 353 def guid() "#{table.guid}[#{id}]" end |
#id ⇒ Object
ID of record (Integer)
365 |
# File 'lib/pg_graph/data/data.rb', line 365 def id() @impl[:id].value end |
#inspect_inner ⇒ Object
439 |
# File 'lib/pg_graph/data/data.rb', line 439 def inspect_inner() "{" + columns.map(&:inspect_inner).join(', ') + "}" end |
#name ⇒ Object
Redefine name to be the record ID“
356 |
# File 'lib/pg_graph/data/data.rb', line 356 def name() id end |
#names ⇒ Object
List of field names
401 |
# File 'lib/pg_graph/data/data.rb', line 401 def names() @impl.keys end |
#to_yaml ⇒ Object
def to_sql()
"(" + type.columns.map { |column_type|
field?(column_type.name) ? self[column_type.name]&.to_sql || 'NULL' : 'DEFAULT'
}.join(", ") + ")"
end
437 |
# File 'lib/pg_graph/data/data.rb', line 437 def to_yaml() data end |
#uid ⇒ Object
Redefine UID to include ID of record
345 |
# File 'lib/pg_graph/data/data.rb', line 345 def uid() "#{table.uid}[#{id}]" end |
#value_columns ⇒ Object
List of columns excluding generated columns
410 411 412 |
# File 'lib/pg_graph/data/data.rb', line 410 def value_columns() columns.select { |column| !column.type.generated? }.uniq end |