Class: Opium::GeoPoint

Inherits:
Object show all
Includes:
Comparable
Defined in:
lib/opium/extensions/geo_point.rb

Constant Summary collapse

NULL_ISLAND =
new( [0, 0] ).freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ GeoPoint

Returns a new instance of GeoPoint.



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/opium/extensions/geo_point.rb', line 5

def initialize( value )
  self.latitude, self.longitude = *
    case value
    when Hash
      [value[:latitude] || value['latitude'], value[:longitude] || value['longitude']]
    when Array
      [value.first, value.last]
    when /^[+-]?\d+(\.\d+)?\s*,\s*[+-]?\d+(\.\d+)?$/
      value.split(',').map {|c| c.to_f}
    else
      raise ArgumentError.new( "invalid value for GeoPoint: \"#{ value }\"" )
    end
end

Instance Attribute Details

#latitudeObject

Returns the value of attribute latitude.



19
20
21
# File 'lib/opium/extensions/geo_point.rb', line 19

def latitude
  @latitude
end

#longitudeObject

Returns the value of attribute longitude.



19
20
21
# File 'lib/opium/extensions/geo_point.rb', line 19

def longitude
  @longitude
end

Class Method Details

.===(other) ⇒ Object



51
52
53
# File 'lib/opium/extensions/geo_point.rb', line 51

def ===( other )
  other != NULL_ISLAND && super
end

.to_parse(object) ⇒ Object



59
60
61
# File 'lib/opium/extensions/geo_point.rb', line 59

def to_parse(object)
  object.to_geo_point.to_parse
end

.to_ruby(object) ⇒ Object



55
56
57
# File 'lib/opium/extensions/geo_point.rb', line 55

def to_ruby(object)
  object.to_geo_point unless object.nil?
end

Instance Method Details

#<=>(geo_point) ⇒ Object



33
34
35
36
# File 'lib/opium/extensions/geo_point.rb', line 33

def <=>( geo_point )
  return nil unless geo_point.is_a?( self.class )
  [self.latitude, self.longitude] <=> [geo_point.latitude, geo_point.longitude]
end

#===(other) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'lib/opium/extensions/geo_point.rb', line 38

def ===( other )
  if other.is_a? self.class
    self == other
  elsif other <= self.class
    self != NULL_ISLAND
  else
    nil
  end
end

#to_geo_pointObject



21
22
23
# File 'lib/opium/extensions/geo_point.rb', line 21

def to_geo_point
  self
end

#to_parseObject



25
26
27
# File 'lib/opium/extensions/geo_point.rb', line 25

def to_parse
  { "__type" => "GeoPoint", "latitude" => self.latitude, "longitude" => self.longitude }
end

#to_sObject



29
30
31
# File 'lib/opium/extensions/geo_point.rb', line 29

def to_s
  "#{ self.latitude },#{ self.longitude }"
end