Class: Convergence::DSL

Inherits:
Object
  • Object
show all
Defined in:
lib/convergence/dsl.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDSL

Returns a new instance of DSL.



6
7
8
9
# File 'lib/convergence/dsl.rb', line 6

def initialize
  @tables = {}
  @raw_sqls = []
end

Instance Attribute Details

#current_dir_pathObject

Returns the value of attribute current_dir_path.



4
5
6
# File 'lib/convergence/dsl.rb', line 4

def current_dir_path
  @current_dir_path
end

#raw_sqlsObject

Returns the value of attribute raw_sqls.



4
5
6
# File 'lib/convergence/dsl.rb', line 4

def raw_sqls
  @raw_sqls
end

#tablesObject

Returns the value of attribute tables.



4
5
6
# File 'lib/convergence/dsl.rb', line 4

def tables
  @tables
end

Class Method Details

.parse(code, current_dir_path) ⇒ Object



33
34
35
# File 'lib/convergence/dsl.rb', line 33

def self.parse(code, current_dir_path)
  parse_dsl(code, current_dir_path).tables
end

.parse_dsl(code, current_dir_path) ⇒ Object



37
38
39
40
41
42
# File 'lib/convergence/dsl.rb', line 37

def self.parse_dsl(code, current_dir_path)
  parser = new
  parser.current_dir_path = current_dir_path
  parser.instance_eval(code)
  parser
end

Instance Method Details

#create_table(table_name, options = {}, &block) ⇒ Object



11
12
13
14
15
16
# File 'lib/convergence/dsl.rb', line 11

def create_table(table_name, options = {}, &block)
  table = Convergence::Table.new(table_name.to_s, options)
  block.call(table)
  @tables[table_name.to_s] = table
  table
end

#execute(sql) ⇒ Object

Execute an arbitrary SQL statement that the DSL has no dedicated syntax for (e.g. triggers, stored procedures, data backfills). Unlike create_table, this is not diffed against the current schema: it runs every time the schema file is applied, so the SQL itself must be idempotent.



22
23
24
# File 'lib/convergence/dsl.rb', line 22

def execute(sql)
  @raw_sqls << sql
end

#include(path) ⇒ Object



26
27
28
29
30
31
# File 'lib/convergence/dsl.rb', line 26

def include(path)
  next_dir_path = File.dirname("#{@current_dir_path}/#{path}")
  included = Convergence::DSL.parse_dsl(File.open("#{current_dir_path}/#{path}").read, next_dir_path)
  @tables.merge!(included.tables)
  @raw_sqls.concat(included.raw_sqls)
end