class Dancer

Constants

Defined
Infinity
VERSION

Attributes

comparator[R]
end_at[R]
operator[R]
start_at[R]
step[R]

Public Class Methods

extent(start_at, size, step, exclude_end = false) click to toggle source

Create a timeslice from a start time and a number of a points

# File lib/dancer/factories.rb, line 13
def self.extent(start_at, size, step, exclude_end = false)
  offset = exclude_end ? 0 : 1

  end_at = if size < 0
    start_at + (step * size) + offset
  else
    start_at + (step * size) - offset
  end

  new(start_at, end_at, step, exclude_end)
end
keys(keys, step, exclude_end = false) click to toggle source

Create a timeslice from a list of start times

# File lib/dancer/factories.rb, line 26
def self.keys(keys, step, exclude_end = false)
  offset = exclude_end ? 0 : 1

  start_at = keys.min

  end_at = keys.max ? (keys.max + (step * offset) - offset) : nil

  new(start_at, end_at, step, exclude_end)
end
new(start_at, end_at, step, exclude_end = false) click to toggle source

Start can be a Time or an Integer End can be a Time or an Integer Step is an Integer

# File lib/dancer.rb, line 10
def initialize(start_at, end_at, step, exclude_end = false)
  unless step
    fail ArgumentError, "step is required"
  end

  @start_at = start_at
  @end_at = end_at
  @step = step
  @exclude_end = exclude_end

  if start_at && end_at
    if start_at > end_at
      @operator = :-
      @comparator = :>
    else
      @operator = :+
      @comparator = :<
    end
  end
end
range(range, step) click to toggle source

Create a timeslice from a range

# File lib/dancer/factories.rb, line 8
def self.range(range, step)
  new(range.begin, range.end, step, range.exclude_end?)
end
unbounded(step) click to toggle source

Create an unbounded timeslice

# File lib/dancer/factories.rb, line 3
def self.unbounded(step)
  new(nil, nil, step)
end

Public Instance Methods

count()
Alias for: size
duration() click to toggle source

Number of seconds covered by the slice

# File lib/dancer.rb, line 94
def duration
  return unless bounded?

  (end_at - start_at).abs.to_i + offset
end
Also aliased as: to_i
each_range() { |current_range| ... } click to toggle source

Enumerator for each time range

# File lib/dancer.rb, line 72
def each_range
  return enum_for(:each_range) unless block_given?
  return self unless bounded?

  current = start_at
  while current.public_send(comparator, end_at)
    current_end_at = current.public_send(operator, step) - (0.public_send(operator, offset))

    current_range = if exclude_end?
      current...current_end_at
    else
      current..current_end_at
    end

    yield current_range

    current = current.public_send(operator, step)
  end
  self
end
each_time() { |current| ... } click to toggle source

Enumerator for each start time

# File lib/dancer.rb, line 59
def each_time
  return enum_for(:each_time) unless block_given?
  return self unless bounded?

  current = start_at
  while current.public_send(comparator, end_at)
    yield current
    current = current.public_send(operator, step)
  end
  self
end
exclude_end?() click to toggle source
# File lib/dancer.rb, line 33
def exclude_end?
  @exclude_end
end
inspect() click to toggle source
# File lib/dancer.rb, line 106
def inspect
  "#<Dancer #{to_s}>"
end
length()
Alias for: size
range() click to toggle source

Range of start and end of slice

# File lib/dancer.rb, line 48
def range
  return unless bounded?

  if exclude_end?
    start_at...end_at
  else
    start_at..end_at
  end
end
size() click to toggle source

Total number of points in the range

# File lib/dancer.rb, line 38
def size
  return unless bounded?

  ((end_at.public_send(operator, offset) - start_at) / step).abs.to_i
end
Also aliased as: count, length
to_i()
Alias for: duration
to_s() click to toggle source
# File lib/dancer.rb, line 102
def to_s
  "#{start_at.inspect}#{exclude_end? ? "..." : ".."}#{end_at.inspect} (step: #{step})"
end

Protected Instance Methods

bounded?() click to toggle source
# File lib/dancer.rb, line 112
def bounded?
  start_at && end_at
end
offset() click to toggle source
# File lib/dancer.rb, line 116
def offset
  exclude_end? ? 0 : 1
end