Skip to content

Instantly share code, notes, and snippets.

@confact
Last active March 25, 2020 13:31
Show Gist options
  • Save confact/9f2e9048935ce33196697f3393223910 to your computer and use it in GitHub Desktop.
Save confact/9f2e9048935ce33196697f3393223910 to your computer and use it in GitHub Desktop.
Trying to add db parsing to enum and cast it correctly.
module AvramEnum
macro included
def self.adapter
Lucky
end
module Lucky
alias ColumnType = Int32
include Avram::Type
def from_db!(value : Int32)
{{@type}}.new(value)
end
def parse(value : String)
{{@type}}.from_value(value.to_i)
rescue ArgumentError
FailedCast.new
end
def parse(value : Int32)
{{@type}}.from_value(value)
rescue ArgumentError
FailedCast.new
end
def to_db(value : Int32)
value.to_s
end
def to_db(value : Enum)
value.value.to_s
end
class Criteria(T, V) < Avram::Criteria(T, V)
@upper = false
@lower = false
end
end
end
end
struct Enum
macro inherited
include AvramEnum
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment