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.



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

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.



134
135
136
# File 'lib/ridgepole/dsl_parser.rb', line 134

def __definition
  @__definition
end

#__executeObject (readonly)

Returns the value of attribute __execute.



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

def __execute
  @__execute
end

Class Method Details

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



143
144
145
146
147
148
149
150
151
152
153
# File 'lib/ridgepole/dsl_parser.rb', line 143

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

Instance Method Details

#add_foreign_key(from_table, to_table, options = {}) ⇒ Object



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/ridgepole/dsl_parser.rb', line 211

def add_foreign_key(from_table, to_table, options = {})
  unless options[:name]
    raise "Foreign key name in `#{from_table}` is undefined"
  end

  from_table = from_table.to_s
  to_table = to_table.to_s
  options[:name] = options[:name].to_s
  @__definition[from_table] ||= {}
  @__definition[from_table][:foreign_keys] ||= {}
  idx = options[:name]

  if @__definition[from_table][:foreign_keys][idx]
    raise "Foreign Key `#{from_table}(#{idx})` already defined"
  end

  @__definition[from_table][:foreign_keys][idx] = {
    :to_table => to_table,
    :options => options,
  }
end

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



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/ridgepole/dsl_parser.rb', line 175

def add_index(table_name, column_name, options = {})
  table_name = table_name.to_s
  # Keep column_name for expression index support
  # https://github.com/rails/rails/pull/23393
  unless column_name.is_a?(String) && /\W/ === column_name
    column_name = [column_name].flatten.map {|i| i.to_s }
  end
  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

  if options[:length].is_a?(Numeric)
    index_length = options[:length]
    options[:length] = {}

    column_name.each do |col|
      options[:length][col] = index_length
    end

    # XXX: fix for https://github.com/rails/rails/commit/5025fd3a99c68f95bdd6fd43f382c62e9653236b
    if ActiveRecord::VERSION::MAJOR >= 6 or (ActiveRecord::VERSION::MAJOR == 5 and (ActiveRecord::VERSION::MINOR >= 1 or ActiveRecord::VERSION::TINY >= 1))
      options[:length] = options[:length].symbolize_keys
    end
  end

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

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

Yields:

  • (table_definition)


155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/ridgepole/dsl_parser.rb', line 155

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

  if options[:primary_key] and options[:primary_key].is_a?(Symbol)
    options[:primary_key] = options[:primary_key].to_s
  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



245
246
247
248
249
250
# File 'lib/ridgepole/dsl_parser.rb', line 245

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

#require(file) ⇒ Object



233
234
235
236
237
238
239
240
241
242
243
# File 'lib/ridgepole/dsl_parser.rb', line 233

def require(file)
  schemafile = (file =~ %r|\A/|) ? file : 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