class Weesked::OffsetHandler

Attributes

ary[R]
length[R]
offset[R]

Public Class Methods

new(ary = [], offset = 0, length = 24) click to toggle source
# File lib/weesked/offset_handler.rb, line 4
def initialize(ary = [], offset = 0, length = 24)
  @ary = ary.sort
  @offset = offset
  @length = length
end

Public Instance Methods

to_a() click to toggle source
# File lib/weesked/offset_handler.rb, line 10
def to_a
  return ary if ary.length == 0 || offset == 0
  shift_offset = ary.take_while {|n| n < offset}.length
  ary.push(ary.shift(shift_offset)).flatten
end
to_range() click to toggle source
# File lib/weesked/offset_handler.rb, line 16
def to_range
  to_a.inject([]) do |spans, n|
    if start_new_range? spans, n
      spans + [n..n]
    else
      spans[0..-2] + [spans.last.first..n]
    end
  end
end

Private Instance Methods

consecutive?(x, y) click to toggle source
# File lib/weesked/offset_handler.rb, line 48
def consecutive? x, y
  x == y - 1
end
consecutive_with_offset?(x, y) click to toggle source
# File lib/weesked/offset_handler.rb, line 44
def consecutive_with_offset? x, y
  x % max == y && max?(x)
end
max() click to toggle source
# File lib/weesked/offset_handler.rb, line 40
def max
  length - 1
end
max?(n) click to toggle source
# File lib/weesked/offset_handler.rb, line 36
def max? n
  n == max
end
start_new_range?(spans, current) click to toggle source
# File lib/weesked/offset_handler.rb, line 30
def start_new_range? spans, current
  return true if spans.empty?
  previous = spans.last.last
  return true unless consecutive?(previous, current) || consecutive_with_offset?(previous, current)
end