lib/active_mocker/collection/queries.rb in active_mocker-1.3 vs lib/active_mocker/collection/queries.rb in active_mocker-1.3.1
- old
+ new
@@ -1,59 +1,70 @@
module ActiveMocker
- class RecordNotFound < Exception
+class RecordNotFound < Exception; end
- end
module Collection
-
-
module Queries
- def delete_all
- all.map(&:delete)
- end
+ class Find
- def destroy_all
- delete_all
- end
+ def initialize(record)
+ @record = record
+ end
- def all(options={})
- if options.has_key?(:conditions)
- where(options[:conditions])
- else
- Relation.new( to_a || [] )
+ def is_of(options={})
+ options.all? do |col, match|
+ next match.any? { |m| @record.send(col) == m } if match.class == Array
+ @record.send(col) == match
+ end
end
+
end
class WhereNotChain
def initialize(collection)
@collection = collection
end
def not(options={})
@collection.reject do |record|
- options.all? { |col, match| record.send(col) == match }
+ Find.new(record).is_of(options)
end
end
+ end
+ def delete_all
+ all.map(&:delete)
end
+ def destroy_all
+ delete_all
+ end
+
+ def all(options={})
+ if options.has_key?(:conditions)
+ where(options[:conditions])
+ else
+ Relation.new( to_a || [] )
+ end
+ end
+
def where(options=nil)
return WhereNotChain.new(all) if options.nil?
all.select do |record|
- options.all? { |col, match| record.send(col) == match }
+ Find.new(record).is_of(options)
end
end
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
+ results.first
end
def update_all(options)
all.each{ |i| i.update(options)}
end