Class: CamelotKeyConverter::Converter

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

Defined Under Namespace

Classes: ExitError

Constant Summary collapse

MAP_FILE =
File.expand_path('~/.key_convert_map.yml')

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConverter

Returns a new instance of Converter.



12
13
14
15
16
17
# File 'lib/camelot_key_converter/converter.rb', line 12

def initialize
  self.ignored = []
  self.map = {}
  self.should_convert_all = false
  try_load_file
end

Instance Attribute Details

#ignoredObject

Returns the value of attribute ignored.



10
11
12
# File 'lib/camelot_key_converter/converter.rb', line 10

def ignored
  @ignored
end

#mapObject

Returns the value of attribute map.



10
11
12
# File 'lib/camelot_key_converter/converter.rb', line 10

def map
  @map
end

#should_convert_allObject

Returns the value of attribute should_convert_all.



10
11
12
# File 'lib/camelot_key_converter/converter.rb', line 10

def should_convert_all
  @should_convert_all
end

Instance Method Details

#cliObject



27
28
29
# File 'lib/camelot_key_converter/converter.rb', line 27

def cli
  @cli ||= HighLine.new
end

#convert(path) ⇒ Object



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
66
67
68
# File 'lib/camelot_key_converter/converter.rb', line 40

def convert(path)
  if path.file?
    convert_file! path
    return
  end

  path.children.each do |child|
    if self.should_convert_all
      convert(child)
      next
    end

    cli.choose do |menu|
      menu.prompt = "What to do next?"
      menu.choice(:convert, "Convert #{child}") do
        convert(child)
      end

      menu.choice(:exit) do
        raise ExitError
      end

      menu.choice(:convert_all) do
        self.should_convert_all = true
        convert(child)
      end
    end
  end
end

#convert_file!(path) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/camelot_key_converter/converter.rb', line 78

def convert_file!(path)
  TagLib::MPEG::File.open(path.to_s) do |file|
    tag = file.id3v2_tag
    key = tag.frame_list('TKEY').first

    if key.nil?
      cli.say "no key found for #{path}"
      return
    end

    if key.field_list.length != 1
      binding.pry
      return
    end

    if ignored.include? key.to_s
      cli.say("#{key.to_s} is ignored")
      return
    end

    if map.values.include? key.to_s
      cli.say("#{key.to_s} already correct value")
      return
    end

    new_key = map[key.to_s]
    unless new_key
      result = cli.choose do |menu|
        menu.prompt = "Cant find #{key.to_s}. What to you want to do?"
        menu.choice(:add)
        menu.choice(:ignore) do
          ignored << key.to_s
          return
        end
        menu.choice(:exit) do
          raise ExitError
        end
      end

      # can only be :add because :ignore and :exit return

      loop do
        new_key = cli.ask('Enter the replacement and press enter: ')
        if map.keys.include?(new_key)
          cli.say('This value does already exist as key, which is not allowed to prevent loops.')
        else
          break
        end
      end

      if cli.agree('Do you want to save the replacement?')
        map[key.to_s] = new_key
      end
    end

    cli.say("#{path.to_s} - #{new_key}")

    # don't alter file if nothing changed
    return if new_key == key.to_s

    key.field_list = [new_key]
    file.save
  end
end

#run!Object



19
20
21
22
23
24
25
# File 'lib/camelot_key_converter/converter.rb', line 19

def run!
  convert(Pathname.new('.'))
rescue ExitError => e
  cli.say('exiting ...')
ensure
  save_config!
end

#save_config!Object



31
32
33
34
35
36
37
38
# File 'lib/camelot_key_converter/converter.rb', line 31

def save_config!
  File.open(MAP_FILE, 'w') do |f|
    f.write({
      "ignored" => ignored,
      "map" => map
    }.to_yaml)
  end
end

#try_load_fileObject



70
71
72
73
74
75
# File 'lib/camelot_key_converter/converter.rb', line 70

def try_load_file
  x = YAML::load_file(MAP_FILE)
  self.ignored = x['ignored'] || []
  self.map = x['map'] || {}
rescue
end