Class: FileNameHistory

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, history = []) ⇒ FileNameHistory

Returns a new instance of FileNameHistory.



4
5
6
7
# File 'lib/filename_history.rb', line 4

def initialize(name, history = [])
  @history = history.empty? ? [name] : history
  @current = name
end

Instance Attribute Details

#currentObject (readonly)

Returns the value of attribute current.



2
3
4
# File 'lib/filename_history.rb', line 2

def current
  @current
end

#historyObject (readonly)

Returns the value of attribute history.



2
3
4
# File 'lib/filename_history.rb', line 2

def history
  @history
end

Class Method Details

.new_from_history(history) ⇒ Object



15
16
17
18
# File 'lib/filename_history.rb', line 15

def self.new_from_history(history)
  FileNameHistory.new(history.last,
                      history)
end

Instance Method Details

#changed?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/filename_history.rb', line 49

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

#last_nameObject



28
29
30
# File 'lib/filename_history.rb', line 28

def last_name
  @history.last
end

#log(where = $stdout) ⇒ Object



63
64
65
66
67
# File 'lib/filename_history.rb', line 63

def log(where = $stdout)
  return unless changed?
  where.puts(@current)
  where.puts("-> #{@history.last}")
end

#originalObject



24
25
26
# File 'lib/filename_history.rb', line 24

def original
  @history.first
end

#plan_reapplication(name) ⇒ Object



45
46
47
# File 'lib/filename_history.rb', line 45

def plan_reapplication(name)
  @current = name
end

#plan_rename(regexp, replacement) ⇒ Object



32
33
34
35
# File 'lib/filename_history.rb', line 32

def plan_rename(regexp, replacement)
  new_name = @current.gsub(regexp, replacement)
  @history << new_name if new_name != current
end

#plan_rollbackObject



37
38
39
# File 'lib/filename_history.rb', line 37

def plan_rollback
  @history << original
end

#present?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/filename_history.rb', line 20

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


58
59
60
61
# File 'lib/filename_history.rb', line 58

def print_history(where = $stdout)
  where.puts(@current)
  where.print "-> #{@history.join("\n-> ")}\n\n"
end

#renameObject



53
54
55
56
# File 'lib/filename_history.rb', line 53

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

#to_hashObject



9
10
11
12
13
# File 'lib/filename_history.rb', line 9

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

#was_named?(name) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/filename_history.rb', line 41

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