Converters
Declare a new converter
require "./base"
struct MyApp::Color
property r : UInt8 = 0
property g : UInt8 = 0
property b : UInt8 = 0
property a : UInt8 = 0
def to_s
# ...
end
def self.from_string(x : String)
# ...
end
def self.from_slice(x : Slice(UInt8))
# ...
end
end
class MyApp::ColorConverter
def self.to_column(x) : MyApp::Color?
case x
when Nil
nil
when Slice(UInt8)
MyApp::Color.from_slice(x)
when String
MyApp::Color.from_string(x)
else
raise "Cannot convert from #{x.class} to MyApp::Color"
end
end
def self.to_db(x : MyApp::Color?)
x.to_s #< css style output, e.g. #12345400
end
end
Clear::Model::Converter.add_converter("MyApp::Color", MyApp::ColorConverter)converter option
converter optionLast updated