Class: Ridgepole::DSLParser::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/ridgepole/dsl_parser.rb

Defined Under Namespace

Classes: TableDefinition

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Context

Returns a new instance of Context.



42
43
44
45
# File 'lib/ridgepole/dsl_parser.rb', line 42

def initialize(opts = {})
  @__working_dir = File.expand_path(opts[:path] ? File.dirname(opts[:path]) : Dir.pwd)
  @__definition = {}
end

Instance Attribute Details

#__definitionObject (readonly)

Returns the value of attribute __definition.



40
41
42
# File 'lib/ridgepole/dsl_parser.rb', line 40

def __definition
  @__definition
end

Class Method Details

.eval(dsl, opts = {}) ⇒ Object



47
48
49
50
51
# File 'lib/ridgepole/dsl_parser.rb', line 47

def self.eval(dsl, opts = {})
  ctx = self.new(opts)
  ctx.instance_eval(dsl)
  ctx.__definition
end

Instance Method Details

#add_index(table_name, column_name, options = {}) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/ridgepole/dsl_parser.rb', line 67

def add_index(table_name, column_name, options = {})
  @__definition[table_name] ||= {}
  @__definition[table_name][:indices] ||= {}
  idx = options[:name] || column_name

  if @__definition[table_name][:indices][idx]
    raise "Index `#{table_name}(#{idx})` already defined"
  end

  @__definition[table_name][:indices][idx] = {
    :column_name => column_name,
    :options => options,
  }
end

#create_table(table_name, options = {}) {|table_definition| ... } ⇒ Object

Yields:

  • (table_definition)


53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/ridgepole/dsl_parser.rb', line 53

def create_table(table_name, options = {})
  table_definition = TableDefinition.new
  yield(table_definition)
  @__definition[table_name] ||= {}

  if @__definition[table_name][:definition]
    raise "Table `#{table_name}` already defined"
  end

  @__definition[table_name][:definition] = table_definition.__definition
  options.delete(:force)
  @__definition[table_name][:options] = options
end

#require(file) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/ridgepole/dsl_parser.rb', line 82

def require(file)
  schemafile = File.join(@__working_dir, file)

  if File.exist?(schemafile)
    instance_eval(File.read(schemafile))
  elsif File.exist?(schemafile + '.rb')
    instance_eval(File.read(schemafile + '.rb'))
  else
    Kernel.require(file)
  end
end