Class: NitroRails::TimeInterval

Inherits:
Object
  • Object
show all
Defined in:
app/lib/nitro_rails/time_interval.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**attributes) ⇒ TimeInterval



27
28
29
# File 'app/lib/nitro_rails/time_interval.rb', line 27

def initialize(**attributes)
  set(**attributes)
end

Instance Attribute Details

#ended_atObject

Returns the value of attribute ended_at.



2
3
4
# File 'app/lib/nitro_rails/time_interval.rb', line 2

def ended_at
  @ended_at
end

#started_atObject

Returns the value of attribute started_at.



2
3
4
# File 'app/lib/nitro_rails/time_interval.rb', line 2

def started_at
  @started_at
end

Class Method Details

.from(started_at, **options) ⇒ Object



23
24
25
# File 'app/lib/nitro_rails/time_interval.rb', line 23

def self.from(started_at, **options)
  new(**options.merge(started_at: started_at))
end

.parse(string, date: Date.current, offset: 0) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'app/lib/nitro_rails/time_interval.rb', line 4

def self.parse(string, date: Date.current, offset: 0)
   if valid_time_interval_string?(string)
     times = string.split("-").map do |time| 
       time.to_time(:utc).change(
         offset: offset, 
         day: date.day, 
         month: date.month, 
         year: date.year
       )
     end

     new(started_at: times.first, ended_at: times.first > times.last ? times.last + 1.day : times.last)
   end
end

.valid_time_interval_string?(string) ⇒ Boolean



19
20
21
# File 'app/lib/nitro_rails/time_interval.rb', line 19

def self.valid_time_interval_string?(string)
  string.match?(/\A\s*#{NitroRails::TimeString::TIME_INTERVAL_REGEX}\s*\z/)
end

Instance Method Details

#change(**attributes) ⇒ Object



31
32
33
34
35
# File 'app/lib/nitro_rails/time_interval.rb', line 31

def change(**attributes)
  set(**attributes)
  reset_duration
  return self
end

#datesObject



55
56
57
# File 'app/lib/nitro_rails/time_interval.rb', line 55

def dates 
  [started_at.to_date, ended_at.to_date]
end

#durationObject



45
46
47
# File 'app/lib/nitro_rails/time_interval.rb', line 45

def duration
  @duration ||= ActiveSupport::Duration.build(ended_at - started_at)
end

#from(time) ⇒ Object



37
38
39
# File 'app/lib/nitro_rails/time_interval.rb', line 37

def from(time)
  change(started_at: time)
end

#inspectObject



70
71
72
# File 'app/lib/nitro_rails/time_interval.rb', line 70

def inspect 
  to_fs(:db)
end

#localtime(offset) ⇒ Object



59
60
61
62
63
64
# File 'app/lib/nitro_rails/time_interval.rb', line 59

def localtime(offset)
  NitroRails::TimeInterval.new(
    started_at: started_at.localtime(offset),
    ended_at: ended_at.localtime(offset)
  )
end

#timesObject Also known as: to_a



49
50
51
# File 'app/lib/nitro_rails/time_interval.rb', line 49

def times 
  [started_at, ended_at]
end

#to(time) ⇒ Object



41
42
43
# File 'app/lib/nitro_rails/time_interval.rb', line 41

def to(time)
  change(ended_at: time)
end

#to_fs(opt = :time, join: " - ") ⇒ Object



66
67
68
# File 'app/lib/nitro_rails/time_interval.rb', line 66

def to_fs(opt = :time, join: " - ")
  times.map { |time| time.to_fs(opt) }.join(join)
end