Class: Melon::Commands::Remove

Inherits:
Base
  • Object
show all
Defined in:
lib/melon/commands/remove.rb

Instance Attribute Summary

Attributes inherited from Base

#args, #description, #options

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

command_name, #initialize, #parse_options!, #parser, #usagebanner, #verify_args

Methods included from Helpers

#blockquote, #error, #expand_dir, #format_command, #recursively_expand, #resolve_symlinks, #wrap_text

Constructor Details

This class inherits a constructor from Melon::Commands::Base

Class Method Details

.descriptionObject



7
8
9
# File 'lib/melon/commands/remove.rb', line 7

def self.description
  "Remove a file from the database"
end

Instance Method Details

#helptextObject



11
12
13
14
15
16
17
18
19
20
# File 'lib/melon/commands/remove.rb', line 11

def helptext
  "For each argument, the database is checked for the path of that\nargument.  If present, it is removed.  If not, the database is\nchecked for the hash of the argument.  Once again, the record\nin the database is removed if found.\n\n If a file is found, the path that was in the database is printed.\n"
end

#parser_options(parser) ⇒ Object



26
27
28
# File 'lib/melon/commands/remove.rb', line 26

def parser_options(parser)
  CommonOptions.recursive(parser, options)
end

#runObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/melon/commands/remove.rb', line 30

def run
  parse_options!

  if options.recursive
    self.args = recursively_expand(args)
  end

  options.database.transaction do
    args.each do |arg|
      filename = File.expand_path(arg)
      filename = resolve_symlinks(filename)

      if File.directory?(filename)
        error "argument is a directory: #{arg}"
      end

      if options.database[:by_path][filename]
        hash = options.database[:by_path].delete(filename)
        options.database[:by_hash].delete(hash)
        puts filename
      else
        hash = Hasher.digest(filename)
        if options.database[:by_hash][hash]
          filename = options.database[:by_hash].delete(hash)
          options.database[:by_path].delete(filename)
          puts filename
        else
          error("untracked file: #{arg}")
        end
      end
    end
  end
end

#usageargsObject



22
23
24
# File 'lib/melon/commands/remove.rb', line 22

def usageargs
  "FILE [FILE [FILE ...]]"
end