class Mongoid::Geospatial::Point

Point

Attributes

x[RW]
y[RW]
z[RW]

Public Class Methods

demongoize(obj) click to toggle source

Database -> Object Get it back

# File lib/mongoid/geospatial/fields/point.rb, line 132
def demongoize(obj)
  obj && new(*obj)
end
evolve(obj) click to toggle source

Converts the object that was supplied to a criteria into a database friendly form.

# File lib/mongoid/geospatial/fields/point.rb, line 155
def evolve(obj)
  case obj
  when Point then obj.mongoize
  else obj
  end
end
mongoize(obj) click to toggle source

Object -> Database Send it to MongoDB

# File lib/mongoid/geospatial/fields/point.rb, line 139
def mongoize(obj)
  case obj
  when Point  then obj.mongoize
  when String then from_string(obj)
  when Array  then from_array(obj)
  when Hash   then from_hash(obj)
  when NilClass then nil
  else
    return obj.to_xy if obj.respond_to?(:to_xy)

    raise 'Invalid Point'
  end
end
new(x, y, z = nil) click to toggle source
# File lib/mongoid/geospatial/fields/point.rb, line 9
def initialize(x, y, z = nil)
  @x = x
  @y = y
  @z = z
end

Private Class Methods

from_array(array) click to toggle source

Sanitize a `Point` from an `Array`

Also makes life easier:

-> []

1,2

-> [1.0, 2.0]

@return (Array)

# File lib/mongoid/geospatial/fields/point.rb, line 189
def from_array(array)
  return nil if array.empty?

  array.flatten[0..1].map(&:to_f)
end
from_hash(hsh) click to toggle source

Sanitize a `Point` from a `Hash`

Uses Mongoid::Geospatial.lat_symbols & lng_symbols

Also makes life easier: {x: 1.0, y: 2.0} -> [1.0, 2.0] {lat: 1.0, lon: 2.0} -> [1.0, 2.0] {lat: 1.0, long: 2.0} -> [1.0, 2.0]

Throws error if hash has less than 2 items.

@return (Array)

# File lib/mongoid/geospatial/fields/point.rb, line 209
def from_hash(hsh)
  raise 'Hash must have at least 2 items' if hsh.size < 2

  [from_hash_x(hsh), from_hash_y(hsh)]
end
from_hash_x(hsh) click to toggle source
# File lib/mongoid/geospatial/fields/point.rb, line 222
def from_hash_x(hsh)
  v = (Mongoid::Geospatial::Config::Point.x & hsh.keys).first
  return hsh[v].to_f if !v.nil? && hsh[v]

  raise "Hash must contain #{Mongoid::Geospatial::Config::Point.x.inspect}"
end
from_hash_y(hsh) click to toggle source
# File lib/mongoid/geospatial/fields/point.rb, line 215
def from_hash_y(hsh)
  v = (Mongoid::Geospatial::Config::Point.y & hsh.keys).first
  return hsh[v].to_f if !v.nil? && hsh[v]

  raise "Hash must contain #{Mongoid::Geospatial::Config::Point.y.inspect}"
end
from_string(str) click to toggle source

Sanitize a `Point` from a `String`

Makes life easier: “” -> [] “1, 2” -> [1.0, 2.0] “1.1 2.2” -> [1.1, 2.2]

@return (Array)

# File lib/mongoid/geospatial/fields/point.rb, line 174
def from_string(str)
  return nil if str.empty?

  str.split(/,|\s/).reject(&:empty?).map(&:to_f)
end

Public Instance Methods

[](args) click to toggle source
# File lib/mongoid/geospatial/fields/point.rb, line 27
def [](args)
  mongoize[args]
end
each() { |x| ... } click to toggle source
# File lib/mongoid/geospatial/fields/point.rb, line 31
def each
  yield x
  yield y
end
mongoize() click to toggle source

Object -> Database Let's store NilClass if we are invalid.

@return (Array)

# File lib/mongoid/geospatial/fields/point.rb, line 19
def mongoize
  return nil unless x && y

  [x, y]
end
Also aliased as: to_a, to_xy
radius(r = 1) click to toggle source

Helper for [self, radius]

@return [Array] with [self, radius]

# File lib/mongoid/geospatial/fields/point.rb, line 51
def radius(r = 1)
  [mongoize, r]
end
radius_sphere(r = 1, unit = :km) click to toggle source

Radius Sphere

Validates that x & y are `Numeric`

@return [Array] with [self, radius / earth radius]

# File lib/mongoid/geospatial/fields/point.rb, line 62
def radius_sphere(r = 1, unit = :km)
  radius r.to_f / Mongoid::Geospatial.earth_radius[unit]
end
reverse() click to toggle source

Point inverse/reverse

MongoDB: “x, y” Reverse: “y, x”

@return [Array] Point reversed: “y, x”

# File lib/mongoid/geospatial/fields/point.rb, line 96
def reverse
  [y, x]
end
to_a()
Alias for: mongoize
to_hash(xl = :x, yl = :y)
Alias for: to_hsh
to_hsh(xl = :x, yl = :y) click to toggle source

Point representation as a Hash

@return [Hash] with { xl => x, yl => y }

# File lib/mongoid/geospatial/fields/point.rb, line 41
def to_hsh(xl = :x, yl = :y)
  { xl => x, yl => y }
end
Also aliased as: to_hash
to_s() click to toggle source

Point definition as string

“x, y”

@return [String] Point as comma separated String

# File lib/mongoid/geospatial/fields/point.rb, line 84
def to_s
  "#{x}, #{y}"
end
to_xy()
Alias for: mongoize
valid?() click to toggle source

Am I valid?

Validates that x & y are `Numeric`

@return [Boolean] if self x && y are valid

# File lib/mongoid/geospatial/fields/point.rb, line 73
def valid?
  x && y && x.is_a?(Numeric) && y.is_a?(Numeric)
end