Class: AlgebraDB::Table
- Inherits:
-
Object
- Object
- AlgebraDB::Table
- Defined in:
- lib/algebra_db/table.rb
Overview
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.
Class Attribute Summary collapse
-
.relationships ⇒ Object
readonly
Determine the relationships defined on this table.
-
.table_name ⇒ Object
The name of this table in Postgres-land.
Instance Attribute Summary collapse
-
#table_alias ⇒ Object
readonly
Returns the value of attribute table_alias.
Class Method Summary collapse
- .column(name, value) ⇒ Object
-
.column?(name) ⇒ Boolean
Does this table contain this column?.
-
.columns ⇒ Object
Hash of column_name -> column_type.
-
.inspect ⇒ Object
We customize the inspect method to return a quick defintion of this table.
- .relationship(name, other_table, &block) ⇒ Object
- .to_relation(relation_alias) ⇒ Object
Instance Method Summary collapse
- #column(name) ⇒ Object
- #columns ⇒ Object
- #from_clause ⇒ Object
-
#initialize(table_alias) ⇒ Table
constructor
A new instance of Table.
- #to_select_item ⇒ Object
Constructor Details
#initialize(table_alias) ⇒ Table
Returns a new instance of Table.
72 73 74 |
# File 'lib/algebra_db/table.rb', line 72 def initialize(table_alias) @table_alias = table_alias end |
Class Attribute Details
.relationships ⇒ Object (readonly)
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.
64 65 66 |
# File 'lib/algebra_db/table.rb', line 64 def relationships @relationships end |
.table_name ⇒ Object
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?
69 70 71 |
# File 'lib/algebra_db/table.rb', line 69 def table_name @table_name end |
Instance Attribute Details
#table_alias ⇒ Object (readonly)
Returns the value of attribute table_alias.
96 97 98 |
# File 'lib/algebra_db/table.rb', line 96 def table_alias @table_alias end |
Class Method Details
.column(name, value) ⇒ Object
27 28 29 30 31 32 33 34 |
# 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) ⇒ Boolean
Does this table contain this column?
38 39 40 |
# File 'lib/algebra_db/table.rb', line 38 def column?(name) columns.key?(name.to_sym) end |
.columns ⇒ Object
Hash of column_name -> column_type
This returns a value modified with #dup, so modifying it will have no effect on the table defintion.
15 16 17 |
# File 'lib/algebra_db/table.rb', line 15 def columns (@columns || {}).dup end |
.inspect ⇒ Object
We customize the inspect method to return a quick defintion of this table. This makes working with it in a REPL much, much easier.
22 23 24 25 |
# File 'lib/algebra_db/table.rb', line 22 def inspect str = "#<#{name}:#{object_id} " str << "columns=[#{columns.keys.map(&:inspect).join(', ')}]>" end |
.relationship(name, other_table, &block) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 |
# 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) ⇒ Object
42 43 44 |
# File 'lib/algebra_db/table.rb', line 42 def to_relation(relation_alias) new(relation_alias) end |
Instance Method Details
#column(name) ⇒ Object
80 81 82 83 84 |
# 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 ⇒ Object
86 87 88 89 90 |
# File 'lib/algebra_db/table.rb', line 86 def columns self.class.columns.keys.map do |k| column(k) end end |
#from_clause ⇒ Object
76 77 78 |
# File 'lib/algebra_db/table.rb', line 76 def from_clause Build::TableFrom.new(self.class.table_name, @table_alias) end |
#to_select_item ⇒ Object
92 93 94 |
# File 'lib/algebra_db/table.rb', line 92 def to_select_item columns.flat_map(&:to_select_item) end |