Class: Ion::Scope
Instance Attribute Summary collapse
Instance Method Summary collapse
- #all_of(&blk) ⇒ Object
- #any_of(&blk) ⇒ Object
- #boost(amount = 1.0, &blk) ⇒ Object
-
#count ⇒ Object
Only when done.
- #ids(range) ⇒ Object
-
#initialize(search, args = {}, &blk) ⇒ Scope
constructor
A new instance of Scope.
- #options ⇒ Object
- #score(score, &blk) ⇒ Object
-
#search(type, field, what, args = {}) ⇒ Object
Searches a given field.
-
#search_hash ⇒ Object
Returns a unique hash of what the scope contains.
- #sort_by(what) ⇒ Object
Methods included from Helpers
Constructor Details
#initialize(search, args = {}, &blk) ⇒ Scope
Returns a new instance of Scope.
6 7 8 9 10 11 12 13 14 15 |
# File 'lib/ion/scope.rb', line 6 def initialize(search, args={}, &blk) @search = search @gate = args[:gate] || :all @score = args[:score] || 1.0 @type = :z # or :l yieldie(&blk) and done if block_given? raise Ion::Error unless [:all, :any].include?(@gate) end |
Instance Attribute Details
#key ⇒ Object
38 39 40 |
# File 'lib/ion/scope.rb', line 38 def key @key ||= Ion.volatile_key end |
Instance Method Details
#all_of(&blk) ⇒ Object
26 27 28 |
# File 'lib/ion/scope.rb', line 26 def all_of(&blk) scopes << subscope(:gate => :all, &blk) end |
#any_of(&blk) ⇒ Object
22 23 24 |
# File 'lib/ion/scope.rb', line 22 def any_of(&blk) scopes << subscope(:gate => :any, &blk) end |
#boost(amount = 1.0, &blk) ⇒ Object
30 31 32 |
# File 'lib/ion/scope.rb', line 30 def boost(amount=1.0, &blk) boosts << [Ion::Scope.new(@search, :gate => :all, &blk), amount] end |
#count ⇒ Object
Only when done
48 49 50 51 52 |
# File 'lib/ion/scope.rb', line 48 def count return key.zcard if key.type == "zset" return key.llen if key.type == "list" 0 end |
#ids(range) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/ion/scope.rb', line 82 def ids(range) from, to = range.first, range.last to -= 1 if range.exclude_end? type = key.type results = if type == "zset" key.zrevrange(from, to) elsif type == "list" key.lrange(from, to) else Array.new end expire and results end |
#options ⇒ Object
78 79 80 |
# File 'lib/ion/scope.rb', line 78 def @search. end |
#score(score, &blk) ⇒ Object
34 35 36 |
# File 'lib/ion/scope.rb', line 34 def score(score, &blk) scopes << subscope(:score => score, &blk) end |
#search(type, field, what, args = {}) ⇒ Object
Searches a given field.
71 72 73 74 75 76 |
# File 'lib/ion/scope.rb', line 71 def search(type, field, what, args={}) subkey = .index(type, field).search(what, args) temp_keys << subkey subkeys << subkey search_hash << [type,field,what,args] end |
#search_hash ⇒ Object
Returns a unique hash of what the scope contains.
18 19 20 |
# File 'lib/ion/scope.rb', line 18 def search_hash @search_hash ||= [[@gate, @score]] end |
#sort_by(what) ⇒ Object
42 43 44 45 |
# File 'lib/ion/scope.rb', line 42 def sort_by(what) index = @search..index(:sort, what) key.sort by: index.spec, order: "ASC ALPHA", store: key end |