Sha256: 65f648935b7f01e3ee82f42e1e60de8670b79becd547ee53461fe090cc3a4b3c

Contents?: true

Size: 978 Bytes

Versions: 1

Compression:

Stored size: 978 Bytes

Contents

module DatabaseFlusher
  class Cleaner
    attr_reader :strategy

    def initialize(orm)
      @orm = orm
      reset_strategy
    end

    def strategy=(name)
      strategy_changed = name != @strategy_name

      stop if strategy_changed

      if name
        create_strategy(name) if strategy_changed
      else
        reset_strategy
      end
    end

    def clean_with(name)
      self.strategy = name
      strategy.clean
    end

    def start
      strategy.start
    end

    def stop
      strategy.stop
    end

    def clean
      strategy.clean
    end

    private

    def create_strategy(name)
      @strategy_name = name
      @strategy = DatabaseFlusher.
        const_get(classify(@orm)).
        const_get("#{classify(name)}Strategy").new
    end

    def reset_strategy
      @strategy_name = nil
      @strategy = DatabaseFlusher::NullStrategy.new
    end

    def classify(name)
      name.to_s.split('_').collect(&:capitalize).join
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
database_flusher-0.2.0 lib/database_flusher/cleaner.rb