Module: Battman::DSL
- Included in:
- Battman
- Defined in:
- lib/battman/dsl.rb,
lib/battman/dsl/every_block.rb,
lib/battman/dsl/watch_block.rb
Defined Under Namespace
Classes: EveryBlock, WatchBlock
Instance Method Summary
collapse
Instance Method Details
#initialize {|_self| ... } ⇒ Object
8
9
10
11
12
13
|
# File 'lib/battman/dsl.rb', line 8
def initialize
@blocks = Hash.new {|hash, key| hash[key] = []}
@intervals_due = Hash.new {|hash, key| hash[key] = 0}
yield self if block_given?
end
|
#register(interval, block) ⇒ Object
15
16
17
18
|
# File 'lib/battman/dsl.rb', line 15
def register(interval, block)
@blocks[interval.to_i] << block
@greatest_common_interval = @blocks.keys.inject(&:gcd)
end
|
#run ⇒ Object
40
41
42
43
44
45
|
# File 'lib/battman/dsl.rb', line 40
def run
loop do
run_once
sleep @greatest_common_interval
end
end
|
#run_once ⇒ Object
29
30
31
32
33
34
35
36
37
38
|
# File 'lib/battman/dsl.rb', line 29
def run_once
@blocks.each do |interval, blocks|
if @intervals_due[interval] <= @greatest_common_interval
blocks.map(&:call)
@intervals_due[interval] = interval
else
@intervals_due[interval] -= @greatest_common_interval
end
end
end
|
#watch(type, index = 0, **opts) {|WatchBlock.new(self, battery_class.new(index, **opts))| ... } ⇒ Object
20
21
22
23
24
25
26
27
|
# File 'lib/battman/dsl.rb', line 20
def watch(type, index = 0, **opts)
raise ArgumentError.new('no block given') unless block_given?
require "battman/#{type}_battery"
battery_class = ("Battman::" + "#{type}_battery".camelize).constantize
yield WatchBlock.new(self, battery_class.new(index, **opts))
end
|