Sha256: be23b4383d704cf42ff03198ca092b2186b0415a56652971b6bc41d59af7fd3b

Contents?: true

Size: 1.02 KB

Versions: 4

Compression:

Stored size: 1.02 KB

Contents

module CartoJson
  class Point
    include Shape

    type :point

    attr_accessor LAT, LNG

    def initialize(input)
      raise InputError, 'cannot create a shape with an array' if input.is_a?(Array)

      if input[LAT].nil? || input[LNG].nil?
        raise InputError, "#{LAT} and #{LNG} are required to instantiate a point"
      end

      input[LAT] = input[LAT].to_f
      input[LNG] = input[LNG].to_f

      super input

      unless (-90..90).include? input[LAT]
        raise InputError, "latitude must be between -90 and 90, \"#{input[LAT]}\" was provided"
      end

      unless (-180..180).include? input[LNG]
        raise InputError, "longitude must be between -180 and 180, \"#{input[LNG]}\" was provided"
      end
    end

    def to_hash
      {:type => self.class.type,
       LAT  => send(LAT),
       LNG  => send(LNG)}
    end

    def to_hash_for_array
      {LAT  => send(LAT),
       LNG  => send(LNG)}
    end
    
    def ==(point)
      send(LAT) == point.send(LAT) && send(LNG) == point.send(LNG)
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
carto_json-0.0.9 lib/carto_json/point.rb
carto_json-0.0.8 lib/carto_json/point.rb
carto_json-0.0.7 lib/carto_json/point.rb
carto_json-0.0.6 lib/carto_json/point.rb