Class: Lupa::Search
- Inherits:
-
Object
- Object
- Lupa::Search
- Defined in:
- lib/lupa/search.rb
Defined Under Namespace
Classes: Scope
Instance Attribute Summary collapse
-
#scope ⇒ Object
readonly
Public: Return class scope.
-
#search_attributes ⇒ Object
readonly
Public: Return class search attributes including default search attributes.
Class Method Summary collapse
-
.search(attributes) ⇒ Object
Public: Creates a new instance of the search class an applies search method with attributes to it.
Instance Method Summary collapse
-
#default_search_attributes ⇒ Object
Public: Return default a hash containing default search attributes of the class.
-
#initialize(scope) ⇒ Search
constructor
Public: Create a new instance of the class.
-
#method_missing(method_sym, *arguments, &block) ⇒ Object
Public: Apply the missing method to the search result.
-
#results ⇒ Object
Public: Return the search result.
-
#search(attributes) ⇒ Object
Public: Set and checks search attributes, and instantiates the Scope class.
Constructor Details
#initialize(scope) ⇒ Search
Public: Create a new instance of the class.
Options
scope
- An object which will be use to perform all the search operations.
Examples
class ProductSearch < Lupa::Search
class Scope
def category
scope.where(category: search_attributes[:category])
end
def in_stock
scope.where(in_stock: search_attributes[:in_stock])
end
end
def default_search_attributes
{ in_stock: true }
end
end
scope = Product.where(price: 20..30)
search = ProductSearch.new(scope)
Returns a new instance of the class.
99 100 101 |
# File 'lib/lupa/search.rb', line 99 def initialize(scope) @scope = scope end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_sym, *arguments, &block) ⇒ Object
Public: Apply the missing method to the search result.
Examples
class ProductSearch < Lupa::Search
class Scope
def category
scope.where(category: search_attributes[:category])
end
end
def initialize(scope = Product.in_stock)
@scope = scope
end
end
search = ProductSearch.search({ category: 'furniture' }).first
# => #<Product:0x007f9c0ce1b1a8>
Returns the search result.
269 270 271 272 273 274 275 |
# File 'lib/lupa/search.rb', line 269 def method_missing(method_sym, *arguments, &block) if results.respond_to?(method_sym) results.send(method_sym, *arguments, &block) else raise Lupa::ResultMethodNotImplementedError, "The resulting scope does not respond to #{method_sym} method." end end |
Instance Attribute Details
#scope ⇒ Object (readonly)
Public: Return class scope.
Examples
class ProductSearch < Lupa::Search
class Scope
def category
scope.where(category: search_attributes[:category])
end
def in_stock
scope.where(in_stock: search_attributes[:in_stock])
end
end
def default_search_attributes
{ in_stock: true }
end
end
search = ProductSearch.new(@products).search({ category: 'furniture' })
search.scope
# => @products
Returns your class scope.
34 35 36 |
# File 'lib/lupa/search.rb', line 34 def scope @scope end |
#search_attributes ⇒ Object (readonly)
Public: Return class search attributes including default search attributes.
Examples
class ProductSearch < Lupa::Search
class Scope
def category
scope.where(category: search_attributes[:category])
end
def in_stock
scope.where(in_stock: search_attributes[:in_stock])
end
end
def default_search_attributes
{ in_stock: true }
end
end
search = ProductSearch.new(@products).search({ category: 'furniture' })
search.search_attributes
# => { category: furniture, in_stock: true }
Returns your class search attributes including default search attributes.
65 66 67 |
# File 'lib/lupa/search.rb', line 65 def search_attributes @search_attributes end |
Class Method Details
.search(attributes) ⇒ Object
Public: Creates a new instance of the search class an applies search method with attributes to it.
Options
attributes
- The hash containing the search attributes.
-
If search class doesn’t have a default scope specified, it will raise a Lupa::DefaultScopeError exception.
-
If attributes is not a Hash kind of class, it will raise a Lupa::SearchAttributesError exception.
-
If attributes keys don’t match methods defined on your class, it will raise a Lupa::NotImplementedError.
Examples
class ProductSearch < Lupa::Search
class Scope
def category
scope.where(category: search_attributes[:category])
end
end
def initialize(scope = Product.in_stock)
@scope = scope
end
end
search = ProductSearch.search({ category: 'furniture' })
# => #<ProductSearch:0x007f7f74070850 @scope=scope, @search_attributes={:category=>'furniture'}, @scope_class=#<ProductSearch::Scope:0x007fd2811001e8 @scope=[1, 2, 3, 4, 5, 6, 7, 8], @search_attributes={:even_numbers=>true}>>
Returns the class instance itself.
211 212 213 214 215 |
# File 'lib/lupa/search.rb', line 211 def self.search(attributes) new.search(attributes) rescue ArgumentError raise Lupa::DefaultScopeError, "You need to define a default scope in order to user search class method." end |
Instance Method Details
#default_search_attributes ⇒ Object
Public: Return default a hash containing default search attributes of the class.
Examples
class ProductSearch < Lupa::Search
class Scope
def category
scope.where(category: search_attributes[:category])
end
def in_stock
scope.where(in_stock: search_attributes[:in_stock])
end
end
def default_search_attributes
{ in_stock: true }
end
end
scope = Product.where(price: 20..30)
search = ProductSearch.new(scope).search({ category: 'furniture' })
search.default_search_attributes
# => { in_stock: true }
Returns default a hash containing default search attributes of the class.
133 134 135 |
# File 'lib/lupa/search.rb', line 133 def default_search_attributes {} end |
#results ⇒ Object
Public: Return the search result.
Examples
class ProductSearch < Lupa::Search
class Scope
def category
scope.where(category: search_attributes[:category])
end
end
def initialize(scope = Product.in_stock)
@scope = scope
end
end
search = ProductSearch.search({ category: 'furniture' }).results
# => #<Product::ActiveRecord_Relation:0x007ffda11b7d48>
Returns the search result.
241 242 243 |
# File 'lib/lupa/search.rb', line 241 def results @results ||= run end |
#search(attributes) ⇒ Object
Public: Set and checks search attributes, and instantiates the Scope class.
Options
attributes
- The hash containing the search attributes.
-
If attributes is not a Hash kind of class, it will raise a Lupa::SearchAttributesError.
-
If attributes keys don’t match methods defined on your class, it will raise a Lupa::NotImplementedError.
Examples
class ProductSearch < Lupa::Search
class Scope
def category
scope.where(category: search_attributes[:category])
end
end
end
scope = Product.where(price: 20..30)
search = ProductSearch.new(scope).search({ category: 'furniture' })
# => #<ProductSearch:0x007f7f74070850 @scope=scope, @search_attributes={:category=>'furniture'}, @scope_class=#<ProductSearch::Scope:0x007fd2811001e8 @scope=[1, 2, 3, 4, 5, 6, 7, 8], @search_attributes={:even_numbers=>true}>>
Returns the class instance itself.
167 168 169 170 171 172 173 174 |
# File 'lib/lupa/search.rb', line 167 def search(attributes) raise Lupa::SearchAttributesError, "Your search params needs to be a hash." unless attributes.respond_to?(:keys) set_search_attributes(attributes) set_scope_class check_method_definitions self end |