Class: Ion::Search

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/ion/search.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options, &blk) ⇒ Search

Returns a new instance of Search.



7
8
9
10
# File 'lib/ion/search.rb', line 7

def initialize(options, &blk)
  @options = options
  @scope   = Ion::Scope.new(self, &blk)
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



4
5
6
# File 'lib/ion/search.rb', line 4

def options
  @options
end

#scopeObject (readonly)

Returns the value of attribute scope.



5
6
7
# File 'lib/ion/search.rb', line 5

def scope
  @scope
end

Instance Method Details

#each(&blk) ⇒ Object



52
53
54
# File 'lib/ion/search.rb', line 52

def each(&blk)
  to_a.each &blk
end

#idsObject



68
69
70
# File 'lib/ion/search.rb', line 68

def ids
  @scope.ids range
end

#keyObject



72
73
74
# File 'lib/ion/search.rb', line 72

def key
  @scope.key
end

#modelObject

Returns the model.

Examples:


search = Album.ion.search { ... }
assert search.model == Album


18
19
20
# File 'lib/ion/search.rb', line 18

def model
  @options.model
end

#range(args = nil) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/ion/search.rb', line 28

def range(args=nil)
  @range = if args == :all
      nil
    elsif args.is_a?(Range)
      args
    elsif !args.is_a?(Hash)
      @range
    elsif args[:from] && args[:limit]
      ((args[:from]-1)..(args[:from]-1 + args[:limit]-1))
    elsif args[:page] && args[:limit]
      (((args[:page]-1)*args[:limit])..((args[:page])*args[:limit]))
    elsif args[:from] && args[:to]
      ((args[:from]-1)..(args[:to]-1))
    elsif args[:from]
      ((args[:from]-1)..-1)
    else
      @range
    end || (0..-1)
end

#search_hashObject

Returns a unique hash for the search.



23
24
25
26
# File 'lib/ion/search.rb', line 23

def search_hash
  require 'digest'
  Digest::MD5.hexdigest @scope.search_hash.inspect
end

#sizeObject



56
57
58
# File 'lib/ion/search.rb', line 56

def size
  @scope.count
end

#sort_by(what) ⇒ Object



64
65
66
# File 'lib/ion/search.rb', line 64

def sort_by(what)
  @scope.sort_by what
end

#to_aObject



48
49
50
# File 'lib/ion/search.rb', line 48

def to_a
  ids.map &model
end

#yieldie(&blk) ⇒ Object



60
61
62
# File 'lib/ion/search.rb', line 60

def yieldie(&blk)
  @scope.yieldie &blk
end