Skip to content

Instantly share code, notes, and snippets.

@confact
Created March 23, 2020 15:02
Show Gist options
  • Save confact/aa9a88304ee1dc7babd93d058ecee5f5 to your computer and use it in GitHub Desktop.
Save confact/aa9a88304ee1dc7babd93d058ecee5f5 to your computer and use it in GitHub Desktop.
macro avram_enum(enum_name)
enum {{ enum_name }}
{{ yield }}
end
class Avram{{ enum_name }}
def self.adapter
Lucky
end
# You may need to prefix with {{ @type }}
#
# {{ @type }}::{{enum_name}}
def initialize(@enum : {{ enum_name }})
end
def initialize(enum_value : Int32)
@enum = {{ enum_name }}.new(enum_value)
end
def initialize(enum_value : String)
@enum = {{ enum_name }}.new(enum_value.to_i)
end
def to_s : String
@enum.to_s
end
def value
@enum
end
def blank?
@enum.nil?
end
module Lucky
alias ColumnType = Int32
include Avram::Type
def from_db!(value : Int32)
Avram{{ enum_name }}.new(value)
end
def parse(value : Avram{{ enum_name }})
SuccessfulCast(Avram{{ enum_name }}).new(value)
end
def parse(value : String)
SuccessfulCast(Avram{{ enum_name }}).new(Avram{{ enum_name }}.new(value.to_i))
end
def parse(value : Int32)
SuccessfulCast(Avram{{ enum_name }}).new(Avram{{ enum_name }}.new(value))
end
def to_db(value : Int32)
value.to_s
end
def to_db(value : Avram{{ enum_name }})
value.value.to_s
end
class Criteria(T, V) < Int32::Lucky::Criteria(T, V)
end
end
end
end
class MergeRequest < BaseModel
avram_enum Status do
Open = 0
Closed = 1
Merged = 2
end
table do
column name : String
column description : String
column branch : String
column status : AvramStatus
belongs_to repository : Repository
belongs_to author : User
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment