Sha256: db3ea513eeccf894d5fcec72af9a6eb231a0a6b459e86933f18560e44971907c

Contents?: true

Size: 1.91 KB

Versions: 7

Compression:

Stored size: 1.91 KB

Contents

module ActiveMocker

  class Config
    class << self

      attr_accessor :schema_file,
                    :model_dir,
                    :mock_dir,
                    :model_base_classes,
                    :file_reader,
                    :build_in_progress,
                    :log_location,
                    :clear_log_on_build

      def model_base_classes=(val)
        @model_base_classes = val
      end

      def set
        load_defaults
        yield self
      end

      def load_defaults
        @schema_file        = nil unless @schema_file
        @model_dir          = nil unless @model_dir
        @mock_dir           = nil unless @mock_dir
        @model_base_classes = %w[ ActiveRecord::Base ] unless @model_base_classes
        @file_reader        = FileReader     unless @file_reader
        @log_location       = 'log/active_mocker.log' unless @log_location
        @clear_log_on_build = true
        rails_defaults if Object.const_defined?('Rails')
      end

      def logger
        default_logger
      end

      def reset_all
        [ :@schema_file,
          :@model_dir,
          :@mock_dir,
          :@model_base_classes,
          :@file_reader,
          :@logger,
          :@schema_file,
          :@model_dir,
          :@mock_dir].each{|ivar| instance_variable_set(ivar, nil)}
      end

      def default_logger
        FileUtils.mkdir_p(File.dirname(@log_location)) unless File.directory?(File.dirname(@log_location))
        ::Logger.new(@log_location)
      end

      def rails_defaults
        @schema_file = File.join(Rails.root, 'db/schema.rb') unless @schema_file
        @model_dir   = File.join(Rails.root, 'app/models')   unless @model_dir
        @mock_dir    = File.join(Rails.root, 'spec/mocks')   unless @mock_dir
      end

      def clear_log
        if @clear_log_on_build && File.exist?(@log_location)
          FileUtils.rm(@log_location)
        end
      end

    end

  end

end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
active_mocker-1.7.1 lib/active_mocker/config.rb
active_mocker-1.7.1rc lib/active_mocker/config.rb
active_mocker-1.7 lib/active_mocker/config.rb
active_mocker-1.7rc2 lib/active_mocker/config.rb
active_mocker-1.7.beta3 lib/active_mocker/config.rb
active_mocker-1.7.beta2 lib/active_mocker/config.rb
active_mocker-1.7.beta1 lib/active_mocker/config.rb