Class: Stash

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Stash

Returns a new instance of Stash.



9
10
11
# File 'lib/stash.rb', line 9

def initialize(path)
  @path = File.expand_path(path)
end

Class Method Details

.stash!Object

Run the stasher based upon command line options.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/stash.rb', line 44

def self.stash!
  opts =
    OptionParser.new do |opts|

      opts.banner = "Usage: stash [options] <files>"

      opts.on("-m", "--message MSG", String, "Note to attach to this stash") do |msg|
        ENV["STASH_MSG"] = msg
      end

      opts.on("-s", "--stash STASH", String, "Path to the stash to use [defaults to STASH_PATH or ~/.stash]") do |stash|
        ENV["STASH_PATH"] = stash
      end

      opts.on_tail("-h", "--help", "Show this message") do
        puts opts
        exit
      end

    end

  opts.parse!

  if ARGV.empty?
    puts opts
    exit
  end

  Stash.new(ENV["STASH_PATH"] || "~/.stash").stash(ARGV, :message => ENV["STASH_MSG"])
end

Instance Method Details

#stash(files, options = {}) ⇒ Object

Perform the stash.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/stash.rb', line 25

def stash(files, options = {})
  validate files

  message    = options[:message].to_s.gsub(/\s/, ".").downcase.gsub(/[^a-z0-9\.]/, "")
  node_note  = "-#{message}" unless message.empty?
  node_name  = Time.now.iso8601 + node_note.to_s
  node_path  = File.join(@path, node_name)

  FileUtils.mkdir_p node_path

  ARGV.each do |src|
    dst = File.join(node_path, File.basename(src))
    FileUtils.mv src, dst
  end
rescue Exception => e
  puts e.message
end

#validate(files) ⇒ Object

Validate all specified files.



14
15
16
17
18
19
20
21
22
# File 'lib/stash.rb', line 14

def validate(files)
  rejected_files = files.find_all { |filename| !File.exists? filename }
  if rejected_files.count > 0
    rejected_files.each { |filename| puts "cannot find file: #{filename}" }
    raise "stash not performed"
  end

  self
end