Class: Mangos::PagesDeflater

Inherits:
Object
  • Object
show all
Defined in:
lib/mangos/pages_deflater.rb

Instance Method Summary collapse

Constructor Details

#initialize(page_paths) ⇒ PagesDeflater

Returns a new instance of PagesDeflater.



2
3
4
# File 'lib/mangos/pages_deflater.rb', line 2

def initialize(page_paths)
  @page_paths = page_paths
end

Instance Method Details

#deflateObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/mangos/pages_deflater.rb', line 6

def deflate
  last_ext = nil
  last_base = nil
  last_count = 0

  out = []

  @page_paths.each do |path|
    path_parts = path.rpartition(".")
    base, ext = path_parts.first, path_parts.last

    if last_base.nil? || (ext != last_ext) || (base != last_base.succ)
      if last_count > 0
        out.last.replace("#{out.last}/#{last_count}")
        last_count = 0
      end

      out << path
    else
      last_count += 1
    end

    last_base = base
    last_ext = ext
  end

  if last_count > 0
    out.last.replace("#{out.last}/#{last_count}")
  end

  out.join("|")
end