Class: ZoneRanger::Core
- Inherits:
-
Object
- Object
- ZoneRanger::Core
- Defined in:
- lib/zone_ranger/core.rb
Instance Attribute Summary collapse
-
#duration_in_minutes ⇒ Object
readonly
Returns the value of attribute duration_in_minutes.
-
#repeat_type ⇒ Object
readonly
Returns the value of attribute repeat_type.
-
#start_time_string ⇒ Object
readonly
Returns the value of attribute start_time_string.
Instance Method Summary collapse
- #daily? ⇒ Boolean
- #expired?(time = Time.now.utc) ⇒ Boolean
- #includes?(time_point = Time.now.utc) ⇒ Boolean
-
#initialize(start_date_time, duration_in_minutes, timezone, options = {}) ⇒ Core
constructor
A new instance of Core.
- #monthly_by_day_of_month? ⇒ Boolean
- #monthly_by_day_of_week? ⇒ Boolean
- #not_started_yet?(time) ⇒ Boolean
- #parsed_time_string ⇒ Object
- #repeat? ⇒ Boolean
- #start_on ⇒ Object
- #started? ⇒ Boolean
- #static_include?(time_point) ⇒ Boolean
- #time_range(current_day = Time.now.utc, options = {}) ⇒ Object
- #timezone_object ⇒ Object
- #utc_offset ⇒ Object
- #weekly? ⇒ Boolean
- #zoned_date(time = Time.now.utc) ⇒ Object
- #zoned_end_date ⇒ Object
- #zoned_time(time = nil) ⇒ Object
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, ={} @start_time_string = start_date_time @duration_in_minutes = duration_in_minutes @timezone = timezone @ending = .fetch(:ending, nil) @repeat_type = .fetch(:repeat, nil) end |
Instance Attribute Details
#duration_in_minutes ⇒ Object (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_type ⇒ Object (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_string ⇒ Object (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
25 26 27 |
# File 'lib/zone_ranger/core.rb', line 25 def daily? @repeat_type == :daily end |
#expired?(time = Time.now.utc) ⇒ 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
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
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
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
85 86 87 |
# File 'lib/zone_ranger/core.rb', line 85 def not_started_yet? time start_on > zoned_date end |
#parsed_time_string ⇒ Object
117 118 119 |
# File 'lib/zone_ranger/core.rb', line 117 def parsed_time_string timezone_object.parse(start_time_string) end |
#repeat? ⇒ Boolean
21 22 23 |
# File 'lib/zone_ranger/core.rb', line 21 def repeat? !!@repeat_type end |
#start_on ⇒ Object
89 90 91 |
# File 'lib/zone_ranger/core.rb', line 89 def start_on parsed_time_string.to_date end |
#started? ⇒ Boolean
93 94 95 |
# File 'lib/zone_ranger/core.rb', line 93 def started? !not_started_yet? end |
#static_include?(time_point) ⇒ 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, ={} offset = .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_object ⇒ Object
41 42 43 |
# File 'lib/zone_ranger/core.rb', line 41 def timezone_object ActiveSupport::TimeZone.new((@timezone || "UTC")) end |
#utc_offset ⇒ Object
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
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_date ⇒ Object
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 |