Class: Bdb::ResultSet
Defined Under Namespace
Classes: LimitReached
Instance Attribute Summary collapse
-
#count ⇒ Object
readonly
Returns the value of attribute count.
-
#group ⇒ Object
readonly
Returns the value of attribute group.
-
#limit ⇒ Object
readonly
Returns the value of attribute limit.
-
#offset ⇒ Object
readonly
Returns the value of attribute offset.
-
#results ⇒ Object
readonly
Returns the value of attribute results.
Instance Method Summary collapse
- #<<(item) ⇒ Object
- #hash_class ⇒ Object
-
#initialize(opts, &block) ⇒ ResultSet
constructor
A new instance of ResultSet.
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
#count ⇒ Object (readonly)
Returns the value of attribute count.
19 20 21 |
# File 'lib/bdb/result_set.rb', line 19 def count @count end |
#group ⇒ Object (readonly)
Returns the value of attribute group.
19 20 21 |
# File 'lib/bdb/result_set.rb', line 19 def group @group end |
#limit ⇒ Object (readonly)
Returns the value of attribute limit.
19 20 21 |
# File 'lib/bdb/result_set.rb', line 19 def limit @limit end |
#offset ⇒ Object (readonly)
Returns the value of attribute offset.
19 20 21 |
# File 'lib/bdb/result_set.rb', line 19 def offset @offset end |
#results ⇒ Object (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
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_class ⇒ Object
21 22 23 |
# File 'lib/bdb/result_set.rb', line 21 def hash_class @hash_class ||= defined?(ActiveSupport::OrderedHash) ? ActiveSupport::OrderedHash : Hash end |