Class: Lunar::ResultSet
- Inherits:
-
Object
- Object
- Lunar::ResultSet
- Includes:
- Enumerable
- Defined in:
- lib/lunar/result_set.rb
Overview
This wraps a Lunar search result set. You can do all the expected operations with an Enumerable.
results = Lunar.search(Gadget, :q => "Apple Macbook Pro")
results.class == Lunar::ResultSet
# => true
results.kind_of?(Enumerable)
# => true
#sort and #sort_by commands are available and directly calls the Redis SORT command.
results = Lunar.search(Gadget, :q => "Apple Macbook Pro")
results.sort(:by => :name, :order => "ALPHA")
results.sort(:by => :name, :order => "ALPHA ASC")
results.sort(:by => :name, :order => "ALPHA DESC")
Instance Attribute Summary collapse
-
#distkey ⇒ Object
readonly
Returns the value of attribute distkey.
-
#nest ⇒ Object
readonly
Returns the value of attribute nest.
-
#sortables ⇒ Object
readonly
Returns the value of attribute sortables.
Instance Method Summary collapse
- #each ⇒ Object
-
#initialize(distkey, nest, finder) ⇒ ResultSet
constructor
A new instance of ResultSet.
- #size ⇒ Object
-
#sort(opts = {}) ⇒ Array
Gives the ability to sort the search results via a ‘sortable` field in your index.
-
#sort_by(att, opts = {}) ⇒ Array
Provides syntatic sugar for ‘sort`.
Constructor Details
#initialize(distkey, nest, finder) ⇒ ResultSet
Returns a new instance of ResultSet.
28 29 30 31 32 33 |
# File 'lib/lunar/result_set.rb', line 28 def initialize(distkey, nest, finder) @distkey = distkey @nest = nest @finder = finder @sortables = @nest[:Sortables]['*'] end |
Instance Attribute Details
#distkey ⇒ Object (readonly)
Returns the value of attribute distkey.
24 25 26 |
# File 'lib/lunar/result_set.rb', line 24 def distkey @distkey end |
#nest ⇒ Object (readonly)
Returns the value of attribute nest.
25 26 27 |
# File 'lib/lunar/result_set.rb', line 25 def nest @nest end |
#sortables ⇒ Object (readonly)
Returns the value of attribute sortables.
26 27 28 |
# File 'lib/lunar/result_set.rb', line 26 def sortables @sortables end |
Instance Method Details
#each ⇒ Object
35 36 37 38 39 |
# File 'lib/lunar/result_set.rb', line 35 def each return if not distkey objects(distkey.zrange(0, -1)).each { |e| yield e } end |
#size ⇒ Object
102 103 104 105 106 |
# File 'lib/lunar/result_set.rb', line 102 def size return 0 if not distkey distkey.zcard end |
#sort(opts = {}) ⇒ Array
Gives the ability to sort the search results via a ‘sortable` field in your index.
90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/lunar/result_set.rb', line 90 def sort(opts = {}) return [] if not distkey opts[:by] = sortables[opts[:by]] if opts[:by] if opts[:start] && opts[:limit] opts[:limit] = [opts[:start], opts[:limit]] end objects(distkey.sort(opts)) end |
#sort_by(att, opts = {}) ⇒ Array
Provides syntatic sugar for ‘sort`.
56 57 58 |
# File 'lib/lunar/result_set.rb', line 56 def sort_by(att, opts = {}) sort(opts.merge(:by => att)) end |