class ExampleFile
Class for example file
Constants
- DEFAULT_CHOICES
- HIGHLINE
- SUFFIX
- VERSION
Attributes
choices[R]
question_prefix[W]
Public Class Methods
all(directory)
click to toggle source
# File lib/example_file.rb, line 15 def all(directory) Dir[File.join(directory, '**', "*#{SUFFIX}*")] .map { |file_name| new file_name } end
new(file_name)
click to toggle source
# File lib/example_file.rb, line 24 def initialize(file_name) @file_name = file_name @regular_file_name = @file_name.sub SUFFIX, '' @basename = Paint[File.basename(@file_name), :green, :bold] @regular_basename = Paint[File.basename(@regular_file_name), :red, :bold] @choices = DEFAULT_CHOICES.dup @question_prefix = nil end
Public Instance Methods
actualize_regular_file()
click to toggle source
# File lib/example_file.rb, line 35 def actualize_regular_file return create_regular_file unless regular_file_exist? return unless updated? return update_regular_file if diff.chomp.empty? ask_question_and_make_actions end
Private Instance Methods
ask_question_and_make_actions()
click to toggle source
# File lib/example_file.rb, line 85 def ask_question_and_make_actions puts warning_with_diff HIGHLINE.choose do |menu| ## I don't know how to catch complex options like `replace-and-edit`, via shortcut like `rae` # menu.layout = :one_line menu.header = "What to do with #{@regular_basename} " choices.each do |answer, block| menu.choice(answer) { instance_exec(&block) } end end end
create_regular_file()
click to toggle source
# File lib/example_file.rb, line 55 def create_regular_file FileUtils.cp @file_name, @regular_file_name edit_file @regular_file_name end
diff()
click to toggle source
# File lib/example_file.rb, line 64 def diff @diff ||= Diffy::Diff .new(@regular_file_name, @file_name, source: 'files', context: 3) .to_s(:color) end
edit_file(filename)
click to toggle source
# File lib/example_file.rb, line 111 def edit_file(filename) abort '`EDITOR` environment variable is empty, see README' if ENV['EDITOR'].to_s.empty? system "eval $EDITOR #{filename}" end
regular_file_exist?()
click to toggle source
# File lib/example_file.rb, line 51 def regular_file_exist? File.exist? @regular_file_name end
rewrite_regular_file()
click to toggle source
# File lib/example_file.rb, line 117 def rewrite_regular_file File.write @regular_file_name, File.read(@file_name) end
update_regular_file()
click to toggle source
# File lib/example_file.rb, line 60 def update_regular_file FileUtils.touch @regular_file_name end
updated?()
click to toggle source
# File lib/example_file.rb, line 47 def updated? File.mtime(@file_name) > File.mtime(@regular_file_name) end
warning_with_diff()
click to toggle source
# File lib/example_file.rb, line 100 def warning_with_diff <<~WARN #{@question_prefix}#{@basename} was modified after #{@regular_basename}. ```diff #{diff} ``` WARN end