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(x, y, z = 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_hsh(xl = :x, yl = :y) ⇒ Hash
(also: #to_hash)
Point representation as a Hash.
-
#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(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
#x ⇒ Object
Returns the value of attribute x.
7 8 9 |
# File 'lib/mongoid/geospatial/fields/point.rb', line 7 def x @x end |
#y ⇒ Object
Returns the value of attribute y.
7 8 9 |
# File 'lib/mongoid/geospatial/fields/point.rb', line 7 def y @y end |
#z ⇒ Object
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
31 32 33 34 |
# File 'lib/mongoid/geospatial/fields/point.rb', line 31 def each yield x yield y end |
#mongoize ⇒ Array
Object -> Database Let’s store NilClass if we are invalid.
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]
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`
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 |
#reverse ⇒ Array
Point inverse/reverse
MongoDB: “x, y” Reverse: “y, x”
96 97 98 |
# File 'lib/mongoid/geospatial/fields/point.rb', line 96 def reverse [y, x] end |
#to_a ⇒ Array
Object -> Database Let’s store NilClass if we are invalid.
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
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_s ⇒ String
Point definition as string
“x, y”
84 85 86 |
# File 'lib/mongoid/geospatial/fields/point.rb', line 84 def to_s "#{x}, #{y}" end |
#to_xy ⇒ Array
Object -> Database Let’s store NilClass if we are invalid.
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`
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 |