class AlgebraDB::Table

Represent a table in AlgebraDB. You should subclass this for your own tables.

You should not call new on this directly. Instead, use the class in a syntax runner.

Attributes

relationships[R]

Determine the relationships defined on this table. This is a pretty simple hash of the name of the relationship to the related table. It does not include any information on how to obtain that relationship: you need to use the defined instance method on a table instance to get that.

table_name[RW]

The name of this table in Postgres-land. This is anything you want, and we don't default this. TODO: maybe default this to follow some kind of rails conventions?

table_alias[R]

Public Class Methods

column(name, value) click to toggle source
# File lib/algebra_db/table.rb, line 27
def column(name, value)
  value = ::AlgebraDB::Value.const_get(value) if value.is_a?(Symbol)
  @columns ||= {}
  @columns[name.to_sym] = value
  define_method(name) do
    value.new(Build::Column.new(table_alias, name))
  end
end
column?(name) click to toggle source

Does this table contain this column?

# File lib/algebra_db/table.rb, line 38
def column?(name)
  columns.key?(name.to_sym)
end
columns() click to toggle source

Hash of column_name -> column_type

This returns a value modified with {#dup}, so modifying it will have no effect on the table defintion.

# File lib/algebra_db/table.rb, line 15
def columns
  (@columns || {}).dup
end
inspect() click to toggle source

We customize the inspect method to return a quick defintion of this table. This makes working with it in a REPL much, much easier.

# File lib/algebra_db/table.rb, line 22
def inspect
  str = "#<#{name}:#{object_id} "
  str << "columns=[#{columns.keys.map(&:inspect).join(', ')}]>"
end
new(table_alias) click to toggle source
# File lib/algebra_db/table.rb, line 72
def initialize(table_alias)
  @table_alias = table_alias
end
relationship(name, other_table, &block) click to toggle source
# File lib/algebra_db/table.rb, line 46
def relationship(name, other_table, &block)
  (@relationships ||= {})[name] = other_table
  define_method(name) do
    relater_proc =
      if block.arity == 2
        proc { |other_relation| block.call(self, other_relation) }
      else
        proc { |other_relation| instance_exec(other_relation, &block) }
      end
    Def::Relationship.new(other_table, relater_proc)
  end
end
to_relation(relation_alias) click to toggle source
# File lib/algebra_db/table.rb, line 42
def to_relation(relation_alias)
  new(relation_alias)
end

Public Instance Methods

column(name) click to toggle source
# File lib/algebra_db/table.rb, line 80
def column(name)
  self.class.columns.fetch(name.to_sym).new(
    Build::Column.new(table_alias, name)
  )
end
columns() click to toggle source
# File lib/algebra_db/table.rb, line 86
def columns
  self.class.columns.keys.map do |k|
    column(k)
  end
end
from_clause() click to toggle source
# File lib/algebra_db/table.rb, line 76
def from_clause
  Build::TableFrom.new(self.class.table_name, @table_alias)
end
to_select_item() click to toggle source
# File lib/algebra_db/table.rb, line 92
def to_select_item
  columns.flat_map(&:to_select_item)
end