Class: ReVIEW::Book::ImageFinder

Inherits:
Object
  • Object
show all
Defined in:
lib/review/book/image_finder.rb

Instance Method Summary collapse

Constructor Details

#initialize(basedir, chapid, builder, exts) ⇒ ImageFinder

Returns a new instance of ImageFinder.



16
17
18
19
20
21
22
# File 'lib/review/book/image_finder.rb', line 16

def initialize(basedir, chapid, builder, exts)
  @basedir = basedir
  @chapid = chapid
  @builder = builder
  @exts = exts
  @entries = dir_entries
end

Instance Method Details

#add_entry(path) ⇒ Object



28
29
30
31
# File 'lib/review/book/image_finder.rb', line 28

def add_entry(path)
  @entries << path unless @entries.include?(path)
  @entries
end

#dir_entriesObject



24
25
26
# File 'lib/review/book/image_finder.rb', line 24

def dir_entries
  Dir.glob(File.join(@basedir, '**{,/*/**}/*.*')).uniq.sort
end

#find_path(id) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/review/book/image_finder.rb', line 33

def find_path(id)
  targets = target_list(id)
  targets.each do |target|
    @exts.each do |ext|
      @entries.find do |entry|
        downname = entry.sub(/\.[^.]+$/, File.extname(entry).downcase)
        if downname == "#{target}#{ext}"
          return entry
        end
      end
    end
  end
  nil
end

#target_list(id) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/review/book/image_finder.rb', line 48

def target_list(id)
  [
    # 1. <basedir>/<builder>/<chapid>/<id>.<ext>
    "#{@basedir}/#{@builder}/#{@chapid}/#{id}",

    # 2. <basedir>/<builder>/<chapid>-<id>.<ext>
    "#{@basedir}/#{@builder}/#{@chapid}-#{id}",

    # 3. <basedir>/<builder>/<id>.<ext>
    "#{@basedir}/#{@builder}/#{id}",

    # 4. <basedir>/<chapid>/<id>.<ext>
    "#{@basedir}/#{@chapid}/#{id}",

    # 5. <basedir>/<chapid>-<id>.<ext>
    "#{@basedir}/#{@chapid}-#{id}",

    # 6. <basedir>/<id>.<ext>
    "#{@basedir}/#{id}"
  ]
end