lib/active_enumerable/where.rb in active_enumerable-1.0.0 vs lib/active_enumerable/where.rb in active_enumerable-1.1.0
- old
+ new
@@ -1,32 +1,11 @@
require "active_enumerable/finder"
+require "active_enumerable/where/where_not_chain"
+require "active_enumerable/where/where_or_chain"
module ActiveEnumerable
module Where
- class WhereNotChain
- def initialize(collection, parent_class)
- @collection = collection
- @parent_class = parent_class
- end
-
- # Returns a new relation expressing WHERE + NOT condition according to
- # the conditions in the arguments.
- #
- # #not accepts conditions as a string, array, or hash. See Where#where for
- # more details on each format.
- #
- # <#ActiveEnumerable>.where.not(name: "Jon")
- # <#ActiveEnumerable>.where.not(name: nil)
- # <#ActiveEnumerable>.where.not(name: %w(Ko1 Nobu))
- # <#ActiveEnumerable>.where.not(name: "Jon", role: "admin")
- def not(conditions={})
- @parent_class.call(@collection.reject do |record|
- Finder.new(record).is_of(conditions)
- end)
- end
- end
-
# Returns a new relation, which is the result of filtering the current relation
# according to the conditions in the arguments.
#
# === hash
#
@@ -56,37 +35,19 @@
# Additional conditions can be passed to where in hash form.
#
# <#ActiveEnumerable>.where(id: 1).or(author_id: 3)
#
# @see ActiveEnumerable::Finder#is_of for all usages of conditions.
- def where(conditions=nil)
- return WhereNotChain.new(all, method(:__new_relation__)) if conditions.nil?
- enable_or create_where_relation(conditions, to_a.select do |record|
- Finder.new(record).is_of(conditions)
- end)
- end
-
- def enable_or(relation)
- pre_where_to_a = to_a
- relation.define_singleton_method(:or) do |conditions_or_relation|
- conditions = get_conditions(conditions_or_relation)
- or_result = create_where_relation(where_conditions, pre_where_to_a).where(conditions)
- create_where_relation(or_result.where_conditions, relation.to_a.concat(or_result.to_a).uniq)
+ def where(conditions = nil, &block)
+ return WhereNotChain.new(all, method(:__new_relation__)) unless conditions || block
+ conditions = conditions || { nil => block }
+ create_where_relation(conditions, to_a.select do |record|
+ Finder.new(record).is_of(conditions || { nil => block })
+ end).tap do |where|
+ where.extend(WhereOrChain)
+ where.original_collection = to_a
end
- relation
end
-
- private :enable_or
-
- def get_conditions(conditions_or_relation)
- if conditions_or_relation.respond_to?(:where_conditions)
- conditions_or_relation.where_conditions
- else
- conditions_or_relation
- end
- end
-
- private :get_conditions
def where_conditions
@where_conditions ||= {}
end