class Forematter::Commands::Cleanup

Constants

CLEANUP_MAP

Public Instance Methods

run() click to toggle source
# File lib/forematter/commands/cleanup.rb, line 22
def run
  require 'stringex_lite'
  require 'titleize'

  files_with(field).each do |file|
    old = file[field].to_ruby
    begin
      val = cleanup(old)
    rescue Forematter::UnexpectedValue => e
      log_skip(file, e.message) && next
    end
    unless val == old
      file[field] = val
      file.write
    end
  end
end

Protected Instance Methods

cleanup(val) click to toggle source
# File lib/forematter/commands/cleanup.rb, line 51
def cleanup(val)
  val = val.dup
  return cleanup_array(val) if val.is_a?(Array)
  fail Forematter::UnexpectedValue, "#{field} is not an array" if options[:sort]
  options.keys.each do |option|
    val = val.method(CLEANUP_MAP[option]).call if CLEANUP_MAP.key?(option) && options[option]
  end
  val
end
cleanup_array(val) click to toggle source
# File lib/forematter/commands/cleanup.rb, line 61
def cleanup_array(val)
  options.keys.each do |option|
    val.map!(&CLEANUP_MAP[option]) if CLEANUP_MAP.key?(option) && options[option]
  end
  val.sort_by!(&:downcase) if options[:sort]
  val
end