Class: Mongoid::Geospatial::Point

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/mongoid/geospatial/fields/point.rb

Overview

Point

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x, y, z = nil) ⇒ Point

Returns a new instance of Point.



9
10
11
12
13
# File 'lib/mongoid/geospatial/fields/point.rb', line 9

def initialize(x, y, z = nil)
  @x = x
  @y = y
  @z = z
end

Instance Attribute Details

#xObject

Returns the value of attribute x.



7
8
9
# File 'lib/mongoid/geospatial/fields/point.rb', line 7

def x
  @x
end

#yObject

Returns the value of attribute y.



7
8
9
# File 'lib/mongoid/geospatial/fields/point.rb', line 7

def y
  @y
end

#zObject

Returns the value of attribute z.



7
8
9
# File 'lib/mongoid/geospatial/fields/point.rb', line 7

def z
  @z
end

Class Method Details

.demongoize(obj) ⇒ Object

Database -> Object Get it back



132
133
134
# File 'lib/mongoid/geospatial/fields/point.rb', line 132

def demongoize(obj)
  obj && new(*obj)
end

.evolve(obj) ⇒ Object

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



155
156
157
158
159
160
# 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) ⇒ Object

Object -> Database Send it to MongoDB



139
140
141
142
143
144
145
146
147
148
149
150
151
# 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

Instance Method Details

#[](args) ⇒ Object



27
28
29
# File 'lib/mongoid/geospatial/fields/point.rb', line 27

def [](args)
  mongoize[args]
end

#each {|x| ... } ⇒ Object

Yields:

  • (x)


31
32
33
34
# File 'lib/mongoid/geospatial/fields/point.rb', line 31

def each
  yield x
  yield y
end

#mongoizeArray

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

Returns:

  • (Array)


19
20
21
22
23
# File 'lib/mongoid/geospatial/fields/point.rb', line 19

def mongoize
  return nil unless x && y

  [x, y]
end

#radius(r = 1) ⇒ Array

Helper for [self, radius]

Returns:

  • (Array)

    with [self, radius]



51
52
53
# File 'lib/mongoid/geospatial/fields/point.rb', line 51

def radius(r = 1)
  [mongoize, r]
end

#radius_sphere(r = 1, unit = :km) ⇒ Array

Radius Sphere

Validates that #x & #y are ‘Numeric`

Returns:

  • (Array)

    with [self, radius / earth radius]



62
63
64
# 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

#reverseArray

Point inverse/reverse

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

Returns:

  • (Array)

    Point reversed: “y, x”



96
97
98
# File 'lib/mongoid/geospatial/fields/point.rb', line 96

def reverse
  [y, x]
end

#to_aArray

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

Returns:

  • (Array)


24
25
26
27
28
# File 'lib/mongoid/geospatial/fields/point.rb', line 24

def mongoize
  return nil unless x && y

  [x, y]
end

#to_hsh(xl = :x, yl = :y) ⇒ Hash Also known as: to_hash

Point representation as a Hash

Returns:

  • (Hash)

    with { xl => x, yl => y }



41
42
43
# File 'lib/mongoid/geospatial/fields/point.rb', line 41

def to_hsh(xl = :x, yl = :y)
  { xl => x, yl => y }
end

#to_sString

Point definition as string

“x, y”

Returns:

  • (String)

    Point as comma separated String



84
85
86
# File 'lib/mongoid/geospatial/fields/point.rb', line 84

def to_s
  "#{x}, #{y}"
end

#to_xyArray

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

Returns:

  • (Array)


25
26
27
28
29
# File 'lib/mongoid/geospatial/fields/point.rb', line 25

def mongoize
  return nil unless x && y

  [x, y]
end

#valid?Boolean

Am I valid?

Validates that #x & #y are ‘Numeric`

Returns:

  • (Boolean)

    if self #x && #y are valid



73
74
75
# File 'lib/mongoid/geospatial/fields/point.rb', line 73

def valid?
  x && y && x.is_a?(Numeric) && y.is_a?(Numeric)
end