Class: NoBrainer::Geo::Point
- Inherits:
-
Struct
- Object
- Struct
- NoBrainer::Geo::Point
- Includes:
- Base
- Defined in:
- lib/no_brainer/geo/point.rb
Instance Attribute Summary collapse
-
#latitude ⇒ Object
Returns the value of attribute latitude.
-
#longitude ⇒ Object
Returns the value of attribute longitude.
Class Method Summary collapse
- .nobrainer_cast_db_to_model(value) ⇒ Object
- .nobrainer_cast_model_to_db(value) ⇒ Object
- .nobrainer_cast_user_to_model(value) ⇒ Object
Instance Method Summary collapse
-
#initialize(*args) ⇒ Point
constructor
A new instance of Point.
- #to_rql ⇒ Object
- #to_s ⇒ Object (also: #inspect)
Methods included from Base
Constructor Details
#initialize(*args) ⇒ Point
Returns a new instance of Point.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/no_brainer/geo/point.rb', line 6 def initialize(*args) if args.size == 2 longitude, latitude = args elsif args.size == 1 && args.first.is_a?(self.class) longitude, latitude = args.first.longitude, args.first.latitude elsif args.size == 1 && args.first.is_a?(Hash) opt = args.first.symbolize_keys longitude, latitude = opt[:longitude] || opt[:long], opt[:latitude] || opt[:lat] else raise NoBrainer::Error::InvalidType end longitude = Float.nobrainer_cast_user_to_model(longitude) latitude = Float.nobrainer_cast_user_to_model(latitude) raise NoBrainer::Error::InvalidType unless (-180..180).include?(longitude) raise NoBrainer::Error::InvalidType unless (-90..90).include?(latitude) self.longitude = longitude self.latitude = latitude end |
Instance Attribute Details
#latitude ⇒ Object
Returns the value of attribute latitude
3 4 5 |
# File 'lib/no_brainer/geo/point.rb', line 3 def latitude @latitude end |
#longitude ⇒ Object
Returns the value of attribute longitude
3 4 5 |
# File 'lib/no_brainer/geo/point.rb', line 3 def longitude @longitude end |
Class Method Details
.nobrainer_cast_db_to_model(value) ⇒ Object
41 42 43 44 |
# File 'lib/no_brainer/geo/point.rb', line 41 def self.nobrainer_cast_db_to_model(value) return value unless value.is_a?(Hash) && value['coordinates'].is_a?(Array) && value['coordinates'].size == 2 new(value['coordinates'][0], value['coordinates'][1]) end |
.nobrainer_cast_model_to_db(value) ⇒ Object
46 47 48 |
# File 'lib/no_brainer/geo/point.rb', line 46 def self.nobrainer_cast_model_to_db(value) value.is_a?(self) ? value.to_rql : value end |
.nobrainer_cast_user_to_model(value) ⇒ Object
37 38 39 |
# File 'lib/no_brainer/geo/point.rb', line 37 def self.nobrainer_cast_user_to_model(value) value.is_a?(Array) ? new(*value) : new(value) end |
Instance Method Details
#to_rql ⇒ Object
28 29 30 |
# File 'lib/no_brainer/geo/point.rb', line 28 def to_rql RethinkDB::RQL.new.point(longitude, latitude) end |
#to_s ⇒ Object Also known as: inspect
32 33 34 |
# File 'lib/no_brainer/geo/point.rb', line 32 def to_s [longitude, latitude].inspect end |