Class: CacheManager

Inherits:
Object
  • Object
show all
Defined in:
lib/lyrics_ebook/cache_manager.rb

Instance Method Summary collapse

Constructor Details

#initialize(base_dir = 'data') ⇒ CacheManager

Returns a new instance of CacheManager.



6
7
8
# File 'lib/lyrics_ebook/cache_manager.rb', line 6

def initialize(base_dir='data')
  @base_dir=base_dir
end

Instance Method Details

#alternative_dir=(alt_dir) ⇒ Object



10
11
12
# File 'lib/lyrics_ebook/cache_manager.rb', line 10

def alternative_dir=(alt_dir)
  @alt_dir=alt_dir
end

#get(lyric) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/lyrics_ebook/cache_manager.rb', line 14

def get(lyric)
  file=lyric2file(lyric)
  if !File.exists?(file) && !@alt_dir.nil?
    file=lyric2file(lyric,false,@alt_dir)
  end 
  return false unless File.exists?(file)
  read_lyric_file(file,lyric)
end

#get_lyricsObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/lyrics_ebook/cache_manager.rb', line 31

def get_lyrics
  res=[]
  Dir.foreach(@base_dir) do |artist_dir|
    artist_dir_path=File.join(@base_dir,artist_dir)
    if File.directory?(artist_dir_path)
      Dir.foreach(artist_dir_path) do |lyric_file|
        ext=File.extname(lyric_file)
        if ext==".lyric"
          basename=File.basename(lyric_file,ext)
          lyric_file_path=File.join(artist_dir_path,lyric_file)
          lyric=Lyric.new({:artist=>artist_dir, :title=>basename})
          read_lyric_file(lyric_file_path,lyric)
          res << lyric
        end
      end
    end
  end
  res
end

#store(lyric) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/lyrics_ebook/cache_manager.rb', line 23

def store(lyric)
  file=lyric2file(lyric,true)
  File.open(file,'w') do |f|
    f << "#{lyric.artist} - #{lyric.title}\n\n".toutf8
    f << lyric.text.toutf8
  end
end