From 49715ef4e1adf9dbd2fb99d8645c7ffb941c9cac Mon Sep 17 00:00:00 2001 From: fujikawahiroaki Date: Mon, 17 Jul 2023 11:27:05 +0900 Subject: [PATCH] feat: Support for Point2D --- LICENSE | 21 ++++++++++++++ README.md | 45 ++++++++++++++++++++++++++++++ shard.yml | 19 +++++++++++++ spec/lucky_postgis_spec.cr | 9 ++++++ spec/spec_helper.cr | 2 ++ src/lucky_postgis.cr | 1 + src/point2d_extensions.cr | 56 ++++++++++++++++++++++++++++++++++++++ 7 files changed, 153 insertions(+) create mode 100644 LICENSE create mode 100644 README.md create mode 100644 shard.yml create mode 100644 spec/lucky_postgis_spec.cr create mode 100644 spec/spec_helper.cr create mode 100644 src/lucky_postgis.cr create mode 100644 src/point2d_extensions.cr diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..b6c3e98 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2023 fujikawahiroaki + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..d69650b --- /dev/null +++ b/README.md @@ -0,0 +1,45 @@ +# lucky_postgis + +Library for using PostGIS types in the Lucky framework and Avram ORM. + +## Caution + +Currently the only supported type is Point2D. + +Arrays of type Point2D are not yet supported. + +Other types will be added in the future. + +## Installation + +1. Add the dependency to your `shard.yml`: + + ```yaml + dependencies: + lucky_postgis: + github: fujikawahiroaki/lucky_postgis + ``` + +2. Run `shards install` + +## Usage + +```crystal +require "lucky_postgis" +``` + +## Development + +TODO: Write development instructions here + +## Contributing + +1. Fork it () +2. Create your feature branch (`git checkout -b my-new-feature`) +3. Commit your changes (`git commit -am 'Add some feature'`) +4. Push to the branch (`git push origin my-new-feature`) +5. Create a new Pull Request + +## Contributors + +- [fujikawahiroaki](https://github.com/your-github-user) - creator and maintainer diff --git a/shard.yml b/shard.yml new file mode 100644 index 0000000..334940d --- /dev/null +++ b/shard.yml @@ -0,0 +1,19 @@ +name: lucky_postgis +version: 0.1.0 + +authors: + - fujikawahiroaki + +crystal: ">= 0.36" + +license: MIT + +crystalline: + main: src/lucky_postgis.cr" + +dependencies: + avram: + github: luckyframework/avram + + postgis: + github: fujikawahiroaki/postgis diff --git a/spec/lucky_postgis_spec.cr b/spec/lucky_postgis_spec.cr new file mode 100644 index 0000000..fed7a0b --- /dev/null +++ b/spec/lucky_postgis_spec.cr @@ -0,0 +1,9 @@ +require "./spec_helper" + +describe LuckyPostgis do + # TODO: Write tests + + it "works" do + false.should eq(true) + end +end diff --git a/spec/spec_helper.cr b/spec/spec_helper.cr new file mode 100644 index 0000000..abbaf45 --- /dev/null +++ b/spec/spec_helper.cr @@ -0,0 +1,2 @@ +require "spec" +require "../src/lucky_postgis" diff --git a/src/lucky_postgis.cr b/src/lucky_postgis.cr new file mode 100644 index 0000000..9cab4f1 --- /dev/null +++ b/src/lucky_postgis.cr @@ -0,0 +1 @@ +require "./point2d_extensions" diff --git a/src/point2d_extensions.cr b/src/point2d_extensions.cr new file mode 100644 index 0000000..a6d4caa --- /dev/null +++ b/src/point2d_extensions.cr @@ -0,0 +1,56 @@ +require "avram" +require "postgis" + +struct PostGIS::Point2D + include JSON::Serializable + @[JSON::Field(key: "x")] + property x : Float64 + + @[JSON::Field(key: "y")] + property y : Float64 + + @[JSON::Field(key: "srid")] + property srid : UInt32 + + def self.adapter + Lucky + end + + module Lucky + alias ColumnType = PostGIS::Point2D + include Avram::Type + + def self.criteria(query : T, column) forall T + Criteria(T, Point2D).new(query, column) + end + + def from_db!(value : PostGIS::Point2D) + value + end + + def parse(value : PostGIS::Point2D) + SuccessfulCast(PostGIS::Point2D).new(value) + end + + def parse(value : String) + begin + point_data = Hash(String, (UInt32 | Float64)).from_json(value) + x = point_data["x"].as(Float64) + y = point_data["y"].as(Float64) + srid = point_data["srid"].as(UInt32) + point = PostGIS::Point2D.new(x, y, srid) + SuccessfulCast(PostGIS::Point2D).new(point) + rescue + FailedCast.new + end + end + + def to_db(value : PostGIS::Point2D) + "POINT (#{value.x} #{value.y})" + end + + class Criteria(T, V) < Avram::Criteria(T, V) + include Avram::IncludesCriteria(T, V) + end + end +end