Sha256: 7004362695e53a18722b846cff05d5f5ef4698b81a1d5e377951b1cbd76ab435

Contents?: true

Size: 754 Bytes

Versions: 1

Compression:

Stored size: 754 Bytes

Contents

require 'geojson_model/base'

module GeojsonModel
  class Coder

    # takes the database value and returns an instance of the GeojsonModel class
    def self.load(value)
      dispatch(JSON.load(value))
    end

    # takes an object and returns the value to be stored in the database
    def self.dump(object)
      object.to_json
    end


    private

    # @param data [Hash]
    def self.dispatch(data)
      if hash.has_key?('geometries')
        GeometryCollection.new(data)
      elsif data.has_key?('coordinates')
        Geometry.new(data)
      elsif data.has_key?('features')
        FeatureCollection.new(data)
      elsif data.has_key?('properties')
        Feature.new(data)
      else
        raise TypeError
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
geojson_model-0.1.1 lib/geojson_model/coder.rb