Skip to content

Instantly share code, notes, and snippets.

@manveru
Created November 17, 2019 21:10
Show Gist options
  • Select an option

  • Save manveru/6bc2760424b384912ed1f6550b09ccaf to your computer and use it in GitHub Desktop.

Select an option

Save manveru/6bc2760424b384912ed1f6550b09ccaf to your computer and use it in GitHub Desktop.
point type for avram
record Point, x : Float64, y : Float64
struct Point
def self.adapter
Lucky
end
module Lucky
alias ColumnType = Point
include Avram::Type
def from_db!(value : Point)
value
end
def parse(value : String) : SuccessfulCast( Point ) | FailedCast
if md = value.match(/\((\d+(?:\.\d+)?),(\d+(?:\.\d+)?)\)/)
SuccessfulCast(Point).new(Point.new(md[1].to_f64, md[2].to_f64))
else
FailedCast.new
end
end
def parse(value : Point)
SuccessfulCast(Point).new(value)
end
def to_db(value : Point)
"(#{value.x},#{value.y})"
end
end
end
module Avram::Migrator::Columns
class PointColumn(T) < Base
@default : T | Nil = nil
def initialize(@name, @nilable, @default)
end
def column_type : String
"point"
end
def self.prepare_value_for_database(value : Point)
escape_literal "(#{value.x},#{value.y})"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment