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 raise InputError, "\"#{k}\" is not a valid property" unless respond_to? k 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