Class: Prequel::Relations::Table

Inherits:
Relation
  • Object
show all
Defined in:
lib/prequel/relations/table.rb

Defined Under Namespace

Classes: TableDefinitionContext

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Relation

#find, #join, #project, #query, #table_ref, #to_relation, #where

Constructor Details

#initialize(name, tuple_class = nil, &block) ⇒ Table

Returns a new instance of Table.



6
7
8
9
10
# File 'lib/prequel/relations/table.rb', line 6

def initialize(name, tuple_class=nil, &block)
  @name, @tuple_class = name, tuple_class
  @columns_by_name = {}
  TableDefinitionContext.new(self).instance_eval(&block) if block
end

Instance Attribute Details

#columns_by_nameObject (readonly)

Returns the value of attribute columns_by_name.



4
5
6
# File 'lib/prequel/relations/table.rb', line 4

def columns_by_name
  @columns_by_name
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/prequel/relations/table.rb', line 4

def name
  @name
end

#tuple_classObject (readonly)

Returns the value of attribute tuple_class.



4
5
6
# File 'lib/prequel/relations/table.rb', line 4

def tuple_class
  @tuple_class
end

Instance Method Details

#[](col_name) ⇒ Object



16
17
18
# File 'lib/prequel/relations/table.rb', line 16

def [](col_name)
  "#{name}__#{col_name}".to_sym
end

#columnsObject



32
33
34
# File 'lib/prequel/relations/table.rb', line 32

def columns
  columns_by_name.values
end

#def_column(name, type) ⇒ Object



12
13
14
# File 'lib/prequel/relations/table.rb', line 12

def def_column(name, type)
  columns_by_name[name] = Expressions::Column.new(self, name, type)
end

#get_column(column_name) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/prequel/relations/table.rb', line 20

def get_column(column_name)
  if column_name.match(/(.+)__(.+)/)
    qualifier, column_name = $1.to_sym, $2.to_sym
    return nil unless qualifier == name
  end
  columns_by_name[column_name]
end

#get_table(table_name) ⇒ Object



28
29
30
# File 'lib/prequel/relations/table.rb', line 28

def get_table(table_name)
  self if name == table_name
end

#singular_table_ref(query) ⇒ Object



40
41
42
# File 'lib/prequel/relations/table.rb', line 40

def singular_table_ref(query)
  query.add_singular_table_ref(self, Sql::TableRef.new(self))
end

#visit(query) ⇒ Object



36
37
38
# File 'lib/prequel/relations/table.rb', line 36

def visit(query)
  query.table_ref = table_ref(query)
end