Class: ZoneRanger::Core

Inherits:
Object
  • Object
show all
Defined in:
lib/zone_ranger/core.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(start_date_time, duration_in_minutes, timezone, options = {}) ⇒ Core

Returns a new instance of Core.



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/zone_ranger/core.rb', line 9

def initialize start_date_time, duration_in_minutes, timezone, options={}

  @start_time_string = start_date_time
  @duration_in_minutes = duration_in_minutes
  @timezone = timezone

  @ending = options.fetch(:ending, nil)

  @repeat_type = options.fetch(:repeat, nil)

end

Instance Attribute Details

#duration_in_minutesObject (readonly)

Returns the value of attribute duration_in_minutes.



7
8
9
# File 'lib/zone_ranger/core.rb', line 7

def duration_in_minutes
  @duration_in_minutes
end

#repeat_typeObject (readonly)

Returns the value of attribute repeat_type.



6
7
8
# File 'lib/zone_ranger/core.rb', line 6

def repeat_type
  @repeat_type
end

#start_time_stringObject (readonly)

Returns the value of attribute start_time_string.



6
7
8
# File 'lib/zone_ranger/core.rb', line 6

def start_time_string
  @start_time_string
end

Instance Method Details

#daily?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/zone_ranger/core.rb', line 25

def daily?
  @repeat_type == :daily
end

#expired?(time = Time.now.utc) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
62
# File 'lib/zone_ranger/core.rb', line 59

def expired? time=Time.now.utc
  return false unless @ending
  zoned_time(time) > zoned_end_date
end

#includes?(time_point = Time.now.utc) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
48
49
50
51
52
53
# File 'lib/zone_ranger/core.rb', line 45

def includes? time_point=Time.now.utc
  return false if not_started_yet?(time_point) || expired?(time_point)

  if repeat?
    included_in_repeating_timeframe? time_point
  else
    static_include? time_point
  end
end

#monthly_by_day_of_month?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/zone_ranger/core.rb', line 37

def monthly_by_day_of_month?
  @repeat_type == :monthly_by_day_of_month
end

#monthly_by_day_of_week?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/zone_ranger/core.rb', line 33

def monthly_by_day_of_week?
  @repeat_type == :monthly_by_day_of_week
end

#not_started_yet?(time) ⇒ Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/zone_ranger/core.rb', line 85

def not_started_yet? time
  start_on > zoned_date
end

#parsed_time_stringObject



117
118
119
# File 'lib/zone_ranger/core.rb', line 117

def parsed_time_string
  timezone_object.parse(start_time_string)
end

#repeat?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/zone_ranger/core.rb', line 21

def repeat?
  !!@repeat_type
end

#start_onObject



89
90
91
# File 'lib/zone_ranger/core.rb', line 89

def start_on
  parsed_time_string.to_date
end

#started?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/zone_ranger/core.rb', line 93

def started?
  !not_started_yet?
end

#static_include?(time_point) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/zone_ranger/core.rb', line 55

def static_include? time_point
  zoned_time(time_point).between?(*time_range(time_point))
end

#time_range(current_day = Time.now.utc, options = {}) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/zone_ranger/core.rb', line 97

def time_range current_day=Time.now.utc, options={}
  
  offset = options.fetch(:offset, 0)

  start_date = if repeat?
    #if daily?
    # shift to the day being checked
    start_time = zoned_time(current_day + offset.days)
    start_time.to_date
    #end
  else
    parsed_time_string.to_date
  end

  b_start = timezone_object.parse(parsed_time_string.strftime("#{start_date} %H:%M")).in_time_zone(timezone_object)
  b_end = b_start + duration_in_minutes.minutes

  [b_start, b_end]
end

#timezone_objectObject



41
42
43
# File 'lib/zone_ranger/core.rb', line 41

def timezone_object
  ActiveSupport::TimeZone.new((@timezone || "UTC"))
end

#utc_offsetObject



81
82
83
# File 'lib/zone_ranger/core.rb', line 81

def utc_offset
  ActiveSupport::TimeZone.seconds_to_utc_offset(zoned_time.utc_offset)
end

#weekly?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/zone_ranger/core.rb', line 29

def weekly?
  @repeat_type == :weekly
end

#zoned_date(time = Time.now.utc) ⇒ Object



73
74
75
# File 'lib/zone_ranger/core.rb', line 73

def zoned_date time=Time.now.utc
  zoned_time(time).to_date
end

#zoned_end_dateObject



77
78
79
# File 'lib/zone_ranger/core.rb', line 77

def zoned_end_date
  timezone_object.parse("#{@ending} 23:59:59 #{utc_offset}")
end

#zoned_time(time = nil) ⇒ Object



64
65
66
67
68
69
70
71
# File 'lib/zone_ranger/core.rb', line 64

def zoned_time time=nil
  tzone = timezone_object
  if time.nil?
    tzone.now
  else
    time.in_time_zone(tzone)
  end
end