Sha256: 341334e0d86f01cdb5b2c61e16a13fd83d9416fedf698ed01df2430ad5d69fcf

Contents?: true

Size: 1.48 KB

Versions: 18

Compression:

Stored size: 1.48 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)
        schema
      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
        search_cache(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))
      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

18 entries across 18 versions & 1 rubygems

Version Path
active_mocker-1.3.2 lib/active_mocker/active_record/schema.rb
active_mocker-1.3.1 lib/active_mocker/active_record/schema.rb
active_mocker-1.3 lib/active_mocker/active_record/schema.rb
active_mocker-1.2.4 lib/active_mocker/active_record/schema.rb
active_mocker-1.2.3 lib/active_mocker/active_record/schema.rb
active_mocker-1.2 lib/active_mocker/active_record/schema.rb
active_mocker-1.2.pre.11 lib/active_mocker/active_record/schema.rb
active_mocker-1.2.pre.10 lib/active_mocker/active_record/schema.rb
active_mocker-1.2.pre.9 lib/active_mocker/active_record/schema.rb
active_mocker-1.2.pre.8 lib/active_mocker/active_record/schema.rb
active_mocker-1.2.pre.7 lib/active_mocker/active_record/schema.rb
active_mocker-1.2.pre.6 lib/active_mocker/active_record/schema.rb
active_mocker-1.2.pre.5 lib/active_mocker/active_record/schema.rb
active_mocker-1.2.pre.4 lib/active_mocker/active_record/schema.rb
active_mocker-1.2.pre.3 lib/active_mocker/active_record/schema.rb
active_mocker-1.2.pre.2 lib/active_mocker/active_record/schema.rb
active_mocker-1.2.pre.1 lib/active_mocker/active_record/schema.rb
active_mocker-1.2.pre lib/active_mocker/active_record/schema.rb