Class: ReVIEW::Book::ImageFinder

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

Instance Method Summary collapse

Constructor Details

#initialize(chapter) ⇒ 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(chapter)
  @book = chapter.book
  @basedir = @book.imagedir
  @chapid = chapter.id
  @builder = @book.config['builder']
  @entries = dir_entries.map { |path| entry_object(path) }
end

Instance Method Details

#add_entry(path) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/review/book/image_finder.rb', line 32

def add_entry(path)
  path.sub!(%r{^\./}, '')
  unless @entries.find { |entry| entry[:path] == path }
    @entries << entry_object(path)
  end
  @entries
end

#dir_entriesObject



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

def dir_entries
  Dir.glob(File.join(@basedir, '**{,/*/**}/*.*')).uniq.sort.map { |entry| entry.sub(%r{^\./}, '') }
end

#entry_object(path) ⇒ Object



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

def entry_object(path)
  { path: path, basename: path.sub(/\.[^.]+$/, ''), downcase: path.sub(/\.[^.]+$/, $&.downcase) }
end

#find_path(id) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/review/book/image_finder.rb', line 40

def find_path(id)
  targets = target_list(id)
  targets.each do |target|
    @book.image_types.each do |ext|
      entries = @entries.select do |entry|
        entry[:basename] == target
      end

      unless entries
        break
      end

      entries.find do |entry|
        if entry[:downcase] == "#{target}#{ext}"
          return entry[:path]
        end
      end
    end
  end

  nil
end

#target_list(id) ⇒ Object



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

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