Class: Wukong::Processor::Limit

Inherits:
Filter show all
Defined in:
lib/wukong/widget/filters.rb

Overview

A widget which only lets a certain number of records through.

Examples:

Letting the first 3 records through on the command-line


$ cat input
1
2
3
4
$ cat input | wu-local limit --max=3
1
2
3

Letting the first 3 records through in a dataflow


Wukong.dataflow(:uses_limit) do
  ... | limit(max: 3) | ...
end

See Also:

Constant Summary

Constants inherited from Wukong::Processor

SerializerError

Instance Attribute Summary collapse

Attributes included from Hanuman::StageInstanceMethods

#graph

Instance Method Summary collapse

Methods inherited from Filter

#process, #reject?

Methods inherited from Wukong::Processor

configure, description, #finalize, #perform_action, #process, #receive_action, #stop

Methods included from Logging

included

Methods inherited from Hanuman::Stage

#clone

Methods included from Hanuman::StageClassMethods

#builder, #label, #register, #set_builder

Methods included from Hanuman::StageInstanceMethods

#add_stage_link, #linkable_name, #root

Instance Attribute Details

#countObject

The current record count.



247
248
249
# File 'lib/wukong/widget/filters.rb', line 247

def count
  @count
end

Instance Method Details

#select?(record) ⇒ true, false

Select a record only if we're below the max count. Increments the count for this widget.

Parameters:

  • record (Object)

Returns:

  • (true, false)


259
260
261
262
263
# File 'lib/wukong/widget/filters.rb', line 259

def select?(record)
  keep = (max ? @count < max : true)
  @count += 1
  keep
end

#setupObject

Initializes the record count to zero.



250
251
252
# File 'lib/wukong/widget/filters.rb', line 250

def setup
  self.count = 0
end