Module: ActiveMocker::Collection::Queries
- Included in:
- ActiveHash::ARApi::ClassMethods, Association, Relation
- Defined in:
- lib/active_mocker/collection/queries.rb
Defined Under Namespace
Classes: WhereNotChain
Instance Method Summary collapse
- #all(options = {}) ⇒ Object
- #average(key) ⇒ Object
- #delete_all ⇒ Object
- #destroy_all ⇒ Object
- #find(ids) ⇒ Object
- #find_by(options = {}) ⇒ Object
- #find_by!(options = {}) ⇒ Object
- #limit(num) ⇒ Object
- #maximum(key) ⇒ Object
- #minimum(key) ⇒ Object
- #sum(key) ⇒ Object
- #update_all(options) ⇒ Object
- #where(options = nil) ⇒ Object
Instance Method Details
#all(options = {}) ⇒ Object
20 21 22 23 24 25 26 |
# File 'lib/active_mocker/collection/queries.rb', line 20 def all(={}) if .has_key?(:conditions) where([:conditions]) else Relation.new( to_a || [] ) end end |
#average(key) ⇒ Object
81 82 83 84 85 |
# File 'lib/active_mocker/collection/queries.rb', line 81 def average(key) values = values_by_key(key) total = values.inject { |sum, n| sum + n } BigDecimal.new(total) / BigDecimal.new(values.count) end |
#delete_all ⇒ Object
12 13 14 |
# File 'lib/active_mocker/collection/queries.rb', line 12 def delete_all all.map(&:delete) end |
#destroy_all ⇒ Object
16 17 18 |
# File 'lib/active_mocker/collection/queries.rb', line 16 def destroy_all delete_all end |
#find(ids) ⇒ Object
49 50 51 52 53 54 55 56 |
# File 'lib/active_mocker/collection/queries.rb', line 49 def find(ids) ids_array = [*ids] results = ids_array.map do |id| where(id: id).first end return Relation.new(results) if ids.class == Array return results.first end |
#find_by(options = {}) ⇒ Object
62 63 64 |
# File 'lib/active_mocker/collection/queries.rb', line 62 def find_by( = {}) send(:where, ).first end |
#find_by!(options = {}) ⇒ Object
66 67 68 69 70 |
# File 'lib/active_mocker/collection/queries.rb', line 66 def find_by!(={}) result = find_by() raise RecordNotFound if result.blank? result end |
#limit(num) ⇒ Object
72 73 74 |
# File 'lib/active_mocker/collection/queries.rb', line 72 def limit(num) Relation.new(all.take(num)) end |
#maximum(key) ⇒ Object
91 92 93 |
# File 'lib/active_mocker/collection/queries.rb', line 91 def maximum(key) values_by_key(key).max_by { |i| i } end |
#minimum(key) ⇒ Object
87 88 89 |
# File 'lib/active_mocker/collection/queries.rb', line 87 def minimum(key) values_by_key(key).min_by{|i| i } end |
#sum(key) ⇒ Object
76 77 78 79 |
# File 'lib/active_mocker/collection/queries.rb', line 76 def sum(key) values = values_by_key(key) values.inject { |sum, n| sum + n } end |
#update_all(options) ⇒ Object
58 59 60 |
# File 'lib/active_mocker/collection/queries.rb', line 58 def update_all() all.each{ |i| i.update()} end |
#where(options = nil) ⇒ Object
42 43 44 45 46 47 |
# File 'lib/active_mocker/collection/queries.rb', line 42 def where(=nil) return WhereNotChain.new(all) if .nil? all.select do |record| .all? { |col, match| record.send(col) == match } end end |