class Quby::RangeCategories

Public Class Methods

new(*range_categories) click to toggle source
# File lib/quby/range_categories.rb, line 5
def initialize(*range_categories)
  if range_categories.length.even? || range_categories.length < 3
    fail "RangeCategories should be of the form (0, 'label 0-10', 10, 'label 10-20', 20)"
  end

  @range_hash = {}
  parse_ranges(range_categories)
end

Public Instance Methods

as_range_hash() click to toggle source
# File lib/quby/range_categories.rb, line 14
def as_range_hash
  @range_hash.freeze
end

Private Instance Methods

add_range(from, label, to, inclusive:) click to toggle source
# File lib/quby/range_categories.rb, line 30
def add_range(from, label, to, inclusive:)
  if inclusive
    @range_hash[from..to] = label
  else
    @range_hash[from...to] = label
  end
end
parse_ranges(range_categories) click to toggle source
# File lib/quby/range_categories.rb, line 20
def parse_ranges(range_categories)
  a = range_categories.dup
  while a.length > 1
    from = a.shift.to_f
    label = a.shift
    to = a.first.to_f
    add_range(from, label, to, inclusive: a.length == 1)
  end
end