Class: Dancer
- Inherits:
-
Object
- Object
- Dancer
- Defined in:
- lib/dancer.rb,
lib/dancer/defined.rb,
lib/dancer/version.rb,
lib/dancer/constants.rb,
lib/dancer/factories.rb
Defined Under Namespace
Classes: Defined
Constant Summary collapse
- VERSION =
"0.9.1"- Infinity =
Float::INFINITY
Instance Attribute Summary collapse
-
#comparator ⇒ Object
readonly
Returns the value of attribute comparator.
-
#end_at ⇒ Object
readonly
Returns the value of attribute end_at.
-
#operator ⇒ Object
readonly
Returns the value of attribute operator.
-
#start_at ⇒ Object
readonly
Returns the value of attribute start_at.
-
#step ⇒ Object
readonly
Returns the value of attribute step.
Class Method Summary collapse
-
.extent(start_at, size, step, exclude_end = false) ⇒ Object
Create a timeslice from a start time and a number of a points.
-
.keys(keys, step, exclude_end = false) ⇒ Object
Create a timeslice from a list of start times.
-
.range(range, step) ⇒ Object
Create a timeslice from a range.
-
.unbounded(step) ⇒ Object
Create an unbounded timeslice.
Instance Method Summary collapse
-
#duration ⇒ Object
(also: #to_i)
Number of seconds covered by the slice.
-
#each_range ⇒ Object
Enumerator for each time range.
-
#each_time ⇒ Object
Enumerator for each start time.
- #exclude_end? ⇒ Boolean
-
#initialize(start_at, end_at, step, exclude_end = false) ⇒ Dancer
constructor
Start can be a Time or an Integer End can be a Time or an Integer Step is an Integer.
- #inspect ⇒ Object
-
#range ⇒ Object
Range of start and end of slice.
-
#size ⇒ Object
(also: #count, #length)
Total number of points in the range.
- #to_s ⇒ Object
Constructor Details
#initialize(start_at, end_at, step, exclude_end = false) ⇒ Dancer
Start can be a Time or an Integer End can be a Time or an Integer Step is an Integer
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# 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 |
Instance Attribute Details
#comparator ⇒ Object (readonly)
Returns the value of attribute comparator.
31 32 33 |
# File 'lib/dancer.rb', line 31 def comparator @comparator end |
#end_at ⇒ Object (readonly)
Returns the value of attribute end_at.
31 32 33 |
# File 'lib/dancer.rb', line 31 def end_at @end_at end |
#operator ⇒ Object (readonly)
Returns the value of attribute operator.
31 32 33 |
# File 'lib/dancer.rb', line 31 def operator @operator end |
#start_at ⇒ Object (readonly)
Returns the value of attribute start_at.
31 32 33 |
# File 'lib/dancer.rb', line 31 def start_at @start_at end |
#step ⇒ Object (readonly)
Returns the value of attribute step.
31 32 33 |
# File 'lib/dancer.rb', line 31 def step @step end |
Class Method Details
.extent(start_at, size, step, exclude_end = false) ⇒ Object
Create a timeslice from a start time and a number of a points
13 14 15 16 17 18 19 20 21 22 23 |
# 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) ⇒ Object
Create a timeslice from a list of start times
26 27 28 29 30 31 32 33 34 |
# 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 |
.range(range, step) ⇒ Object
Create a timeslice from a range
8 9 10 |
# File 'lib/dancer/factories.rb', line 8 def self.range(range, step) new(range.begin, range.end, step, range.exclude_end?) end |
.unbounded(step) ⇒ Object
Create an unbounded timeslice
3 4 5 |
# File 'lib/dancer/factories.rb', line 3 def self.unbounded(step) new(nil, nil, step) end |
Instance Method Details
#duration ⇒ Object Also known as: to_i
Number of seconds covered by the slice
94 95 96 97 98 |
# File 'lib/dancer.rb', line 94 def duration return unless bounded? (end_at - start_at).abs.to_i + offset end |
#each_range ⇒ Object
Enumerator for each time range
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# 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 ⇒ Object
Enumerator for each start time
59 60 61 62 63 64 65 66 67 68 69 |
# 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? ⇒ Boolean
33 34 35 |
# File 'lib/dancer.rb', line 33 def exclude_end? @exclude_end end |
#inspect ⇒ Object
106 107 108 |
# File 'lib/dancer.rb', line 106 def inspect "#<Dancer #{to_s}>" end |
#range ⇒ Object
Range of start and end of slice
48 49 50 51 52 53 54 55 56 |
# 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 ⇒ Object Also known as: count, length
Total number of points in the range
38 39 40 41 42 |
# File 'lib/dancer.rb', line 38 def size return unless bounded? ((end_at.public_send(operator, offset) - start_at) / step).abs.to_i end |
#to_s ⇒ Object
102 103 104 |
# File 'lib/dancer.rb', line 102 def to_s "#{start_at.inspect}#{exclude_end? ? "..." : ".."}#{end_at.inspect} (step: #{step})" end |