lib/ridgepole/dsl_parser.rb in ridgepole-0.4.7 vs lib/ridgepole/dsl_parser.rb in ridgepole-0.4.8.rc1

- old
+ new

@@ -38,30 +38,32 @@ end end def timestamps(*args) options = {:null => false}.merge(args.extract_options!) - column(:created_at, :datetime, options.dup) - column(:updated_at, :datetime, options.dup) + column(:created_at, :datetime, options) + column(:updated_at, :datetime, options) end def references(*args) options = args.extract_options! polymorphic = options.delete(:polymorphic) args.each do |col| - column("#{col}_id", :integer, options.dup) - column("#{col}_type", :string, polymorphic.is_a?(Hash) ? polymorphic : options.dup) unless polymorphic.nil? + column("#{col}_id", :integer, options) + column("#{col}_type", :string, polymorphic.is_a?(Hash) ? polymorphic : options) unless polymorphic.nil? end end alias :belongs_to :references end attr_reader :__definition + attr_reader :__execute def initialize(opts = {}) @__working_dir = File.expand_path(opts[:path] ? File.dirname(opts[:path]) : Dir.pwd) @__definition = {} + @__execute = [] end def self.eval(dsl, opts = {}) ctx = self.new(opts) @@ -69,11 +71,11 @@ ctx.instance_eval(dsl, opts[:path]) else ctx.instance_eval(dsl) end - ctx.__definition + [ctx.__definition, ctx.__execute] end def create_table(table_name, options = {}) table_name = table_name.to_s table_definition = TableDefinition.new @@ -121,25 +123,32 @@ instance_eval(File.read(schemafile + '.rb'), schemafile + '.rb') else Kernel.require(file) end end + + def execute(sql, name = nil, &cond) + @__execute << { + :sql => sql, + :condition => cond, + } + end end def initialize(options = {}) @options = options end def parse(dsl, opts = {}) - parsed = Context.eval(dsl, opts) - check_orphan_index(parsed) - parsed + definition, execute = Context.eval(dsl, opts) + check_orphan_index(definition) + [definition, execute] end private - def check_orphan_index(parsed) - parsed.each do |table_name, attrs| + def check_orphan_index(definition) + definition.each do |table_name, attrs| if attrs.length == 1 and attrs[:indices] raise "Table `#{table_name}` to create the index is not defined: #{attrs[:indices].keys.join(',')}" end end end