Class: NoBrainer::Geo::Point

Inherits:
Struct
  • Object
show all
Includes:
Base
Defined in:
lib/no_brainer/geo/point.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Base

normalize_geo_options

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

#latitudeObject

Returns the value of attribute latitude

Returns:

  • (Object)

    the current value of latitude



3
4
5
# File 'lib/no_brainer/geo/point.rb', line 3

def latitude
  @latitude
end

#longitudeObject

Returns the value of attribute longitude

Returns:

  • (Object)

    the current value of 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_rqlObject



28
29
30
# File 'lib/no_brainer/geo/point.rb', line 28

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

#to_sObject Also known as: inspect



32
33
34
# File 'lib/no_brainer/geo/point.rb', line 32

def to_s
  [longitude, latitude].inspect
end