summaryrefslogtreecommitdiff
path: root/lib/humble/database_table.rb
blob: bb3dc07de324fbe4db3fd5c8447e9def15a60cd8 (plain)
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
39
40
41
42
43
44
45
module Humble
  class DatabaseTable
    include Enumerable

    attr_reader :name
    attr_accessor :type

    def initialize
      @columns = []
    end

    def named(name)
      @name = name
    end

    def primary_key(name, default: 0)
      @primary_key = PrimaryKeyColumn.new(name, default)
      add(@primary_key)
    end

    def add(column)
      @columns.push(column)
    end

    def add_column(name)
      add(Column.new(name))
    end

    def each
      @columns.each do |column|
        yield column
      end
    end

    def column_for(key)
      @columns.find { |x| x.matches?(key) }
    end

    def prepare_statement_for(item)
      @columns.inject({}) do |result, column|
        result.merge(column.prepare(item))
      end
    end
  end
end