Module: Tabl::Dsl::ClassMethods

Defined in:
lib/tabl/dsl.rb

Overview

Defines a dsl to create tables.

Instance Method Summary collapse

Instance Method Details

#column(name, args = {}, &block) ⇒ Object



10
11
12
13
14
# File 'lib/tabl/dsl.rb', line 10

def column(name, args = {}, &block)
  columns_hash[name] = column = Column.new(name, args, &block)
  self.send(:define_method, name) { return column }
  singleton_class.send(:define_method, name) { return column }
end

#columnsObject



16
17
18
# File 'lib/tabl/dsl.rb', line 16

def columns
  columns_hash.values
end

#include_columns(mod, args = {}) ⇒ Object

Includes column definitions. These column definitions will be available to any tables defined in the module.

module UserTables
  include Tabl::Dsl

  include_columns(UserColumns)
end


48
49
50
51
52
53
# File 'lib/tabl/dsl.rb', line 48

def include_columns(mod, args = {})
  mod.columns.each do |column|
    column = args[:record] ? DerefColumn.new(column, args[:record]) : column
    columns_hash[column.name] = column
  end
end

#table(name, args = {}, &block) ⇒ Object

Define a new table.

module UserTables
  include Tabl::Dsl

  table :users, [:name, :email, :phone]
end

MyTables.foo
  # => <Table name:foo>


29
30
31
32
33
34
# File 'lib/tabl/dsl.rb', line 29

def table(name, args = {}, &block)
  args[:base_columns] ||= columns
  table = Table.new(args, &block)
  tables << table
  singleton_class.send(:define_method, name) { return table }
end

#tablesObject



36
37
38
# File 'lib/tabl/dsl.rb', line 36

def tables
  @tables ||= []
end