Sha256: ecad76069b4465fb57ac54b1e8d020eecffb7fc5545abdfd81ba801c82e4dc1e

Contents?: true

Size: 958 Bytes

Versions: 2

Compression:

Stored size: 958 Bytes

Contents

module CartoJson

  module Shape
    def self.included(base)
      base.extend(ClassMethods)
    end

    module ClassMethods
      attr_accessor :type

      def type(val=nil)
        @type = val unless val.nil?
        @type
      end

      def parse(input)
        new input
      end
    end

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

      if input.respond_to?(:each)
        input.each do |k,v|
          next if k == :type
          send "#{k}=".to_sym, v
        end
      end
    end

    def to_hash
      raise NotImplementedError, 'you need to provide the to_hash method'
    end

    def to_json
      MultiJson.encode to_hash
    end

    def to_pretty_json
      MultiJson.encode to_hash, :pretty => true
    end

    def type
      self.class.type
    end

    def to_wkt
      "#{self.class.name.split('::').last.upcase} (#{send(LNG)} #{send(LAT)})"
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
carto_json-0.0.7 lib/carto_json/shape.rb
carto_json-0.0.6 lib/carto_json/shape.rb