Skip to content

Commit

Permalink
feat: Support for Point2D
Browse files Browse the repository at this point in the history
  • Loading branch information
fujikawahiroaki committed Jul 17, 2023
0 parents commit 49715ef
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 fujikawahiroaki <[email protected]>

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.
45 changes: 45 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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 (<https://github.com/your-github-user/lucky_postgis/fork>)
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
19 changes: 19 additions & 0 deletions shard.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: lucky_postgis
version: 0.1.0

authors:
- fujikawahiroaki <[email protected]>

crystal: ">= 0.36"

license: MIT

crystalline:
main: src/lucky_postgis.cr"

dependencies:
avram:
github: luckyframework/avram

postgis:
github: fujikawahiroaki/postgis
9 changes: 9 additions & 0 deletions spec/lucky_postgis_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require "./spec_helper"

describe LuckyPostgis do
# TODO: Write tests

it "works" do
false.should eq(true)
end
end
2 changes: 2 additions & 0 deletions spec/spec_helper.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require "spec"
require "../src/lucky_postgis"
1 change: 1 addition & 0 deletions src/lucky_postgis.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require "./point2d_extensions"
56 changes: 56 additions & 0 deletions src/point2d_extensions.cr
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 49715ef

Please sign in to comment.