Class: TimeSlice

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/time_slice.rb,
lib/time_slice/version.rb,
lib/time_slice/duration.rb

Overview

The Duration class extends ActiveSupport::Duration to provide a more flexible way of specifying and working with time durations.

It allows creation of duration objects from string representations and provides methods for working with these durations.

Supported Units

  • s: seconds

  • m: minutes

  • h: hours

  • d: days

  • w: weeks

  • mo: months

  • y: years

Usage

Duration.new("5m")  # => 5 minutes
Duration.new("2h")  # => 2 hours
Duration.new("1d")  # => 1 day

Examples

duration = Duration.new("3h")
duration.unit  # => :hours
duration.period  # => "3h"
duration * 2  # => 21600 (number of seconds in 6 hours)

Defined Under Namespace

Classes: Duration

Constant Summary collapse

VERSION =
"0.1"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(period, options = {}) ⇒ TimeSlice



47
48
49
50
51
# File 'lib/time_slice.rb', line 47

def initialize(period, options = {})
  @duration = Duration.new(period)
  parse_time_options(options)
  set_time_range(options)
end

Instance Attribute Details

#durationObject (readonly)

Returns the value of attribute duration.



45
46
47
# File 'lib/time_slice.rb', line 45

def duration
  @duration
end

#fromObject (readonly)

Returns the value of attribute from.



45
46
47
# File 'lib/time_slice.rb', line 45

def from
  @from
end

#toObject (readonly)

Returns the value of attribute to.



45
46
47
# File 'lib/time_slice.rb', line 45

def to
  @to
end

Instance Method Details

#[](index) ⇒ Object



70
71
72
73
74
# File 'lib/time_slice.rb', line 70

def [](index)
  return nil if index >= length
  at = @from + (index * duration.to_i)
  [at, at + duration.to_i]
end

#eachObject



65
66
67
68
# File 'lib/time_slice.rb', line 65

def each
  return to_enum(:each) unless block_given?
  length.times { |i| yield(self[i]) }
end

#index(starts_at) ⇒ Object



84
85
86
87
88
# File 'lib/time_slice.rb', line 84

def index(starts_at)
  starts_at = parse_time(starts_at)
  index = ((starts_at - @from) / duration.to_i).to_i
  index >= 0 && index < length ? index : nil
end

#lastObject



76
77
78
# File 'lib/time_slice.rb', line 76

def last
  self[length - 1]
end

#lengthObject



57
58
59
# File 'lib/time_slice.rb', line 57

def length
  ((@to - @from) / duration.to_i).to_i + 1
end

#periodObject



53
54
55
# File 'lib/time_slice.rb', line 53

def period
  duration.period
end

#previous(starts_at, n) ⇒ Object



90
91
92
93
# File 'lib/time_slice.rb', line 90

def previous(starts_at, n)
  starts_at = parse_time(starts_at) - duration.to_i
  self.class.new(period, to: starts_at, length: n)
end

#rangeObject



61
62
63
# File 'lib/time_slice.rb', line 61

def range
  from..to
end

#slice(index, length) ⇒ Object



80
81
82
# File 'lib/time_slice.rb', line 80

def slice(index, length)
  length.times.map { |i| self[index + i] }.compact
end