module Clear

Defined in:

clear/cli.cr
clear/core.cr
clear/extensions/enum/enum.cr
clear/extensions/time/interval.cr
clear/model/converters/json_any_converter.cr
clear/seed.cr
clear/sql/lock.cr
clear/sql/sql.cr
clear/sql/truncate.cr
clear/version.cr

Constant Summary

Log = ::Log.for("clear")
VERSION = "v0.9"

Class Method Summary

Macro Summary

Class Method Detail

def self.apply_seeds #

[View source]
def self.seed(&block) #

Register a seed block. this block will be called by Clear.apply_seeds or conveniently by the CLI using $cli_cmd migrate seeds


[View source]
def self.with_cli(&) #

Check for the CLI. If the CLI is not triggered, yield the block passed as parameter


[View source]

Macro Detail

macro 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

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

[View source]
macro 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

[View source]