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



67
68
69
70
71
# File 'lib/ridgepole/dsl_parser.rb', line 67

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

Instance Attribute Details

#__definitionObject (readonly)

Returns the value of attribute __definition.



64
65
66
# File 'lib/ridgepole/dsl_parser.rb', line 64

def __definition
  @__definition
end

#__executeObject (readonly)

Returns the value of attribute __execute.



65
66
67
# File 'lib/ridgepole/dsl_parser.rb', line 65

def __execute
  @__execute
end

Class Method Details

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



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/ridgepole/dsl_parser.rb', line 73

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

  if opts[:path]
    ctx.instance_eval(dsl, opts[:path])
  else
    ctx.instance_eval(dsl)
  end

  [ctx.__definition, ctx.__execute]
end

.include_module(mod) ⇒ Object



3
4
5
6
7
# File 'lib/ridgepole/dsl_parser.rb', line 3

def self.include_module(mod)
  unless self.included_modules.include?(mod)
    include mod
  end
end

Instance Method Details

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



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/ridgepole/dsl_parser.rb', line 105

def add_index(table_name, column_name, options = {})
  table_name = table_name.to_s
  column_name = [column_name].flatten.map {|i| i.to_s }
  options[:name] = options[:name].to_s if options[:name]
  @__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)


85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/ridgepole/dsl_parser.rb', line 85

def create_table(table_name, options = {})
  table_name = table_name.to_s
  table_definition = TableDefinition.new

  [:primary_key].each do |key|
    options[key] = options[key].to_s if options[key]
  end

  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

#execute(sql, name = nil, &cond) ⇒ Object



135
136
137
138
139
140
# File 'lib/ridgepole/dsl_parser.rb', line 135

def execute(sql, name = nil, &cond)
  @__execute << {
    :sql => sql,
    :condition => cond,
  }
end

#require(file) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
# File 'lib/ridgepole/dsl_parser.rb', line 123

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

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