Class: SerialScheduler::Dsl

Inherits:
Object
  • Object
show all
Defined in:
lib/serial_scheduler/dsl.rb

Defined Under Namespace

Classes: CallWrapper

Instance Attribute Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &blk) ⇒ Object



100
101
102
103
104
105
106
107
# File 'lib/serial_scheduler/dsl.rb', line 100

def method_missing(meth, *args, &blk)
  if block_given?
    time_keys = instance_eval(&blk)
    SerialScheduler::TimeTable.add(create_time_table_with_time_keys(time_keys, self.callee, meth))
  else
    super
  end
end

Instance Attribute Details

#calleeObject

Returns the value of attribute callee.



20
21
22
# File 'lib/serial_scheduler/dsl.rb', line 20

def callee
  @callee
end

Instance Method Details

#create_time_table_with_time_keys(time_keys, callee, meth) ⇒ Object



93
94
95
96
97
98
# File 'lib/serial_scheduler/dsl.rb', line 93

def create_time_table_with_time_keys(time_keys, callee, meth)
  time_keys.inject({}) do |time_table, time_key|
    time_table[time_key] = CallWrapper.new(callee, meth)
    time_table
  end
end

#every(*args) ⇒ Object



40
41
42
43
44
45
46
47
48
49
# File 'lib/serial_scheduler/dsl.rb', line 40

def every(*args)
  case args.first
  when Symbol
    time_keys_by_symbol(*args)
  when ActiveSupport::Duration
    time_keys_by_duration(*args)
  else
    raise "SerialScheduler::Dsl - every - You have to specify either a symbol or a duration (was #{args.first.inspect})"
  end
end

#extract_at_or_default(options) ⇒ Object



88
89
90
91
# File 'lib/serial_scheduler/dsl.rb', line 88

def extract_at_or_default(options)
  options ||= {}
  options[:at] ? SerialScheduler::Converter.to_hour_and_minute(options[:at]) : "00:00"
end

#extract_from_to_or_default(options) ⇒ Object



81
82
83
84
85
86
# File 'lib/serial_scheduler/dsl.rb', line 81

def extract_from_to_or_default(options)
  options ||= {}
  from = options[:from] ? SerialScheduler::Converter.to_hour_and_minute(options[:from]) : "00:00"
  to = options[:to] ? SerialScheduler::Converter.to_hour_and_minute(options[:to]) : "23:59"
  return from, to
end

#schedule(callee, &block) ⇒ Object

How to use the scheduler

With the scheduler DSL you can define jobs in a simple language

Example:

schedule AnyReceiver do
  some_method { every :hour }
  some_other_method { every 2.hours, :from => "9:00", :to => "18:00" }
  and_another_method { every :friday, :at => "3:00" }
end


35
36
37
38
# File 'lib/serial_scheduler/dsl.rb', line 35

def schedule(callee, &block)
  self.callee = callee
  self.instance_eval(&block)
end

#time_keys_by_duration(*args) ⇒ Object



70
71
72
73
74
75
76
77
78
79
# File 'lib/serial_scheduler/dsl.rb', line 70

def time_keys_by_duration(*args)
  options = args.extract_options!
  from, to = extract_from_to_or_default(options)
  step = args.first
  time_keys = []
  SerialScheduler::Converter.for_each_time_with_step(from, to, step) do |time_key|
    time_keys << time_key
  end
  time_keys
end

#time_keys_by_symbol(*args) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/serial_scheduler/dsl.rb', line 51

def time_keys_by_symbol(*args)
  time_keys = []
  options = args.extract_options!
  case args.first
  when :hour
    from, to = extract_from_to_or_default(options)
    time_keys = SerialScheduler::Converter.hours_between(from, to)
  when :day
    time_keys = extract_at_or_default(options)
  else
    at = extract_at_or_default(options)
    day = args.first
    if [:monday, :tuesday, :wednesday, :thursday, :friday, :saturday, :sunday].include?(day)
      time_keys << "#{Date.const_get(day.to_s.upcase)}:#{at}"
    end
  end
  return time_keys
end