Class: Mongoid::Spacial::GeoNearResults

Inherits:
Array
  • Object
show all
Defined in:
lib/mongoid_spacial/spacial/geo_near_results.rb

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Methods inherited from Array

#to_lng_lat

Constructor Details

- (GeoNearResults) initialize(document, results, opts = {})

A new instance of GeoNearResults



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/mongoid_spacial/spacial/geo_near_results.rb', line 7

def initialize(document,results,opts = {})
  raise "class must include Mongoid::Spacial::Document" unless document.respond_to?(:spacial_fields_indexed)
  @document = document
  @opts = opts
  @_original_opts = opts.clone
  @stats = results['stats'] || {}
  @opts[:skip] ||= 0
  @opts[:total_entries] = opts[:query]["num"] || @stats['nscanned']

  @_original_array = results['results'].collect do |result|
    res = Mongoid::Factory.from_db(@document, result.delete('obj'))
    res.geo = {}
    # camel case is awkward in ruby when using variables...
    if result['dis']
      res.geo[:distance] = result.delete('dis').to_f
    end
    result.each do |key,value|
      res.geo[key.snakecase.to_sym] = value
    end
    # dist_options[:formula] = opts[:formula] if opts[:formula]
    @opts[:calculate] = @document.spacial_fields_indexed if @document.spacial_fields_indexed.kind_of?(Array) && @opts[:calculate] == true
    if @opts[:calculate]
      @opts[:calculate] = [@opts[:calculate]] unless @opts[:calculate].kind_of? Array
      @opts[:calculate] = @opts[:calculate].map(&:to_sym) & geo_fields
      if @document.spacial_fields_indexed.kind_of?(Array) && @document.spacial_fields_indexed.size == 1
        primary = @document.spacial_fields_indexed.first
      end
      @opts[:calculate].each do |key|
        res.geo[(key.to_s+'_distance').to_sym] = res.distance_from(key,center,{:unit =>@opts[:unit] || @opts[:distance_multiplier], :spherical => @opts[:spherical]} )
        res.geo[:distance] = res.geo[key] if primary && key == primary
      end
    end
    res
  end
  if @opts[:page]
    start = (@opts[:page]-1)*@opts[:per_page] # assuming current_page is 1 based.
    @_paginated_array = @_original_array.clone
    super(@_paginated_array[@opts[:skip]+start, @opts[:per_page]] || [])
  else
    super(@_original_array[@opts[:skip]..-1] || [])
  end
end

Instance Attribute Details

- (Object) _original_array (readonly)

Returns the value of attribute _original_array



4
5
6
# File 'lib/mongoid_spacial/spacial/geo_near_results.rb', line 4

def _original_array
  @_original_array
end

- (Object) _original_opts (readonly)

Returns the value of attribute _original_opts



4
5
6
# File 'lib/mongoid_spacial/spacial/geo_near_results.rb', line 4

def _original_opts
  @_original_opts
end

- (Object) document (readonly)

Returns the value of attribute document



4
5
6
# File 'lib/mongoid_spacial/spacial/geo_near_results.rb', line 4

def document
  @document
end

- (Object) opts

Returns the value of attribute opts



5
6
7
# File 'lib/mongoid_spacial/spacial/geo_near_results.rb', line 5

def opts
  @opts
end

- (Object) stats (readonly)

Returns the value of attribute stats



4
5
6
# File 'lib/mongoid_spacial/spacial/geo_near_results.rb', line 4

def stats
  @stats
end

Instance Method Details

- (Object) current_page



95
96
97
98
# File 'lib/mongoid_spacial/spacial/geo_near_results.rb', line 95

def current_page
  page = (@opts[:page]) ? @opts[:page].to_i.abs : 1
  (page < 1) ? 1 : page
end

- (Object) limit_value Also known as: per_page



100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/mongoid_spacial/spacial/geo_near_results.rb', line 100

def limit_value
  if @opts[:per_page]
    @opts[:per_page] = @opts[:per_page].to_i.abs
  else
    @opts[:per_page] = case self.opts[:paginator]
                       when :will_paginate
                         @document.per_page
                       when :kaminari
                         Kaminari.config.default_per_page
                       else
                         Mongoid::Spacial.default_per_page
                       end
  end
end

- (Object) next_page

current_page + 1 or nil if there is no next page



135
136
137
# File 'lib/mongoid_spacial/spacial/geo_near_results.rb', line 135

def next_page
  self.current_page < self.total_pages ? (self.current_page + 1) : nil
end

- (Object) num_pages Also known as: total_pages



116
117
118
# File 'lib/mongoid_spacial/spacial/geo_near_results.rb', line 116

def num_pages
  (self.total_entries && @opts[:per_page]) ? self.total_entries/@opts[:per_page] : nil
end

- (Object) offset



125
126
127
# File 'lib/mongoid_spacial/spacial/geo_near_results.rb', line 125

def offset
  (self.current_page - 1) * self.per_page
end

- (Boolean) out_of_bounds?

Returns:

  • (Boolean)


121
122
123
# File 'lib/mongoid_spacial/spacial/geo_near_results.rb', line 121

def out_of_bounds?
  self.current_page > self.total_pages
end

- (Object) page(*args)



50
51
52
53
54
# File 'lib/mongoid_spacial/spacial/geo_near_results.rb', line 50

def page(*args)
  new_collection = self.clone
  new_collection.page!(*args)
  new_collection
end

- (Object) page!(page, options = {})



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/mongoid_spacial/spacial/geo_near_results.rb', line 56

def page!(page, options = {})
  original = options.delete(:original)
  self.opts.merge!(options)
  self.opts[:paginator] ||= Mongoid::Spacial.paginator
  self.opts[:page] = page
  start = (self.current_page-1)*self.limit_value # assuming current_page is 1 based.

  if original
    @_paginated_array = @_original_array.clone
    self.replace(@_paginated_array[self.opts[:skip]+start, self.limit_value] || [])
  else
    @_paginated_array ||= self.to_a
    self.replace(@_paginated_array[self.opts[:skip]+start, self.limit_value])
  end
  true
end

- (Object) per(num)



73
74
75
# File 'lib/mongoid_spacial/spacial/geo_near_results.rb', line 73

def per(num)
  self.page(current_page, :per_page => num)
end

- (Object) previous_page

current_page - 1 or nil if there is no previous page



130
131
132
# File 'lib/mongoid_spacial/spacial/geo_near_results.rb', line 130

def previous_page
  self.current_page > 1 ? (self.current_page - 1) : nil
end

- (Object) reset



84
85
86
87
88
# File 'lib/mongoid_spacial/spacial/geo_near_results.rb', line 84

def reset
  clone = self.clone
  clone.reset!
  clone
end

- (Object) reset!



77
78
79
80
81
82
# File 'lib/mongoid_spacial/spacial/geo_near_results.rb', line 77

def reset!
  self.replace(@_original_array)
  @opts = @_original_opts
  @_paginated_array = nil
  true
end

- (Object) total_entries Also known as: total_count



90
91
92
# File 'lib/mongoid_spacial/spacial/geo_near_results.rb', line 90

def total_entries
  @opts[:total_entries]
end