Class: RedisScope
- Inherits:
-
Object
- Object
- RedisScope
- Defined in:
- lib/redis_record/redis_scope.rb
Constant Summary collapse
- DEFAULT_OPTIONS =
{ offset: 0, filters: [], sort: :id, min: '-inf', max: '+inf' }
Instance Method Summary collapse
- #all ⇒ Object
-
#count ⇒ Object
Executors.
-
#filter(name, value = true) ⇒ Object
Modifiers always returns self.
- #first ⇒ Object
-
#initialize(model, opts = {}) ⇒ RedisScope
constructor
A new instance of RedisScope.
- #last ⇒ Object
- #limit(n) ⇒ Object
- #max(value) ⇒ Object
- #min(value) ⇒ Object
- #offset(n) ⇒ Object
- #sort(attr) ⇒ Object
Constructor Details
#initialize(model, opts = {}) ⇒ RedisScope
Returns a new instance of RedisScope.
10 11 12 13 |
# File 'lib/redis_record/redis_scope.rb', line 10 def initialize(model, opts = {}) @model = model @options = DEFAULT_OPTIONS.merge opts end |
Instance Method Details
#all ⇒ Object
59 60 61 |
# File 'lib/redis_record/redis_scope.rb', line 59 def all ids.map {|id| @model.find id} end |
#count ⇒ Object
Executors
46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/redis_record/redis_scope.rb', line 46 def count apply_filters total_records = RedisRecord.REDIS.zcard temp_key remaining_records = total_records - @options[:offset] if @options[:limit] and @options[:limit] < remaining_records @options[:limit] else remaining_records end end |
#filter(name, value = true) ⇒ Object
Modifiers always returns self
18 19 20 21 22 23 |
# File 'lib/redis_record/redis_scope.rb', line 18 def filter(name, value=true) raise NameError, ":#{name} isn't in the defined filters" unless @model.defined_filters.include? name new_filters = @options[:filters] + [[name, value]] chain_scope filters: new_filters end |
#first ⇒ Object
65 66 67 |
# File 'lib/redis_record/redis_scope.rb', line 65 def first limit(1).all[0] end |
#last ⇒ Object
69 70 71 |
# File 'lib/redis_record/redis_scope.rb', line 69 def last offset(@options[:offset] + count - 1).limit(1).all[0] end |
#limit(n) ⇒ Object
37 38 39 |
# File 'lib/redis_record/redis_scope.rb', line 37 def limit(n) chain_scope limit: n end |
#max(value) ⇒ Object
33 34 35 |
# File 'lib/redis_record/redis_scope.rb', line 33 def max(value) chain_scope max: value end |
#min(value) ⇒ Object
29 30 31 |
# File 'lib/redis_record/redis_scope.rb', line 29 def min(value) chain_scope min: value end |
#offset(n) ⇒ Object
41 42 43 |
# File 'lib/redis_record/redis_scope.rb', line 41 def offset(n) chain_scope offset: n end |
#sort(attr) ⇒ Object
25 26 27 |
# File 'lib/redis_record/redis_scope.rb', line 25 def sort(attr) chain_scope sort: attr end |