Class: PgGraph::Data::Association

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

Direct Known Subclasses

KindAssociation, LinkAssociation

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dimension, this_table, that_table, this_field, that_field) ⇒ Association

Returns a new instance of Association.



11
12
13
14
15
16
17
18
19
20
# File 'lib/pg_graph/data/association.rb', line 11

def initialize(dimension, this_table, that_table, this_field, that_field)
  Dimension.validate(dimension, min: 1)
  constrain this_table, Table
  constrain that_table, Table
  constrain this_field, String, Symbol
  constrain that_field, String, Symbol
  @dimension = dimension
  @this_table, @this_field = this_table, this_field
  @that_table, @that_field = that_table, that_field
end

Instance Attribute Details

#dimensionObject (readonly)

Dimension of the association



9
10
11
# File 'lib/pg_graph/data/association.rb', line 9

def dimension
  @dimension
end

#that_fieldObject (readonly)

Returns the value of attribute that_field.



6
7
8
# File 'lib/pg_graph/data/association.rb', line 6

def that_field
  @that_field
end

#that_tableObject (readonly)

Returns the value of attribute that_table.



5
6
7
# File 'lib/pg_graph/data/association.rb', line 5

def that_table
  @that_table
end

#this_fieldObject (readonly)

Returns the value of attribute this_field.



4
5
6
# File 'lib/pg_graph/data/association.rb', line 4

def this_field
  @this_field
end

#this_tableObject (readonly)

Returns the value of attribute this_table.



3
4
5
# File 'lib/pg_graph/data/association.rb', line 3

def this_table
  @this_table
end

Instance Method Details

#get(record) ⇒ Object

Get object(s). Either a single record or a list of (possible duplicate) records



28
# File 'lib/pg_graph/data/association.rb', line 28

def get(record) dimension == 1 ? get_record(record) : get_records(record) end

#get_record(this_record) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/pg_graph/data/association.rb', line 30

def get_record(this_record)
  if that_field == :id
    that_table[this_record[this_field].value]
  else
    that_table.records.find { |that_record|
      that_record[that_field].value == this_record[this_field].value
    }
  end
end

#get_records(this_record) ⇒ Object



40
41
42
43
44
# File 'lib/pg_graph/data/association.rb', line 40

def get_records(this_record)
  that_table.records.find_all { |that_record|
    that_record[that_field].value == this_record[this_field].value
  }
end

#query(field) ⇒ Object

Query the association and return a query object (Record, Query, or QueryWithDuplicates)

Raises:

  • (NotThis)


24
# File 'lib/pg_graph/data/association.rb', line 24

def query(field) raise NotThis end