Module: Ricordami::CanBeQueried::ClassMethods
- Defined in:
- lib/ricordami/can_be_queried.rb
Instance Method Summary collapse
- #all(opts = {}) ⇒ Object
- #first(opts = {}) ⇒ Object
- #last(opts = {}) ⇒ Object
- #paginate(opts = {}) ⇒ Object
- #rand(opts = {}) ⇒ Object
- #sort(*args) ⇒ Object
Instance Method Details
#all(opts = {}) ⇒ Object
20 21 22 23 24 25 |
# File 'lib/ricordami/can_be_queried.rb', line 20 def all(opts = {}) result_key = run_expressions(opts.delete(:expressions) || []) get_result_ids(result_key, opts).map do |id| self[id] end end |
#first(opts = {}) ⇒ Object
38 39 40 41 42 43 |
# File 'lib/ricordami/can_be_queried.rb', line 38 def first(opts = {}) result_key = run_expressions(opts.delete(:expressions) || []) opts[:limit] = [0, 1] ids = get_result_ids(result_key, opts) self[ids.first] end |
#last(opts = {}) ⇒ Object
45 46 47 48 49 50 51 |
# File 'lib/ricordami/can_be_queried.rb', line 45 def last(opts = {}) result_key = run_expressions(opts.delete(:expressions) || []) size = redis.scard(result_key) opts[:limit] = [size - 1, 1] ids = get_result_ids(result_key, opts) self[ids.first] end |
#paginate(opts = {}) ⇒ Object
27 28 29 30 31 32 33 34 35 36 |
# File 'lib/ricordami/can_be_queried.rb', line 27 def paginate(opts = {}) result_key = run_expressions(opts.delete(:expressions) || []) page = opts[:page] || 1 per_page = opts[:per_page] || 20 start = (page - 1) * per_page opts[:limit] = [start, per_page] get_result_ids(result_key, opts).map do |id| self[id] end end |
#rand(opts = {}) ⇒ Object
53 54 55 56 57 58 59 |
# File 'lib/ricordami/can_be_queried.rb', line 53 def rand(opts = {}) result_key = run_expressions(opts.delete(:expressions) || []) size = redis.scard(result_key) opts[:limit] = [Kernel.rand(size), 1] ids = get_result_ids(result_key, opts) self[ids.first] end |