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.1.0"- 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
- #bounded? ⇒ Boolean
-
#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 |
# File 'lib/dancer/factories.rb', line 13 def self.extent(start_at, size, step, exclude_end = false) end_at = start_at + (step * size) - 1 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
20 21 22 23 24 25 |
# File 'lib/dancer/factories.rb', line 20 def self.keys(keys, step, exclude_end = false) start_at = keys.min end_at = keys.max ? (keys.max + step - 1) : 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
#bounded? ⇒ Boolean
91 92 93 |
# File 'lib/dancer.rb', line 91 def bounded? start_at && end_at end |
#duration ⇒ Object Also known as: to_i
Number of seconds covered by the slice
83 84 85 86 87 |
# File 'lib/dancer.rb', line 83 def duration return unless bounded? (end_at - start_at).abs + 1 end |
#each_range ⇒ Object
Enumerator for each time range
68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/dancer.rb', line 68 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, 1)) current_range = current..current_end_at yield current_range current = current.public_send(operator, step) end self end |
#each_time ⇒ Object
Enumerator for each start time
55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/dancer.rb', line 55 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
99 100 101 |
# File 'lib/dancer.rb', line 99 def inspect "#<Dancer #{to_s}>" end |
#range ⇒ Object
Range of start and end of slice
48 49 50 51 52 |
# File 'lib/dancer.rb', line 48 def range return unless bounded? start_at..end_at 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, 1) - start_at) / step).abs end |
#to_s ⇒ Object
95 96 97 |
# File 'lib/dancer.rb', line 95 def to_s "#{start_at.inspect}..#{end_at.inspect} (step: #{step})" end |