Class: Mongoid::Geospatial::Point
- Inherits:
-
Object
- Object
- Mongoid::Geospatial::Point
- Includes:
- Enumerable
- Defined in:
- lib/mongoid/geospatial/fields/point.rb
Overview
Point
Instance Attribute Summary collapse
-
#x ⇒ Object
Returns the value of attribute x.
-
#y ⇒ Object
Returns the value of attribute y.
-
#z ⇒ Object
Returns the value of attribute z.
Class Method Summary collapse
-
.demongoize(obj) ⇒ Object
Database -> Object Get it back.
-
.evolve(obj) ⇒ Object
Converts the object that was supplied to a criteria into a database friendly form.
-
.mongoize(obj) ⇒ Object
Object -> Database Send it to MongoDB.
Instance Method Summary collapse
- #[](args) ⇒ Object
- #each {|x| ... } ⇒ Object
-
#initialize(lon, lat, alt = nil) ⇒ Point
constructor
A new instance of Point.
-
#mongoize ⇒ Array
Object -> Database Let’s store NilClass if we are invalid.
-
#radius(r = 1) ⇒ Array
Helper for [self, radius].
-
#radius_sphere(r = 1, unit = :km) ⇒ Array
Radius Sphere.
-
#reverse ⇒ Array
Point inverse/reverse.
-
#to_a ⇒ Array
Object -> Database Let’s store NilClass if we are invalid.
-
#to_geo_json ⇒ String
Point definition as GeoJSON.
-
#to_hsh(xkey = :x, ykey = :y) ⇒ Hash
(also: #to_hash)
Point representation as a Hash Optional param: custom keys.
-
#to_s ⇒ String
Point definition as string.
-
#to_xy ⇒ Array
Object -> Database Let’s store NilClass if we are invalid.
-
#valid? ⇒ Boolean
Am I valid?.
Constructor Details
#initialize(lon, lat, alt = nil) ⇒ Point
Returns a new instance of Point.
11 12 13 14 15 |
# File 'lib/mongoid/geospatial/fields/point.rb', line 11 def initialize(lon, lat, alt = nil) @x = lon @y = lat @z = alt end |
Instance Attribute Details
#x ⇒ Object
Returns the value of attribute x.
9 10 11 |
# File 'lib/mongoid/geospatial/fields/point.rb', line 9 def x @x end |
#y ⇒ Object
Returns the value of attribute y.
9 10 11 |
# File 'lib/mongoid/geospatial/fields/point.rb', line 9 def y @y end |
#z ⇒ Object
Returns the value of attribute z.
9 10 11 |
# File 'lib/mongoid/geospatial/fields/point.rb', line 9 def z @z end |
Class Method Details
.demongoize(obj) ⇒ Object
Database -> Object Get it back
147 148 149 |
# File 'lib/mongoid/geospatial/fields/point.rb', line 147 def demongoize(obj) obj && new(*obj) end |
.evolve(obj) ⇒ Object
Converts the object that was supplied to a criteria into a database friendly form.
170 171 172 173 174 175 |
# File 'lib/mongoid/geospatial/fields/point.rb', line 170 def evolve(obj) case obj when Point then obj.mongoize else obj end end |
.mongoize(obj) ⇒ Object
Object -> Database Send it to MongoDB
154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/mongoid/geospatial/fields/point.rb', line 154 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
29 30 31 |
# File 'lib/mongoid/geospatial/fields/point.rb', line 29 def [](args) mongoize[args] end |
#each {|x| ... } ⇒ Object
33 34 35 36 |
# File 'lib/mongoid/geospatial/fields/point.rb', line 33 def each yield x yield y end |
#mongoize ⇒ Array
Object -> Database Let’s store NilClass if we are invalid.
21 22 23 24 25 |
# File 'lib/mongoid/geospatial/fields/point.rb', line 21 def mongoize return nil unless x && y [x, y] end |
#radius(r = 1) ⇒ Array
Helper for [self, radius]
54 55 56 |
# File 'lib/mongoid/geospatial/fields/point.rb', line 54 def radius(r = 1) # rubocop:disable Naming/MethodParameterName [mongoize, r] end |
#radius_sphere(r = 1, unit = :km) ⇒ Array
Radius Sphere
Validates that #x & #y are Numeric
65 66 67 |
# File 'lib/mongoid/geospatial/fields/point.rb', line 65 def radius_sphere(r = 1, unit = :km) # rubocop:disable Naming/MethodParameterName radius r.to_f / Mongoid::Geospatial.earth_radius[unit] end |
#reverse ⇒ Array
Point inverse/reverse
MongoDB: “x, y” Reverse: “y, x”
111 112 113 |
# File 'lib/mongoid/geospatial/fields/point.rb', line 111 def reverse [y, x] end |
#to_a ⇒ Array
Object -> Database Let’s store NilClass if we are invalid.
26 27 28 29 30 |
# File 'lib/mongoid/geospatial/fields/point.rb', line 26 def mongoize return nil unless x && y [x, y] end |
#to_geo_json ⇒ String
Point definition as GeoJSON
“x, y”
98 99 100 101 |
# File 'lib/mongoid/geospatial/fields/point.rb', line 98 def to_geo_json # Return a GeoJSON point hash that MongoDB can use { type: 'Point', coordinates: [x, y] } end |
#to_hsh(xkey = :x, ykey = :y) ⇒ Hash Also known as: to_hash
Point representation as a Hash Optional param: custom keys.
44 45 46 |
# File 'lib/mongoid/geospatial/fields/point.rb', line 44 def to_hsh(xkey = :x, ykey = :y) { xkey => x, ykey => y } end |
#to_s ⇒ String
Point definition as string
“x, y”
87 88 89 |
# File 'lib/mongoid/geospatial/fields/point.rb', line 87 def to_s "#{x}, #{y}" end |
#to_xy ⇒ Array
Object -> Database Let’s store NilClass if we are invalid.
27 28 29 30 31 |
# File 'lib/mongoid/geospatial/fields/point.rb', line 27 def mongoize return nil unless x && y [x, y] end |
#valid? ⇒ Boolean
Am I valid?
Validates that #x & #y are Numeric
76 77 78 |
# File 'lib/mongoid/geospatial/fields/point.rb', line 76 def valid? x && y && x.is_a?(Numeric) && y.is_a?(Numeric) end |