Class: Friendly::Scope
- Inherits:
-
Object
- Object
- Friendly::Scope
- Defined in:
- lib/friendly/scope.rb
Instance Attribute Summary collapse
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
-
#parameters ⇒ Object
readonly
Returns the value of attribute parameters.
Instance Method Summary collapse
-
#+(other_scope) ⇒ Object
Create a new Scope that is the combination of self and other, where other takes priority.
-
#all(extra_parameters = {}) ⇒ Object
Fetch all documents at this scope.
-
#build(extra_parameters = {}) ⇒ Object
Build an object at this scope.
-
#chain_with(scope_name) ⇒ Object
Chain with another one of klass’s named_scopes.
-
#create(extra_parameters = {}) ⇒ Object
Create an object at this scope.
-
#first(extra_parameters = {}) ⇒ Object
Fetch the first document at this scope.
-
#initialize(klass, parameters) ⇒ Scope
constructor
A new instance of Scope.
-
#method_missing(method_name, *args, &block) ⇒ Object
Use method_missing to respond to other named scopes on klass.
-
#paginate(extra_parameters = {}) ⇒ Object
Paginate the documents at this scope.
-
#respond_to?(method_name, include_private = false) ⇒ Boolean
Override #respond_to? so that we can return true when it’s another named_scope.
Constructor Details
#initialize(klass, parameters) ⇒ Scope
Returns a new instance of Scope.
5 6 7 8 |
# File 'lib/friendly/scope.rb', line 5 def initialize(klass, parameters) @klass = klass @parameters = parameters end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object
Use method_missing to respond to other named scopes on klass.
71 72 73 |
# File 'lib/friendly/scope.rb', line 71 def method_missing(method_name, *args, &block) respond_to?(method_name) ? chain_with(method_name) : super end |
Instance Attribute Details
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
3 4 5 |
# File 'lib/friendly/scope.rb', line 3 def klass @klass end |
#parameters ⇒ Object (readonly)
Returns the value of attribute parameters.
3 4 5 |
# File 'lib/friendly/scope.rb', line 3 def parameters @parameters end |
Instance Method Details
#+(other_scope) ⇒ Object
Create a new Scope that is the combination of self and other, where other takes priority
87 88 89 |
# File 'lib/friendly/scope.rb', line 87 def +(other_scope) self.class.new(klass, parameters.merge(other_scope.parameters)) end |
#all(extra_parameters = {}) ⇒ Object
Fetch all documents at this scope.
14 15 16 |
# File 'lib/friendly/scope.rb', line 14 def all(extra_parameters = {}) klass.all(params(extra_parameters)) end |
#build(extra_parameters = {}) ⇒ Object
Build an object at this scope.
e.g.
Post.scope(:name => "James").build.name # => "James"
42 43 44 |
# File 'lib/friendly/scope.rb', line 42 def build(extra_parameters = {}) klass.new(params_without_modifiers(extra_parameters)) end |
#chain_with(scope_name) ⇒ Object
Chain with another one of klass’s named_scopes.
79 80 81 |
# File 'lib/friendly/scope.rb', line 79 def chain_with(scope_name) self + klass.send(scope_name) end |
#create(extra_parameters = {}) ⇒ Object
Create an object at this scope.
e.g.
@post = Post.scope(:name => "James").create
@post.new_record? # => false
@post.name # => "James"
55 56 57 |
# File 'lib/friendly/scope.rb', line 55 def create(extra_parameters = {}) klass.create(params_without_modifiers(extra_parameters)) end |
#first(extra_parameters = {}) ⇒ Object
Fetch the first document at this scope.
22 23 24 |
# File 'lib/friendly/scope.rb', line 22 def first(extra_parameters = {}) klass.first(params(extra_parameters)) end |
#paginate(extra_parameters = {}) ⇒ Object
Paginate the documents at this scope.
31 32 33 |
# File 'lib/friendly/scope.rb', line 31 def paginate(extra_parameters = {}) klass.paginate(params(extra_parameters)) end |
#respond_to?(method_name, include_private = false) ⇒ Boolean
Override #respond_to? so that we can return true when it’s another named_scope.
63 64 65 |
# File 'lib/friendly/scope.rb', line 63 def respond_to?(method_name, include_private = false) klass.has_named_scope?(method_name) || super end |