Clear
Constants
Log = ::Log.for("clear")
VERSION = "v0.10.0"
Class methods
apply_seeds
Sourceseed
Register a seed block.
this block will be called by Clear.apply_seeds
or conveniently by the CLI
using $cli_cmd migrate seeds
Macros
enum(name, *values, &block)
Enum
Clear offers full support of postgres enum strings.
Example
Let's say you need to define an enum for genders:
# Define the enum
Clear.enum MyApp::Gender, "male", "female" # , ...
In migration, we tell Postgres about the enum:
create_enum :gender, MyApp::Gender # < Create the new type `gender` in the database
create_table :users do |t|
# ...
t.gender "gender" # < first `gender` is the type of column, while second is the name of the column
end
Finally in your model, simply add the enum as column:
class User
include Clear::Model
# ...
column gender : MyApp::Gender
end
Now, you can assign the enum:
u = User.new
u.gender = MyApp::Gender::Male
You can dynamically check and build the enumeration values:
MyApp::Gender.authorized_values # < return ["male", "female"]
MyApp::Gender.all # < return [MyApp::Gender::Male, MyApp::Gender::Female]
MyApp::Gender.from_string("male") # < return MyApp::Gender::Male
MyApp::Gender.from_string("unknown") # < throw Clear::IllegalEnumValueError
(De)serialisation is also supported
MyApp::Gender.from_json("male") # < return MyApp::Gender::Male
MyApp::Gender.valid?("female") # < Return true
MyApp::Gender.valid?("unknown") # < Return false
However, you cannot write:
u = User.new
u.gender = "male"
But instead:
u = User.new
u.gender = MyApp::Gender::Male
json_serializable_converter(type)
Register a type to allow use in Clear column system. Type must include JSON::Serializable. More info about how to use JSON::Serializable it can be found here
Clear.json_serializable_converter(MyJsonType)
# ...
class YourModel
include Clear::Model
# ...
column my_column : MyJsonType # jsonb (recommended), json or string column in postgresql.
end