Class: PgGraph::Data::Table
- Inherits:
-
DatabaseObject
- Object
- Node
- DatabaseObject
- PgGraph::Data::Table
- Defined in:
- lib/pg_graph/data/data.rb
Overview
Note that Query objects have the same name as the referenced table but different UID. Queries are not included in the schemas list of tables
Instance Attribute Summary collapse
-
#associations ⇒ Object
readonly
Cached association objects.
-
#schema ⇒ Object
readonly
Schema of table.
Attributes inherited from Node
Instance Method Summary collapse
-
#[](id) ⇒ Object
Return record with the given ID.
-
#data ⇒ Object
(also: #to_h)
Return table as a hash from record ID to record hash.
-
#database ⇒ Object
Redefine DatabaseObject#database.
-
#each(&block) ⇒ Object
Iterate 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 record IDs (excluding duplicates).
-
#initialize(schema, type, dimension: 2) ⇒ Table
constructor
A new instance of Table.
- #initialize_associations ⇒ Object
- #inspect_inner ⇒ Object
-
#max_id ⇒ Object
Max.
-
#records ⇒ Object
List of Record objects (including duplicates).
-
#select(arg = nil, dimension: nil, &block) ⇒ Object
:call-seq: select(id, &block) select(ids, &block).
-
#size ⇒ Object
Number of records in the table (including duplicates).
- #to_yaml ⇒ Object
Methods inherited from DatabaseObject
#<=>, #dot, #guid, #name, #uid
Methods inherited from Node
#inspect, #object, #value, #value_type
Constructor Details
#initialize(schema, type, dimension: 2) ⇒ Table
Returns a new instance of Table.
234 235 236 237 238 239 240 241 |
# File 'lib/pg_graph/data/data.rb', line 234 def initialize(schema, type, dimension: 2) constrain schema, Schema constrain type, PgGraph::Type::Table @schema = schema super(type, dimension: dimension) @impl = {} # A table is implemented as a hash from integer ID to Record object @associations = {} # initialized by #initialize_associations end |
Instance Attribute Details
#associations ⇒ Object (readonly)
Cached association objects. Map from record or table field name to Association object. Associations are cached in Table because they have non-trivial constructors. Initialized by #initialize_associations
229 230 231 |
# File 'lib/pg_graph/data/data.rb', line 229 def associations @associations end |
#schema ⇒ Object (readonly)
Schema of table
224 225 226 |
# File 'lib/pg_graph/data/data.rb', line 224 def schema @schema end |
Instance Method Details
#[](id) ⇒ Object
Return record with the given ID
286 |
# File 'lib/pg_graph/data/data.rb', line 286 def [](id) @impl[id] end |
#data ⇒ Object Also known as: to_h
Return table as a hash from record ID to record hash
328 |
# File 'lib/pg_graph/data/data.rb', line 328 def data() @impl.map { |k,v| [k, v.data] }.to_h end |
#database ⇒ Object
Redefine DatabaseObject#database
221 |
# File 'lib/pg_graph/data/data.rb', line 221 def database() schema.database end |
#each(&block) ⇒ Object
Iterate records
325 |
# File 'lib/pg_graph/data/data.rb', line 325 def each(&block) @impl.values.each { |record| yield record } end |
#empty? ⇒ Boolean
True if table is empty
283 |
# File 'lib/pg_graph/data/data.rb', line 283 def empty?() @impl.empty? end |
#id?(id) ⇒ Boolean
True if the table contains a record with the given ID
289 |
# File 'lib/pg_graph/data/data.rb', line 289 def id?(id) @impl.key?(id) end |
#ids ⇒ Object
List of record IDs (excluding duplicates)
292 |
# File 'lib/pg_graph/data/data.rb', line 292 def ids() @impl.keys end |
#initialize_associations ⇒ Object
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 |
# File 'lib/pg_graph/data/data.rb', line 243 def initialize_associations db = schema.database type.fields.each { |column| next if column.is_a?(PgGraph::Type::SimpleColumn) that_table = db[column.type.schema.name][column.type.table.name] association = case column when PgGraph::Type::KindRecordColumn KindAssociation.new(1, self, that_table, column.this_link_column.to_sym, column.that_link_column.to_sym) when PgGraph::Type::RecordColumn Association.new(1, self, that_table, column.this_link_column.to_sym, column.that_link_column.to_sym) when PgGraph::Type::NmTableColumn, PgGraph::Type::MmTableColumn dimension = column.is_a?(PgGraph::Type::NmTableColumn) ? 2 : 3 nm_table = db[column.mm_table.schema.name][column.mm_table.name] LinkAssociation.new(dimension, self, that_table, nm_table, column.this_link_column.to_sym, column.this_mm_column.to_sym, column.that_mm_column.to_sym, column.that_link_column.to_sym) when PgGraph::Type::TableColumn Association.new(2, self, that_table, column.this_link_column.to_sym, column.that_link_column.to_sym) else raise NotHere end @associations[column.name.to_sym] = association } end |
#inspect_inner ⇒ Object
334 |
# File 'lib/pg_graph/data/data.rb', line 334 def inspect_inner() "[" + records.map(&:inspect_inner).join(", ") + "])" end |
#max_id ⇒ Object
Max. record ID. Equal to 0 if the table is empty
295 |
# File 'lib/pg_graph/data/data.rb', line 295 def max_id() @max_id ||= (@impl.keys.max || 0) end |
#records ⇒ Object
List of Record objects (including duplicates)
232 |
# File 'lib/pg_graph/data/data.rb', line 232 def records() @impl.values end |
#select(arg = nil, dimension: nil, &block) ⇒ Object
:call-seq:
select(id, &block)
select(ids, &block)
Select a number of records given by an ID or an array of IDs. Returns a Record object in the first case and a TableSelect otherwise. The :dimension option can be set to 3 to get a MmTableSelect object instead
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 |
# File 'lib/pg_graph/data/data.rb', line 306 def select(arg = nil, dimension: nil, &block) constrain arg, Integer, [Integer], NilClass constrain dimension, Integer, NilClass case arg when Integer candidates = [self[arg]] dimension ||= 1 when Array candidates = arg.map { |id| self[id] }.compact dimension ||= 2 when NilClass candidates = records dimension ||= 2 end candidates = candidates.select { |record| yield(record) } if block_given? Dimension.value_class(dimension).new(self, candidates) end |
#size ⇒ Object
Number of records in the table (including duplicates)
280 |
# File 'lib/pg_graph/data/data.rb', line 280 def size() records.size end |
#to_yaml ⇒ Object
332 |
# File 'lib/pg_graph/data/data.rb', line 332 def to_yaml() @impl.map { |k,v| v.to_h } end |