module Battman::DSL
Public Class Methods
new() { |self| ... }
click to toggle source
# 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
Public Instance Methods
register(interval, block)
click to toggle source
# 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()
click to toggle source
# File lib/battman/dsl.rb, line 40 def run loop do run_once sleep @greatest_common_interval end end
run_once()
click to toggle source
# 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) { |watch_block(self, battery_class(index, **opts))| ... }
click to toggle source
# 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