class Capybara::Screenshot::Pruner

Attributes

strategy[R]

Public Class Methods

new(strategy) click to toggle source
# File lib/capybara-screenshot/pruner.rb, line 6
def initialize(strategy)
  @strategy = strategy

  @strategy_proc = case strategy
  when :keep_all
    lambda { }
  when :keep_last_run
    lambda { prune_with_last_run_strategy }
  when Hash
    raise ArgumentError, ":keep key is required" unless strategy[:keep]
    raise ArgumentError, ":keep must be a Integer" unless strategy[:keep].kind_of?(Integer)
    raise ArgumentError, ":keep value must be number greater than zero" unless strategy[:keep].to_i > 0
    lambda { prune_with_numeric_strategy(strategy[:keep].to_i) }
  else
    fail "Invalid prune strategy #{strategy}. `:keep_all`or `{ keep: 10 }` are valid examples."
  end
end

Public Instance Methods

prune_old_screenshots() click to toggle source
# File lib/capybara-screenshot/pruner.rb, line 24
def prune_old_screenshots
  strategy_proc.call
end

Private Instance Methods

prune_with_last_run_strategy() click to toggle source
# File lib/capybara-screenshot/pruner.rb, line 38
def prune_with_last_run_strategy
  FileUtils.rm_rf(Dir.glob(wildcard_path))
end
prune_with_numeric_strategy(count) click to toggle source
# File lib/capybara-screenshot/pruner.rb, line 42
def prune_with_numeric_strategy(count)
  files = Dir.glob(wildcard_path).sort_by do |file_name|
    File.mtime(File.expand_path(file_name, Screenshot.capybara_root))
  end

  FileUtils.rm_rf(files[0...-count])
end
strategy_proc() click to toggle source
# File lib/capybara-screenshot/pruner.rb, line 30
def strategy_proc
  @strategy_proc
end
wildcard_path() click to toggle source
# File lib/capybara-screenshot/pruner.rb, line 34
def wildcard_path
  File.expand_path('*.{html,png}', Screenshot.capybara_root)
end