Sha256: e797e45f4a5cf7eee71e47accc38cb1822b646de549f9387a99aa213db632446

Contents?: true

Size: 1.53 KB

Versions: 12

Compression:

Stored size: 1.53 KB

Contents

module ActiveMocker
  module ActiveRecord

    class Schema

      def self.define(options, &block)
        version = options[:version]
        search_result = search_cache(@table_search)
        search_result unless search_result.nil?
        schema = parse
        schema.instance_eval(&block)
        add_to_cache schema.tables.first
        schema.tables.first
      end

      def self.parse
        SchemaParser.new(@table_search)
      end

      def self.add_to_cache(table)
        @@tables_cache ||= []
        @@tables_cache << table unless table.nil?
      end

      def self.search_cache(table_name)
        @@tables_cache ||= []
        @@tables_cache.find do |h|
          h.name == table_name
        end
      end

      def self.clear_cache
        @@tables_cache = []
      end

      def self.search(table_name)
        @table_search = table_name
      end

    end

    class SchemaParser

      attr_reader :tables, :table_search

      def initialize(table_search)
        @table_search = table_search
        @tables = []

      end

      def create_table(name, options={}, &block)
        tables << ActiveMocker::Table.new(name, CreateTable.new.instance_eval(&block)) if name == table_search
      end

      def method_missing(meth, *args)
      end

    end
  end

  class CreateTable

    attr_reader :fields

    def initialize
      @fields = []
    end

    def method_missing(meth, *args)
      base_field meth, args
    end

    def base_field(type, args)
      fields << Field.new(args.shift, type, args)
    end

  end

end


Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
active_mocker-1.1.10 lib/active_mocker/active_record/schema.rb
active_mocker-1.1.9 lib/active_mocker/active_record/schema.rb
active_mocker-1.1.8 lib/active_mocker/active_record/schema.rb
active_mocker-1.1.7 lib/active_mocker/active_record/schema.rb
active_mocker-1.1.6 lib/active_mocker/active_record/schema.rb
active_mocker-1.1.5 lib/active_mocker/active_record/schema.rb
active_mocker-1.1.4 lib/active_mocker/active_record/schema.rb
active_mocker-1.1.3 lib/active_mocker/active_record/schema.rb
active_mocker-1.1.2 lib/active_mocker/active_record/schema.rb
active_mocker-1.1.1 lib/active_mocker/active_record/schema.rb
active_mocker-1.1.0 lib/active_mocker/active_record/schema.rb
active_mocker-1.0.1 lib/active_mocker/active_record/schema.rb