Class: Mongoid::Location::GeoNearResults

Inherits:
Array
  • Object
show all
Defined in:
lib/mongoid_location/location/geo_near_results.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Array

#to_xy

Constructor Details

#initialize(document, results, opts = {}) ⇒ GeoNearResults

Returns 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
# File 'lib/mongoid_location/location/geo_near_results.rb', line 7

def initialize(document,results,opts = {})
  raise "#{document.name} class must include Mongoid::Location::Document" unless document.respond_to?(:spatial_fields_indexed)
  @document = document
  @opts = opts
  @_original_opts = opts.clone
  @stats = results['stats'] || {}
  @opts[:skip] ||= 0

  @_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.spatial_fields_indexed if @document.spatial_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.spatial_fields_indexed.kind_of?(Array) && @document.spatial_fields_indexed.size == 1
        primary = @document.spatial_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

#_original_arrayObject (readonly)

Returns the value of attribute _original_array.



4
5
6
# File 'lib/mongoid_location/location/geo_near_results.rb', line 4

def _original_array
  @_original_array
end

#_original_optsObject (readonly)

Returns the value of attribute _original_opts.



4
5
6
# File 'lib/mongoid_location/location/geo_near_results.rb', line 4

def _original_opts
  @_original_opts
end

#documentObject (readonly)

Returns the value of attribute document.



4
5
6
# File 'lib/mongoid_location/location/geo_near_results.rb', line 4

def document
  @document
end

#optsObject

Returns the value of attribute opts.



5
6
7
# File 'lib/mongoid_location/location/geo_near_results.rb', line 5

def opts
  @opts
end

#statsObject (readonly)

Returns the value of attribute stats.



4
5
6
# File 'lib/mongoid_location/location/geo_near_results.rb', line 4

def stats
  @stats
end

Instance Method Details

#current_pageObject



94
95
96
97
# File 'lib/mongoid_location/location/geo_near_results.rb', line 94

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

#limit_valueObject Also known as: per_page



99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/mongoid_location/location/geo_near_results.rb', line 99

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::Location.default_per_page
                       end
  end
end

#next_pageObject

current_page + 1 or nil if there is no next page



134
135
136
# File 'lib/mongoid_location/location/geo_near_results.rb', line 134

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

#num_pagesObject Also known as: total_pages



115
116
117
# File 'lib/mongoid_location/location/geo_near_results.rb', line 115

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

#offsetObject



124
125
126
# File 'lib/mongoid_location/location/geo_near_results.rb', line 124

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

#out_of_bounds?Boolean

Returns:

  • (Boolean)


120
121
122
# File 'lib/mongoid_location/location/geo_near_results.rb', line 120

def out_of_bounds?
  self.current_page > self.total_pages
end

#page(*args) ⇒ Object



49
50
51
52
53
# File 'lib/mongoid_location/location/geo_near_results.rb', line 49

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

#page!(page, options = {}) ⇒ Object



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

def page!(page, options = {})
  original = options.delete(:original)
  self.opts.merge!(options)
  self.opts[:paginator] ||= Mongoid::Location.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

#per(num) ⇒ Object



72
73
74
# File 'lib/mongoid_location/location/geo_near_results.rb', line 72

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

#previous_pageObject

current_page - 1 or nil if there is no previous page



129
130
131
# File 'lib/mongoid_location/location/geo_near_results.rb', line 129

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

#resetObject



83
84
85
86
87
# File 'lib/mongoid_location/location/geo_near_results.rb', line 83

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

#reset!Object



76
77
78
79
80
81
# File 'lib/mongoid_location/location/geo_near_results.rb', line 76

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

#total_entriesObject Also known as: total_count



89
90
91
# File 'lib/mongoid_location/location/geo_near_results.rb', line 89

def total_entries
  (@_paginated_array) ? @_paginated_array.count : @_original_array.count
end