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

Instance Method Details

#all(options = {}) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/active_mocker/collection/queries.rb', line 20

def all(options={})
  if options.has_key?(:conditions)
    where(options[: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_allObject



12
13
14
# File 'lib/active_mocker/collection/queries.rb', line 12

def delete_all
  all.map(&:delete)
end

#destroy_allObject



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(options = {})
  send(:where, options).first
end

#find_by!(options = {}) ⇒ Object

Raises:



66
67
68
69
70
# File 'lib/active_mocker/collection/queries.rb', line 66

def find_by!(options={})
  result = find_by(options)
  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(options)
  all.each{ |i| i.update(options)}
end

#where(options = nil) ⇒ Object



42
43
44
45
46
47
# File 'lib/active_mocker/collection/queries.rb', line 42

def where(options=nil)
  return WhereNotChain.new(all) if options.nil?
  all.select do |record|
    options.all? { |col, match| record.send(col) == match }
  end
end