Class: PdfExtract::MultiRange

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMultiRange

Returns a new instance of MultiRange.



7
8
9
# File 'lib/multi_range.rb', line 7

def initialize
  @ranges = []
end

Instance Attribute Details

#rangesObject

Returns the value of attribute ranges.



5
6
7
# File 'lib/multi_range.rb', line 5

def ranges
  @ranges
end

Instance Method Details

#append(range) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/multi_range.rb', line 11

def append range
  return if range.max.nil? || range.min.nil?

  incident = @ranges.select do |r|
    r.include?(range.min) || r.include?(range.max) ||
      range.include?(r.min) || range.include?(r.max)
  end

  incident << range

  non_incident = @ranges - incident

  non_incident << (incident.collect { |r| r.min }.min .. incident.collect { |r| r.max }.max)
  @ranges = non_incident

  @max_excluded = nil
  @min_excluded = nil
  @max = nil
  @min = nil
end

#avgObject



66
67
68
# File 'lib/multi_range.rb', line 66

def avg
  @ranges.reduce(0) { |sum, r| sum += (r.max - r.min) } / @ranges.count.to_f
end

#countObject



74
75
76
# File 'lib/multi_range.rb', line 74

def count
  @ranges.count
end

#coveredObject



70
71
72
# File 'lib/multi_range.rb', line 70

def covered
  @ranges.reduce(0) { |total, r| total += (r.max - r.min) }
end

#maxObject



48
49
50
# File 'lib/multi_range.rb', line 48

def max
  @ranges.sort_by { |r| -r.max }.first.max
end

#max_excludedObject



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

def max_excluded
  if @max_excluded.nil?
    @max_excluded = @ranges.first.max if count == 1
    @max_excluded = @ranges.sort_by { |r| -r.min }.first.min unless count == 1
  end
  @max_excluded
end

#minObject



52
53
54
# File 'lib/multi_range.rb', line 52

def min
  @ranges.sort_by { |r| r.min }.first.min
end

#min_excludedObject



40
41
42
43
44
45
46
# File 'lib/multi_range.rb', line 40

def min_excluded
  if @min_excluded.nil?
    @min_excluded = @ranges.first.min if count == 1
    @min_excluded = @ranges.sort_by { |r| r.max }.first.max unless count == 1
  end
  @min_excluded
end

#narrowestObject



61
62
63
64
# File 'lib/multi_range.rb', line 61

def narrowest
  narrowest = @ranges.sort_by { |r| r.max - r.min }.first
  narrowest.max - narrowest.min
end

#widestObject



56
57
58
59
# File 'lib/multi_range.rb', line 56

def widest
  widest = @ranges.sort_by { |r| r.max - r.min }.last
  widest.max - widest.min
end