Class: Rutaci::Getter

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

Overview

gets info from a source and display it 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) ⇒ Getter

Returns a new instance of Getter.



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rutaci/getter.rb', line 25

def initialize(options)
  @options = options
  if @options.format
    require 'rutaci/formater'
    # make sure we get all fields nessesary for the expanding the format string
    @options.fields = Array.new
    Formater::PLACEHOLDERS.each_pair do |token, field|
      @options.fields.push field if /%[^a-z]*#{token}/.match @options.format
    end
  end
end

Instance Method Details

#format(info, format) ⇒ Object

return the info formated, format may be something like “%n - %t”



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/rutaci/getter.rb', line 67

def format(info, format)
  str = String.new
  Formater.tokenize format do |token, is_placeholder|
    if is_placeholder
      str += Formater.string_for_token token, info
    else
      str += token
    end
  end
  return str
end

#get(file) ⇒ Object

get and return the actual info for a specific file



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/rutaci/getter.rb', line 47

def get(file)
  source = Source.factory file, @options
  # use the format string if available
  return self.format(source.info, @options.format) if @options.format
  # esle create the standard format
  output = Array.new
  output.push "Filename: #{file}" unless @options.values_only
  source.info.each_pair do |key, value|
    value = nil if value == 0 # HACK: rtaglib seems to return non-existing numeric values as 0 instead of nil
    value = value.to_s
    next if value.empty? unless @options.keep_empty_fields
    element = String.new
    element += key.to_s.capitalize + ": " unless @options.values_only
    element += value
    output.push element
  end
  return output.join("\n")
end

#run(files) ⇒ Object

invoke this executor for an array of filenames



38
39
40
41
42
43
44
# File 'lib/rutaci/getter.rb', line 38

def run(files)
  output = Array.new
  files.each do |file|
    output.push self.get(file) + "\n"
  end
  puts output.join("\n") # a blank line between the items
end