Class: Rutaci::Setter

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

Overview

writes info from a source into the files it is a command executor, so it has tp provide an initializer which takes the options and a method ‘run’ which takes an array of filenames

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Setter

Returns a new instance of Setter.



41
42
43
44
45
46
47
48
49
# File 'lib/rutaci/setter.rb', line 41

def initialize(options)
  @options = options
  if @options.interactive
    require 'rutaci/interaction'
    puts "Using interactive mode you can specify the value for the current field."
    Interaction::print_lineedit_help
    puts
  end
end

Instance Method Details

#run(files) ⇒ Object

invoke this executor for an array of filenames



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/rutaci/setter.rb', line 52

def run(files)
  files.each do |file|
    if @options.dry_run
      puts "dry run for #{file}:"
    else
      puts "processing #{file}:"
    end
    self.set(file)
    puts unless file == files.last # seperator between the files
  end
end

#set(file) ⇒ Object

gets writes the actual info into a specific file



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/rutaci/setter.rb', line 65

def set(file)
  source = Source.factory file, @options
  tagfile = TagFile::File.new file
  source.info.each_pair do |key, value|
    value = Interaction::lineedit "  #{key.to_s.capitalize}: ", value if @options.interactive
    if value.empty? and not @options.keep_empty_fields
      puts "  ignoring empty value"
      next
    end
    puts "  setting #{key} to \"#{value}\""
    tagfile.send key.to_s+"=", value unless @options.dry_run
  end
  if @options.dry_run
    puts "  writing the metadata (if it was not a dry run ;-)"
  else
    puts "  writing the metadata"
    tagfile.save
  end
end