Class: Ion::Scope

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/ion/scope.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#yieldie

Constructor Details

#initialize(search, args = {}, &blk) ⇒ Scope

Returns a new instance of Scope.

Raises:



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

#keyObject



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

#countObject

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

#optionsObject



78
79
80
# File 'lib/ion/scope.rb', line 78

def options
  @search.options
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.

Examples:

class Album
  ion { text :name }
end

Album.ion.search {
  search :text, :name, "Emotional Technology"
  text :name, "Emotional Technology"   # same
}


71
72
73
74
75
76
# File 'lib/ion/scope.rb', line 71

def search(type, field, what, args={})
  subkey = options.index(type, field).search(what, args)
  temp_keys   << subkey
  subkeys     << subkey
  search_hash << [type,field,what,args]
end

#search_hashObject

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.options.index(:sort, what)
  key.sort by: index.spec, order: "ASC ALPHA", store: key
end