Sha256: 44eeb6874716848b4cfef6d887f129e7560f793c70c8ff5fad0e650b51394764

Contents?: true

Size: 1.75 KB

Versions: 1

Compression:

Stored size: 1.75 KB

Contents

class Ridgepole::DSLParser
  class Context
    class TableDefinition
      attr_reader :__definition

      def initialize
        @__definition = {}
      end

      def column(name, type, options = {})
        @__definition[name] = {
          :type => type,
          :options => options,
        }
      end

      TYPES = [
        :string,
        :text,
        :integer,
        :float,
        :decimal,
        :datetime,
        :timestamp,
        :time,
        :date,
        :binary,
        :boolean
      ]

      TYPES.each do |column_type|
        define_method column_type do |*args|
          options = args.extract_options!
          column_names = args
          column_names.each {|name| column(name, column_type, options) }
        end
      end
    end

    attr_reader :__definition

    def initialize
      @__definition = {}
    end

    def self.eval(dsl)
      ctx = self.new
      ctx.instance_eval(dsl)
      ctx.__definition
    end

    def create_table(table_name, options = {})
      # XXX: Add exist check
      table_definition = TableDefinition.new
      yield(table_definition)
      @__definition[table_name] ||= {}
      @__definition[table_name][:definition] = table_definition.__definition
      options.delete(:force)
      @__definition[table_name][:options] = options
    end

    def add_index(table_name, column_name, options = {})
      # XXX: Add exist check
      @__definition[table_name] ||= {}
      @__definition[table_name][:indices] ||= {}
      idx = options[:name] || column_name
      @__definition[table_name][:indices][idx] = {
        :column_name => column_name,
        :options => options,
      }
    end
  end

  def initialize(options = {})
    @options = options
  end

  def parse(dsl)
    Context.eval(dsl)
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ridgepole-0.0.1 lib/ridgepole/dsl_parser.rb