Module: Believer::ActAsEnumerable
- Included in:
- Query
- Defined in:
- lib/believer/act_as_enumerable.rb
Class Method Summary collapse
Instance Method Summary collapse
- #any? ⇒ Boolean
- #count ⇒ Object
-
#exists?(*args) ⇒ Boolean
Tests if there are any records present which conform to the argument filter(s).
- #first ⇒ Object
- #last ⇒ Object
Class Method Details
.included(base) ⇒ Object
4 5 6 7 |
# File 'lib/believer/act_as_enumerable.rb', line 4 def self.included(base) base.delegate *(Enumerable.instance_methods(false).map {|enum_method| enum_method.to_sym}), :to => :to_a base.delegate :each, :size, :[], :to => :to_a end |
Instance Method Details
#any? ⇒ Boolean
43 44 45 46 47 |
# File 'lib/believer/act_as_enumerable.rb', line 43 def any? return loaded_objects.any? unless loaded_objects.nil? q = clone q.limit(1).count > 0 end |
#count ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/believer/act_as_enumerable.rb', line 9 def count return loaded_objects.size unless loaded_objects.nil? count_q = clone count_q.selects = ['COUNT(*)'] result = count_q.execute cnt = -1 result.each do |row| cnt = row['count'] end cnt end |
#exists?(*args) ⇒ Boolean
Tests if there are any records present which conform to the argument filter(s)
25 26 27 28 |
# File 'lib/believer/act_as_enumerable.rb', line 25 def exists?(*args) return count > 0 if args.nil? || args.size == 0 where(*args).exists? end |
#first ⇒ Object
30 31 32 33 |
# File 'lib/believer/act_as_enumerable.rb', line 30 def first return loaded_objects.first unless loaded_objects.nil? clone.limit(1)[0] end |
#last ⇒ Object
35 36 37 38 39 40 41 |
# File 'lib/believer/act_as_enumerable.rb', line 35 def last return loaded_objects.last unless loaded_objects.nil? raise "Cannot retrieve last if no order column is set" if @order_by.nil? lq = clone.limit(1) lq.order_by = @order_by.inverse lq[0] end |