Class: PgGraph::Data::Record

Inherits:
DatabaseObject show all
Defined in:
lib/pg_graph/data/data.rb

Instance Attribute Summary collapse

Attributes inherited from Node

#dimension, #type

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from DatabaseObject

#<=>, #dot

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

#tableObject (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

#associationsObject

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

#columnsObject

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

#dataObject 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

#databaseObject

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

Returns:

  • (Boolean)


398
# File 'lib/pg_graph/data/data.rb', line 398

def field?(name) @impl.key?(name.to_sym) end

#fieldsObject

List of Field objects in the record



404
# File 'lib/pg_graph/data/data.rb', line 404

def fields() @impl.values end

#guidObject

Redefine GUID to include ID of record



353
# File 'lib/pg_graph/data/data.rb', line 353

def guid() "#{table.guid}[#{id}]" end

#idObject

ID of record (Integer)



365
# File 'lib/pg_graph/data/data.rb', line 365

def id() @impl[:id].value end

#inspect_innerObject



439
# File 'lib/pg_graph/data/data.rb', line 439

def inspect_inner() "{" + columns.map(&:inspect_inner).join(', ') + "}" end

#nameObject

Redefine name to be the record ID“



356
# File 'lib/pg_graph/data/data.rb', line 356

def name() id end

#namesObject

List of field names



401
# File 'lib/pg_graph/data/data.rb', line 401

def names() @impl.keys end

#to_yamlObject

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

#uidObject

Redefine UID to include ID of record



345
# File 'lib/pg_graph/data/data.rb', line 345

def uid() "#{table.uid}[#{id}]" end

#value_columnsObject

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