Module: Rolemodel::Utility::TaskTools

Defined in:
lib/rolemodel/utility/task_tools.rb

Constant Summary collapse

PROGRESS =
%w[         ].cycle

Instance Method Summary collapse

Instance Method Details

#dry_run?Boolean

Enables a pattern for dry-run mode in tasks. Assumes usage of other utility methods like say_with_time and indicate_progress that provide user feedback, separate from the actions they perform.

Tasks can be updated to support this mode by adding unless dry_run? to lines of code that write to the file system or database.

Returns:

  • (Boolean)


41
42
43
# File 'lib/rolemodel/utility/task_tools.rb', line 41

def dry_run?
  ENV['DRY_RUN'].present?
end

#indicate_progress(index, total = nil, report_interval: 9) ⇒ Object

Indicate the progress of a long-running process

Usage (with a known total): 100.times do |i| indicate_progress(i, 100) end

Only update every 'report_interval' iteration for eye-trackable animation speed Also displays a completion percentage if a total is provided



30
31
32
33
34
# File 'lib/rolemodel/utility/task_tools.rb', line 30

def indicate_progress(index, total = nil, report_interval: 9)
  return unless (index % report_interval).zero?

  print("#{PROGRESS.next} #{to_percent(index, total) if total}\r") # rubocop:disable Rails/Output
end

#sanitize_arguments(args, defaults = {}) ⇒ Object

Improves the usability of Rake::Task arguments which are positional and don't support default values. This method allows invocation with skipped arguments via _ and supports a defaults hash.



48
49
50
# File 'lib/rolemodel/utility/task_tools.rb', line 48

def sanitize_arguments(args, defaults = {})
  defaults.merge(ArgumentSanitizer.new(args).to_h)
end

#say(message, subitem: false) ⇒ Object

based on the migration helper of the same name



16
17
18
# File 'lib/rolemodel/utility/task_tools.rb', line 16

def say(message, subitem: false)
  puts "#{subitem ? '   ->' : '--'} #{message}" # rubocop:disable Rails/Output
end

#say_with_time(message) ⇒ Object

based on the migration helper of the same name



9
10
11
12
13
# File 'lib/rolemodel/utility/task_tools.rb', line 9

def say_with_time(message, &)
  say message
  time = Benchmark.measure(&)
  say '%.4fs' % time.real, subitem: true
end