Class: IceCubeEx::DayCycleRule
- Inherits:
-
IceCube::ValidatedRule
- Object
- IceCube::ValidatedRule
- IceCubeEx::DayCycleRule
- Includes:
- Validations::DayCycleInterval
- Defined in:
- lib/ice_cube_ex/rules/day_cycle_rule.rb
Instance Attribute Summary
Attributes included from Validations::DayCycleInterval
Instance Method Summary collapse
- #calculate_percentage(day_count) ⇒ Object
- #get_date(time) ⇒ Object
-
#initialize(cycle, skip) ⇒ DayCycleRule
constructor
A new instance of DayCycleRule.
-
#next_time(time, schedule, closing_time) ⇒ Object
given the following case:.
- #number_of_days_between(acceptable_time, initial_time) ⇒ Object
Methods included from Validations::DayCycleInterval
Constructor Details
#initialize(cycle, skip) ⇒ DayCycleRule
Returns a new instance of DayCycleRule.
7 8 9 10 11 12 |
# File 'lib/ice_cube_ex/rules/day_cycle_rule.rb', line 7 def initialize(cycle, skip) super cycle(cycle, skip) schedule_lock(:hour, :min, :sec) reset end |
Instance Method Details
#calculate_percentage(day_count) ⇒ Object
59 60 61 62 63 |
# File 'lib/ice_cube_ex/rules/day_cycle_rule.rb', line 59 def calculate_percentage(day_count) value = (day_count.to_f / @cycle.to_f) percentage = ((value - value.to_i) * 100).to_i percentage.zero? ? 100 : percentage end |
#get_date(time) ⇒ Object
55 56 57 |
# File 'lib/ice_cube_ex/rules/day_cycle_rule.rb', line 55 def get_date(time) Time.new(time.year, time.month, time.day, 0, 0, 0, '+00:00') end |
#next_time(time, schedule, closing_time) ⇒ Object
given the following case:
Schedule start_time 2014-2-2, DayCycleRule with cycle 4, skip 2
if we invoke schedule.next_occurrence(2014-2-1), first calculated time will be 2014-2-2, which will give a day_count of 0, thus being lower than cycle_percentage and being a valid value. this by itself is not a problem, but it will ruin our cycle logic since only for the first case it will repeat 3 times instead of 2, because the following dates will be valid:
2014-2-2, 2014-2-3, 2014-2-4
so to avoid this we start counting from start_time - 1.day (cycle_start_time)
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/ice_cube_ex/rules/day_cycle_rule.rb', line 28 def next_time(time, schedule, closing_time) @time = time @schedule = schedule cycle_start_time = schedule.start_time - 24 * 60 * 60 return nil unless find_acceptable_time_before(closing_time) number_of_days = number_of_days_between(@time, cycle_start_time) current_cycle_percentage = calculate_percentage(number_of_days) until current_cycle_percentage <= @acceptable_cycle_percentage @time += 1 return nil unless find_acceptable_time_before(closing_time) number_of_days = number_of_days_between(@time, cycle_start_time) current_cycle_percentage = calculate_percentage(number_of_days) end @uses += 1 if @time @time end |
#number_of_days_between(acceptable_time, initial_time) ⇒ Object
48 49 50 51 52 53 |
# File 'lib/ice_cube_ex/rules/day_cycle_rule.rb', line 48 def number_of_days_between(acceptable_time, initial_time) acceptable_time_date = get_date(acceptable_time) initial_time_date = get_date(initial_time) ((acceptable_time_date - initial_time_date) / (24 * 60 * 60)).to_i end |