Class: Battman::DSL::EveryBlock

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

Instance Method Summary collapse

Constructor Details

#initialize(battman, battery, interval) ⇒ EveryBlock

Returns a new instance of EveryBlock.



5
6
7
8
9
10
# File 'lib/battman/dsl/every_block.rb', line 5

def initialize(battman, battery, interval)
  @battery = battery
  @interval = interval
  @battman = battman
  @last_values = {}
end

Instance Method Details

#check(attribute, *args, &block) ⇒ Object

Raises:

  • (ArgumentError)


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/battman/dsl/every_block.rb', line 12

def check(attribute, *args, &block)
  raise ArgumentError.new('no block given') unless block_given?

  unless @battery.respond_to?(attribute)
    raise ArgumentError.new("invalid method #{attribute}")
  end

  last_values_hash = (attribute.hash + args.hash).hash

  task = Proc.new do
    value = @battery.send(attribute, *args)
    block.call(value, @last_values[last_values_hash])
    @last_values[last_values_hash] = value
  end

  @battman.register(@interval, task)
end