Class: Melon::Commands::Add

Inherits:
Base
  • Object
show all
Defined in:
lib/melon/commands/add.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, #helptext, #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



8
9
10
# File 'lib/melon/commands/add.rb', line 8

def self.description
  "Add files to the melon database"
end

Instance Method Details

#parser_options(parser) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/melon/commands/add.rb', line 12

def parser_options(parser)
  parser.on("-q", "--quiet", "Suppress printing of hash and path") do
    options.quiet = true
  end

  CommonOptions.recursive(parser, options)

  parser.on("-u", "--update", 
            "Skip paths already present in the database") do
    options.update = true
  end

  CommonOptions.preserve_symlinks(parser, options)
end

#runObject



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
63
64
65
# File 'lib/melon/commands/add.rb', line 31

def run
  parse_options!

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

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

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

      if options.database[:by_path][filename]
        next if options.update
        error "path already present in database: #{arg}"
      end

      # hash strategy should be encapsulated, ergo indirection here
      hash = Hasher.digest(filename)

      if options.database[:by_hash][hash]
        error "file exists elsewhere in the database: #{arg}"
      end


      options.database[:by_hash][hash] = filename
      options.database[:by_path][filename] = hash
      puts "#{hash}:#{filename}" unless options.quiet
    end
  end
end

#usageargsObject



27
28
29
# File 'lib/melon/commands/add.rb', line 27

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