Class: FileNameHistory

Inherits:
Object
  • Object
show all
Defined in:
lib/filename_history.rb

Class Method Summary collapse

Instance Method Summary collapse

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

Returns:

  • (Boolean)


51
52
53
# File 'lib/filename_history.rb', line 51

def changed?
  @history.last != @current
end

#currentObject



71
72
73
# File 'lib/filename_history.rb', line 71

def current
  @current
end

#historyObject



26
27
28
# File 'lib/filename_history.rb', line 26

def history
  @history
end

#last_nameObject



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

#originalObject



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_rollbackObject



39
40
41
# File 'lib/filename_history.rb', line 39

def plan_rollback
  @history << original
end

#present?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/filename_history.rb', line 18

def present?
  File.exists?(@current)
end


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

#renameObject



55
56
57
58
# File 'lib/filename_history.rb', line 55

def rename
  File.rename("#{@current}", "#{@history.last}")
  @current = @history.last
end

#to_hashObject



7
8
9
10
11
# File 'lib/filename_history.rb', line 7

def to_hash
  {
    @history.last => @history
  }
end

#was_named?(name) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/filename_history.rb', line 43

def was_named?(name)
  @history.include? name
end