Module: DateTimeStepWith::CronStepper

Defined in:
lib/date_time_step_with/cron_stepper.rb

Instance Method Summary collapse

Instance Method Details

#step_with_cron(cron_expression, limit, step = nil, &block) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/date_time_step_with/cron_stepper.rb', line 3

def step_with_cron(cron_expression, limit,step=nil, &block)

  #if no step given, then iterate on the minimum cron value
  if step.nil? && self.respond_to?(:minute)
    step = (1.to_f/24/60) #one minute
  else
    step = step || 1# or one day
  end
  
  #Caching
  if (@step_with_cron_cache == "#{cron_expression} #{limit} #{step}")
    @step_with_cron_collection ||= self.step(limit,step).select{|d| d.match_cron?(cron_expression) }
  else
    @step_with_cron_cache = "#{cron_expression} #{limit} #{step}"
    @step_with_cron_collection = self.step(limit,step).select{|d| d.match_cron?(cron_expression) }
  end
  
  if block_given?
    @step_with_cron_collection.each do |d|
      yield d
    end
  else
    @step_with_cron_collection 
  end
end