aboutsummaryrefslogtreecommitdiffstats
path: root/lib/arel/table.rb
blob: a189838c07dbd222b0428e1b4c91c1eaa2793bc1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
module Arel
  class Table
    @engine = nil
    class << self; attr_accessor :engine; end

    attr_reader :name, :engine

    def initialize name, engine = Table.engine
      @name    = name
      @engine  = engine
      @engine  = engine[:engine] if Hash === engine
      @columns = nil
    end

    def tm
      TreeManager.new(@engine).from(self)
    end

    def where condition
      tm.where condition
    end

    def project thing
      tm.project thing
    end

    def columns
      @columns ||= @engine.connection.columns(@name, "#{@name} Columns").map do |column|
        Attributes.for(column).new self, column.name, column
      end
    end

    def [] name
      name = name.to_s
      columns.find { |column| column.name == name }
    end
  end
end