Class: Bulker::Criteria
- Inherits:
-
Object
- Object
- Bulker::Criteria
- Defined in:
- lib/bulker/criteria.rb
Overview
Bulker::Criteria provides interface for Bulker::Pager.
data = (1..20).to_a
criteria = Bulker::Criteria.new data
criteria.define_each lambda { |values, offset, limit, block|
values[offset..(offset + limit - 1)].each do |v|
block.call(v)
end
}
Bulker::Pager.each(criteria, 10) do |v|
# do some process
end
Instance Attribute Summary collapse
-
#values ⇒ Object
Returns the value of attribute values.
Class Method Summary collapse
-
.from_array(ary) ⇒ Object
create criteria from
ary.
Instance Method Summary collapse
-
#count ⇒ Object
get size of
values. -
#define_each(each_proc) ⇒ Object
set implementation of
eachby lambda. -
#each(&block) ⇒ Object
do
blockfor each data. -
#initialize(values) ⇒ Criteria
constructor
create criteria with
values. -
#limit(limit) ⇒ Object
set
limitto specify numbers to fetch data. -
#offset(offset) ⇒ Object
set
offsetto specify offset from head of data.
Constructor Details
#initialize(values) ⇒ Criteria
create criteria with values.
defaults:
-
offset: 0 -
limit: 10
34 35 36 37 38 |
# File 'lib/bulker/criteria.rb', line 34 def initialize(values) @values = values @offset = 0 @limit = 10 end |
Instance Attribute Details
#values ⇒ Object
Returns the value of attribute values.
28 29 30 |
# File 'lib/bulker/criteria.rb', line 28 def values @values end |
Class Method Details
.from_array(ary) ⇒ Object
create criteria from ary
18 19 20 21 22 23 24 25 |
# File 'lib/bulker/criteria.rb', line 18 def from_array(ary) criteria = Criteria.new ary criteria.define_each lambda { |values, offset, limit, block| values[offset..(offset + limit - 1)].each do |v| block.call(v) end } end |
Instance Method Details
#count ⇒ Object
get size of values.
53 54 55 |
# File 'lib/bulker/criteria.rb', line 53 def count @values.size end |
#define_each(each_proc) ⇒ Object
set implementation of each by lambda.
58 59 60 61 |
# File 'lib/bulker/criteria.rb', line 58 def define_each(each_proc) @each_proc = each_proc self end |
#each(&block) ⇒ Object
do block for each data
64 65 66 |
# File 'lib/bulker/criteria.rb', line 64 def each(&block) @each_proc.call(values, @offset, @limit, block) end |
#limit(limit) ⇒ Object
set limit to specify numbers to fetch data.
47 48 49 50 |
# File 'lib/bulker/criteria.rb', line 47 def limit(limit) @limit = limit self end |
#offset(offset) ⇒ Object
set offset to specify offset from head of data.
41 42 43 44 |
# File 'lib/bulker/criteria.rb', line 41 def offset(offset) @offset = offset self end |