Permalink
Browse files

#refactoring

  • Loading branch information...
alexeypetrushin committed Dec 24, 2011
1 parent 9f00aa3 commit 935d57b589df53cade97843cbc5b2b720a5a81af
Showing with 94 additions and 89 deletions.
  1. +2 −0 lib/mongo/model/conversion.rb
  2. +92 −89 lib/mongo/model/scope.rb
@@ -62,6 +62,8 @@ def to_rson options = {}
end
result[:id] = _id if _id and (options[:id] != false)
+ result[:_saved] = _saved if _saved and (options[:_saved] != false)
+ result[:param] = to_param if _saved and respond_to?(:to_param) and (options[:param] != false)
result
end
View
@@ -1,113 +1,116 @@
-module Mongo::Model::Scope
- module ClassMethods
- def current_scope
- scope, exclusive = Thread.current[scope_identifier]
- current = if exclusive
- scope
- elsif scope
- default_scope ? default_scope.merge(scope) : scope
- else
- default_scope
- end
- end
+module Mongo::Model
+ PER_PAGE, MAX_PER_PAGE = 25, 100
- def with_exclusive_scope *args, &block
- with_scope *(args << true), &block
- end
+ module Scope
+ module ClassMethods
+ def current_scope
+ scope, exclusive = Thread.current[scope_identifier]
+ current = if exclusive
+ scope
+ elsif scope
+ default_scope ? default_scope.merge(scope) : scope
+ else
+ default_scope
+ end
+ end
- def with_scope *args, &block
- if args.last.is_a?(TrueClass) or args.last.is_a?(FalseClass)
- exclusive = args.pop
- else
- exclusive = false
+ def with_exclusive_scope *args, &block
+ with_scope *(args << true), &block
end
- scope = query *args
- previous_scope, previous_exclusive = Thread.current[scope_identifier]
- raise "exclusive scope already applied!" if previous_exclusive
+ def with_scope *args, &block
+ if args.last.is_a?(TrueClass) or args.last.is_a?(FalseClass)
+ exclusive = args.pop
+ else
+ exclusive = false
+ end
- begin
- scope = previous_scope.merge scope if !exclusive and previous_scope
- Thread.current[scope_identifier] = [scope, exclusive]
- return block.call
- ensure
- Thread.current[scope_identifier] = [previous_scope, false]
+ scope = query *args
+ previous_scope, previous_exclusive = Thread.current[scope_identifier]
+ raise "exclusive scope already applied!" if previous_exclusive
+
+ begin
+ scope = previous_scope.merge scope if !exclusive and previous_scope
+ Thread.current[scope_identifier] = [scope, exclusive]
+ return block.call
+ ensure
+ Thread.current[scope_identifier] = [previous_scope, false]
+ end
end
- end
- inheritable_accessor :_default_scope, nil
- def default_scope *args, &block
- if block
- self._default_scope = -> {query block.call}
- elsif !args.empty?
- self._default_scope = -> {query *args}
- else
- _default_scope && _default_scope.call
+ inheritable_accessor :_default_scope, nil
+ def default_scope *args, &block
+ if block
+ self._default_scope = -> {query block.call}
+ elsif !args.empty?
+ self._default_scope = -> {query *args}
+ else
+ _default_scope && _default_scope.call
+ end
end
- end
- def scope name, *args, &block
- model = self
- metaclass.define_method name do
- query (block && instance_eval(&block)) || args
+ def scope name, *args, &block
+ model = self
+ metaclass.define_method name do
+ query (block && instance_eval(&block)) || args
+ end
end
- end
- # Finders.
+ # Finders.
- def count selector = {}, options = {}
- if current = current_scope
- super current.selector.merge(selector), current.options.merge(options)
- else
- super selector, options
+ def count selector = {}, options = {}
+ if current = current_scope
+ super current.selector.merge(selector), current.options.merge(options)
+ else
+ super selector, options
+ end
end
- end
- def first selector = {}, options = {}
- if current = current_scope
- super current.selector.merge(selector), current.options.merge(options)
- else
- super selector, options
+ def first selector = {}, options = {}
+ if current = current_scope
+ super current.selector.merge(selector), current.options.merge(options)
+ else
+ super selector, options
+ end
end
- end
- def each selector = {}, options = {}, &block
- if current = current_scope
- super current.selector.merge(selector), current.options.merge(options), &block
- else
- super selector, options, &block
+ def each selector = {}, options = {}, &block
+ if current = current_scope
+ super current.selector.merge(selector), current.options.merge(options), &block
+ else
+ super selector, options, &block
+ end
end
- end
-
- # Shortcuts for frequently used scopes.
- def limit n; query({}, limit: n) end
- def skip n; query({}, skip: n) end
- def sort *list
- list = list.collect{|item| item.is_a?(Array) ? item : [item, 1]}
- query({}, sort: list)
- end
- alias_method :sort_by, :sort
- def snapshot; query({}, snapshot: true) end
+ # Shortcuts for frequently used scopes.
- PER_PAGE, MAX_PER_PAGE = 25, 100
- def paginate *args
- args.size.must.be_in 1..2
- if args.size == 2
- page, per_page = *args
- else
- options = args.first
- page, per_page = options[:page], options[:per_page]
+ def limit n; query({}, limit: n) end
+ def skip n; query({}, skip: n) end
+ def sort *list
+ list = list.collect{|item| item.is_a?(Array) ? item : [item, 1]}
+ query({}, sort: list)
end
- page ||= 1
- per_page ||= PER_PAGE
- per_page = MAX_PER_PAGE if per_page > MAX_PER_PAGE
- skip((page - 1) * per_page).limit(per_page)
- end
+ alias_method :sort_by, :sort
+ def snapshot; query({}, snapshot: true) end
- protected
- def scope_identifier
- @scope_identifier ||= :"mms_#{self.name}"
+ def paginate *args
+ args.size.must.be_in 1..2
+ if args.size == 2
+ page, per_page = *args
+ else
+ options = args.first
+ page, per_page = options[:page].try(:to_i), options[:per_page].try(:to_i)
+ end
+ page ||= 1
+ per_page ||= PER_PAGE
+ per_page = MAX_PER_PAGE if per_page > MAX_PER_PAGE
+ skip((page - 1) * per_page).limit(per_page)
end
+
+ protected
+ def scope_identifier
+ @scope_identifier ||= :"mms_#{self.name}"
+ end
+ end
end
end

0 comments on commit 935d57b

Please sign in to comment.