Class: Bdb::ResultSet

Inherits:
Object show all
Defined in:
lib/bdb/result_set.rb

Defined Under Namespace

Classes: LimitReached

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts, &block) ⇒ ResultSet

Returns a new instance of ResultSet.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/bdb/result_set.rb', line 4

def initialize(opts, &block)
  @block  = block
  @count  = 0
  @limit  = opts[:limit] || opts[:per_page]
  @limit  = @limit.to_i if @limit
  @offset = opts[:offset] || (opts[:page] ? @limit * (opts[:page] - 1) : 0)
  @offset = @offset.to_i if @offset
  
  if @group = opts[:group]
    raise 'block not supported with group' if @block     
    @results = hash_class.new
  else
    @results = []
  end
end

Instance Attribute Details

#countObject (readonly)

Returns the value of attribute count.



19
20
21
# File 'lib/bdb/result_set.rb', line 19

def count
  @count
end

#groupObject (readonly)

Returns the value of attribute group.



19
20
21
# File 'lib/bdb/result_set.rb', line 19

def group
  @group
end

#limitObject (readonly)

Returns the value of attribute limit.



19
20
21
# File 'lib/bdb/result_set.rb', line 19

def limit
  @limit
end

#offsetObject (readonly)

Returns the value of attribute offset.



19
20
21
# File 'lib/bdb/result_set.rb', line 19

def offset
  @offset
end

#resultsObject (readonly)

Returns the value of attribute results.



19
20
21
# File 'lib/bdb/result_set.rb', line 19

def results
  @results
end

Instance Method Details

#<<(item) ⇒ Object

Raises:



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/bdb/result_set.rb', line 25

def <<(item)
  @count += 1
  return if count <= offset
  
  raise LimitReached if limit and count > limit + offset
  
  if group
    key = item.bdb_locator_key
    group_key = group.is_a?(Fixnum) ? key[0,group] : key
    (results[group_key] ||= []) << item
  elsif @block
    @block.call(item)
  else
    results << item
  end
end

#hash_classObject



21
22
23
# File 'lib/bdb/result_set.rb', line 21

def hash_class
  @hash_class ||= defined?(ActiveSupport::OrderedHash) ? ActiveSupport::OrderedHash : Hash
end