Class: PdfExtract::MultiRange
- Inherits:
-
Object
- Object
- PdfExtract::MultiRange
- Defined in:
- lib/multi_range.rb
Instance Attribute Summary collapse
-
#ranges ⇒ Object
Returns the value of attribute ranges.
Instance Method Summary collapse
- #append(range) ⇒ Object
- #avg ⇒ Object
- #count ⇒ Object
- #covered ⇒ Object
-
#initialize ⇒ MultiRange
constructor
A new instance of MultiRange.
- #max ⇒ Object
- #max_excluded ⇒ Object
- #min ⇒ Object
- #min_excluded ⇒ Object
- #narrowest ⇒ Object
- #widest ⇒ Object
Constructor Details
#initialize ⇒ MultiRange
Returns a new instance of MultiRange.
7 8 9 |
# File 'lib/multi_range.rb', line 7 def initialize @ranges = [] end |
Instance Attribute Details
#ranges ⇒ Object
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 |
#avg ⇒ Object
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 |
#count ⇒ Object
74 75 76 |
# File 'lib/multi_range.rb', line 74 def count @ranges.count end |
#covered ⇒ Object
70 71 72 |
# File 'lib/multi_range.rb', line 70 def covered @ranges.reduce(0) { |total, r| total += (r.max - r.min) } end |
#max ⇒ Object
48 49 50 |
# File 'lib/multi_range.rb', line 48 def max @ranges.sort_by { |r| -r.max }.first.max end |
#max_excluded ⇒ Object
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 |
#min ⇒ Object
52 53 54 |
# File 'lib/multi_range.rb', line 52 def min @ranges.sort_by { |r| r.min }.first.min end |
#min_excluded ⇒ Object
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 |
#narrowest ⇒ Object
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 |
#widest ⇒ Object
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 |