Sha256: 12becb667f015945ed49583bb733ce90c0239d4ae8520f0439c2c5ed7ca26d78
Contents?: true
Size: 1.88 KB
Versions: 3
Compression:
Stored size: 1.88 KB
Contents
# Based on ActiveRecord::NamedScope module MongoDoc module Scope def scopes read_inheritable_attribute(:scopes) || write_inheritable_attribute(:scopes, {}) end def scope(name, *args, &block) options = args.extract_options! raise ArgumentError if args.size != 1 criteria = args.first name = name.to_sym scopes[name] = lambda do |parent_scope, *args| CriteriaProxy.new(parent_scope, Mongoid::Criteria === criteria ? criteria : criteria.call(*args), options, &block) end (class << self; self; end).class_eval <<-EOT def #{name}(*args) scopes[:#{name}].call(self, *args) end EOT end class CriteriaProxy undef id attr_accessor :criteria, :klass, :parent_scope delegate :scopes, :to => :parent_scope def initialize(parent_scope, criteria, options, &block) [options.delete(:extend)].flatten.each { |extension| extend extension } if options.include?(:extend) extend Module.new(&block) if block_given? if CriteriaProxy === parent_scope chained = Mongoid::Criteria.new(klass) chained.merge(parent_scope) chained.merge(criteria) self.criteria = chained self.klass = criteria.klass else self.criteria = criteria self.klass = parent_scope end self.parent_scope = parent_scope end def respond_to?(method, include_private = false) return true if scopes.include?(method) criteria.respond_to?(method, include_private) end private def method_missing(method, *args, &block) if scopes.include?(method) scopes[method].call(self, *args) else chained = Mongoid::Criteria.new(klass) chained.merge(criteria) chained.send(method, *args, &block) end end end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
mongo_doc-0.6.19 | lib/mongo_doc/scope.rb |
mongo_doc-0.6.18 | lib/mongo_doc/scope.rb |
mongo_doc-0.6.17 | lib/mongo_doc/scope.rb |