Summarize

class
Superclass
Object
Extended With
Loggability

An summarization action taken by an Observer.

Attributes

block[R]

The object to call when the action is triggered.

count_threshold[R]

The number of events that cause the action to be called.

event_history[R]

The Hash of recent events, keyed by their arrival time.

schedule[R]

The schedule that applies to this action.

time_threshold[R]

The number of seconds between calls to the action

Public Class Methods

anchor
new( every: 0, count: 0, during: nil, &block )

Create a new Summary that will call the specified block during the given schedule, every specified number of seconds or count events, whichever is sooner.

# File lib/arborist/observer/summarize.rb, line 21
def initialize( every: 0, count: 0, during: nil, &block )
        raise ArgumentError, "Summarize requires a block" unless block
        raise ArgumentError, "Summarize requires a value for `every` or `count`." if
                every.zero? && count.zero?

        @time_threshold  = every
        @count_threshold = count
        @schedule        = Schedulability::Schedule.parse( during ) if during
        @block           = block

        @event_history = {}
end

Public Instance Methods

anchor
call_block()

Execute the action block.

# File lib/arborist/observer/summarize.rb, line 75
def call_block
        self.block.call( self.event_history.dup )
ensure
        self.event_history.clear
end
anchor
count_threshold_exceeded?()

Returns true if the number of events in the event history meet or exceed the count_threshold.

# File lib/arborist/observer/summarize.rb, line 98
def count_threshold_exceeded?
        return false if self.count_threshold.zero?
        self.log.debug "Event history has %d events" % [ self.event_history.size ]
        return self.event_history.size >= self.count_threshold
end
anchor
handle_event( event )

Call the action for the specified event.

# File lib/arborist/observer/summarize.rb, line 61
def handle_event( event )
        self.record_event( event )
        self.call_block if self.should_run?
end
anchor
on_timer( * )

Handle a timing event by calling the block with any events in the history.

# File lib/arborist/observer/summarize.rb, line 68
def on_timer( * )
        self.log.debug "Timer event: %d pending event/s" % [ self.event_history.size ]
        self.call_block unless self.event_history.empty?
end
anchor
record_event( event )

Record the specified event in the event history if within the scheduled period(s).

# File lib/arborist/observer/summarize.rb, line 83
def record_event( event )
        return if self.schedule && !self.schedule.now?
        self.event_history[ Time.now ] = event
end
anchor
should_run?()

Returns true if the count threshold is exceeded and the current time is within the action's schedule.

# File lib/arborist/observer/summarize.rb, line 91
def should_run?
        return self.count_threshold_exceeded?
end