Class: RiakRecord::Finder::Basic
- Inherits:
-
Object
- Object
- RiakRecord::Finder::Basic
- Includes:
- Enumerable
- Defined in:
- lib/riak_record/finder/basic.rb
Direct Known Subclasses
Instance Method Summary collapse
- #all ⇒ Object (also: #to_ary)
- #any?(&block) ⇒ Boolean
- #count ⇒ Object
- #count_by(attribute) ⇒ Object
- #each ⇒ Object
- #empty?(&block) ⇒ Boolean (also: #none?)
- #find(ifnone = nil, &block) ⇒ Object (also: #detect)
- #first(n = nil) ⇒ Object
-
#initialize(finder_class, conditions) ⇒ Basic
constructor
A new instance of Basic.
- #page(continuation = nil, page_size = 100) ⇒ Object
- #pluck_by_map_reduce(attribute, timeout = nil) ⇒ Object
- #to_a ⇒ Object
Constructor Details
#initialize(finder_class, conditions) ⇒ Basic
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/riak_record/finder/basic.rb', line 6 def initialize(finder_class, conditions) unless finder_class <= RiakRecord::Base raise ArgumentError, "RiakRecord::Finder requires a RiakRecord::Base class" end unless conditions.is_a?(Hash) && conditions.size == 1 raise ArgumentError, "RiakRecord::Finder requires exactly one condition specified as a hash" end @finder_class = finder_class @bucket = finder_class.bucket @index = finder_class.index_names[conditions.keys.first.to_sym] @value = conditions.values.first @load_complete = false @page_size = 100 @loaded_objects = [] end |
Instance Method Details
#all ⇒ Object Also known as: to_ary
22 23 24 25 26 27 |
# File 'lib/riak_record/finder/basic.rb', line 22 def all until @load_complete do load_next_page end @loaded_objects end |
#any?(&block) ⇒ Boolean
75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/riak_record/finder/basic.rb', line 75 def any?(&block) if block_given? return true if @loaded_objects.any? &block until @load_complete load_next_page.any? &block end false else !empty? end end |
#count ⇒ Object
38 39 40 |
# File 'lib/riak_record/finder/basic.rb', line 38 def count @load_complete ? @loaded_objects.count : count_map_reduce end |
#count_by(attribute) ⇒ Object
87 88 89 90 91 92 |
# File 'lib/riak_record/finder/basic.rb', line 87 def count_by(attribute) return count_by_map_reduce(attribute) unless @load_complete results = {} @loaded_objects.each{|o| k = o.send(attribute).to_s; results[k] ||= 0; results[k] += 1 } results end |
#each ⇒ Object
34 35 36 |
# File 'lib/riak_record/finder/basic.rb', line 34 def each all.each{|o| yield o} end |
#empty?(&block) ⇒ Boolean Also known as: none?
65 66 67 68 69 70 71 72 |
# File 'lib/riak_record/finder/basic.rb', line 65 def empty?(&block) if block_given? !any?(&block) else load_next_page(1) unless load_started? @loaded_objects.count.zero? end end |
#find(ifnone = nil, &block) ⇒ Object Also known as: detect
54 55 56 57 58 59 60 61 62 |
# File 'lib/riak_record/finder/basic.rb', line 54 def find(ifnone = nil, &block) found = @loaded_objects.find(&block) until found || @load_complete found = load_next_page.find(&block) end return found if found return ifnone.call if ifnone nil end |
#first(n = nil) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/riak_record/finder/basic.rb', line 42 def first(n=nil) if n unless @load_complete || @loaded_objects.count >= n load_next_page(n-@loaded_objects.count) end @loaded_objects.first(n) else load_next_page(1) unless load_started? @loaded_objects.first end end |
#page(continuation = nil, page_size = 100) ⇒ Object
94 95 96 97 98 99 100 |
# File 'lib/riak_record/finder/basic.rb', line 94 def page(continuation = nil, page_size = 100) = { :max_results => page_size } [:continuation] = continuation if continuation.present? querier = Riak::SecondaryIndex.new(@bucket, @index, @value, ) results = querier.values.compact.map{ |robject| @finder_class.new(robject) } return results, querier.keys.continuation end |
#pluck_by_map_reduce(attribute, timeout = nil) ⇒ Object
102 103 104 105 106 107 108 109 110 |
# File 'lib/riak_record/finder/basic.rb', line 102 def pluck_by_map_reduce(attribute, timeout = nil) pluck_by_index = @finder_class.index_names[attribute.to_sym].present? parsed_attribute = pluck_by_index ? "v.values[0].metadata.index.#{@finder_class.index_names[attribute.to_sym]}" : "JSON.parse(v.values[0].data).#{attribute}" mr = Riak::MapReduce.new(@finder_class.client). index(@bucket, @index, @value). map("function(v){ return [#{parsed_attribute}] }", :keep => true) mr.timeout = timeout if timeout.present? mr.run end |
#to_a ⇒ Object
30 31 32 |
# File 'lib/riak_record/finder/basic.rb', line 30 def to_a all.dup # new array end |