Class: NoBrainer::GeoSpatial::Point

Inherits:
Struct
  • Object
show all
Defined in:
lib/nobrainer_geospatial/types/geo_point.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#latitudeObject

Returns the value of attribute latitude

Returns:

  • (Object)

    the current value of latitude



2
3
4
# File 'lib/nobrainer_geospatial/types/geo_point.rb', line 2

def latitude
  @latitude
end

#longitudeObject

Returns the value of attribute longitude

Returns:

  • (Object)

    the current value of longitude



2
3
4
# File 'lib/nobrainer_geospatial/types/geo_point.rb', line 2

def longitude
  @longitude
end

Class Method Details

.nobrainer_cast_db_to_model(value) ⇒ Object

This class method translates a value from the database to the proper type. It is used when reading from the database.



31
32
33
# File 'lib/nobrainer_geospatial/types/geo_point.rb', line 31

def nobrainer_cast_db_to_model(value)
  NoBrainer::GeoSpatial::Point.new(value['coordinates'][0], value['coordinates'][1])
end

.nobrainer_cast_model_to_db(value) ⇒ Object



25
26
27
# File 'lib/nobrainer_geospatial/types/geo_point.rb', line 25

def nobrainer_cast_model_to_db(value)
  RethinkDB::RQL.new.point(value.longitude, value.latitude)
end

.nobrainer_cast_user_to_model(value) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/nobrainer_geospatial/types/geo_point.rb', line 9

def nobrainer_cast_user_to_model(value)
  case value
  when NoBrainer::GeoSpatial::Point then value
  when Array then
       new(value[0], value[1])
  when Hash then
      longitude = value[:longitude] ||= value[:long] ||= value['longitude'] ||= value['long']
      latitude = value[:latitude] ||= value[:lat] ||= value['latitude'] ||= value['latitude']
      raise NoBrainer::Error::InvalidType.new('longitude out of range') if longitude < -180 || longitude > 180
      raise NoBrainer::Error::InvalidType.new('latitude out of range') if latitude < -90 || latitude > 90
      raise 'You must supply :longitude and :latitude!' unless latitude && longitude
      new(longitude, latitude)
  else raise NoBrainer::Error::InvalidType
  end
end

Instance Method Details

#to_rqlObject



4
5
6
# File 'lib/nobrainer_geospatial/types/geo_point.rb', line 4

def to_rql
  RethinkDB::RQL.new.point(self.longitude, self.latitude)
end