Module: Jpdfer::PageRangeUtilities

Defined in:
lib/jpdfer/page_range_utilities.rb

Class Method Summary collapse

Class Method Details

.expand_page_range(page_range) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/jpdfer/page_range_utilities.rb', line 10

def expand_page_range page_range
  page_range.split(',').reduce([]) do |result, sub_range|
    if sub_range.include? '-'
      raise "Invalid Range: #{sub_range}" unless sub_range =~ /^\d+-\d+$/
      first, last = sub_range.split '-'
      result.concat (first.to_i..last.to_i).to_a
    else
      raise "Invalid Range: #{sub_range}" unless sub_range.to_i.to_s == sub_range
      result << sub_range.to_i
    end
  end
end

.normalize_page_range(page_range) ⇒ Object



5
6
7
8
# File 'lib/jpdfer/page_range_utilities.rb', line 5

def normalize_page_range page_range
  pages = expand_page_range page_range
  split_list pages
end

.split_list(list) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/jpdfer/page_range_utilities.rb', line 23

def split_list list
  list.reduce([[]]) do |result, number|
    current = result.last
    if current.include? number
      result << [number]
    else
      current << number
      result
    end
  end
end