Class: FileNameHistory
- Inherits:
-
Object
- Object
- FileNameHistory
- Defined in:
- lib/filename_history.rb
Class Method Summary collapse
Instance Method Summary collapse
- #changed? ⇒ Boolean
- #current ⇒ Object
- #history ⇒ Object
-
#initialize(name, history = []) ⇒ FileNameHistory
constructor
A new instance of FileNameHistory.
- #last_name ⇒ Object
- #log(where = $stdout) ⇒ Object
- #original ⇒ Object
- #plan_reapplication(name) ⇒ Object
- #plan_rename(regexp, replacement) ⇒ Object
- #plan_rollback ⇒ Object
- #present? ⇒ Boolean
- #print_history(where = $stdout) ⇒ Object
- #rename ⇒ Object
- #to_hash ⇒ Object
- #was_named?(name) ⇒ Boolean
Constructor Details
#initialize(name, history = []) ⇒ FileNameHistory
Returns a new instance of FileNameHistory.
2 3 4 5 |
# File 'lib/filename_history.rb', line 2 def initialize(name, history = []) @history = history.empty? ? [name] : history @current = name end |
Class Method Details
.new_from_history(history) ⇒ Object
13 14 15 16 |
# File 'lib/filename_history.rb', line 13 def self.new_from_history(history) return FileNameHistory.new(history.last, history) end |
Instance Method Details
#changed? ⇒ Boolean
51 52 53 |
# File 'lib/filename_history.rb', line 51 def changed? @history.last != @current end |
#current ⇒ Object
71 72 73 |
# File 'lib/filename_history.rb', line 71 def current @current end |
#history ⇒ Object
26 27 28 |
# File 'lib/filename_history.rb', line 26 def history @history end |
#last_name ⇒ Object
30 31 32 |
# File 'lib/filename_history.rb', line 30 def last_name @history.last end |
#log(where = $stdout) ⇒ Object
65 66 67 68 69 |
# File 'lib/filename_history.rb', line 65 def log(where = $stdout) return unless changed? where.puts(@current) where.puts("-> #{@history.last}") end |
#original ⇒ Object
22 23 24 |
# File 'lib/filename_history.rb', line 22 def original @history.first end |
#plan_reapplication(name) ⇒ Object
47 48 49 |
# File 'lib/filename_history.rb', line 47 def plan_reapplication(name) @current = name end |
#plan_rename(regexp, replacement) ⇒ Object
34 35 36 37 |
# File 'lib/filename_history.rb', line 34 def plan_rename(regexp, replacement) new_name = @current.gsub(regexp, replacement) @history << new_name if new_name != current end |
#plan_rollback ⇒ Object
39 40 41 |
# File 'lib/filename_history.rb', line 39 def plan_rollback @history << original end |
#present? ⇒ Boolean
18 19 20 |
# File 'lib/filename_history.rb', line 18 def present? File.exists?(@current) end |
#print_history(where = $stdout) ⇒ Object
60 61 62 63 |
# File 'lib/filename_history.rb', line 60 def print_history(where = $stdout) where.puts(@current) where.print "-> #{@history.join("\n-> ")}\n\n" end |
#rename ⇒ Object
55 56 57 58 |
# File 'lib/filename_history.rb', line 55 def rename File.rename("#{@current}", "#{@history.last}") @current = @history.last end |
#to_hash ⇒ Object
7 8 9 10 11 |
# File 'lib/filename_history.rb', line 7 def to_hash { @history.last => @history } end |
#was_named?(name) ⇒ Boolean
43 44 45 |
# File 'lib/filename_history.rb', line 43 def was_named?(name) @history.include? name end |