Class: Bulker::Criteria

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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

#valuesObject

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

#countObject

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