class Repository

Included Modules

Extended Modules

Defined in:

models/repository.cr

Constant Summary

COLUMNS = {"id" => {type: Int64, primary: true, converter: "Int64", db_column_name: "id", crystal_variable_name: id, presence: false}, "provider" => {type: String, primary: false, converter: "String", db_column_name: "provider", crystal_variable_name: provider, presence: true}, "provider_id" => {type: Int32, primary: false, converter: "Int32", db_column_name: "provider_id", crystal_variable_name: provider_id, presence: true}, "name" => {type: String, primary: false, converter: "String", db_column_name: "name", crystal_variable_name: name, presence: true}, "description" => {type: String | ::Nil, primary: false, converter: "String", db_column_name: "description", crystal_variable_name: description, presence: true}, "shard_yml" => {type: String | ::Nil, primary: false, converter: "String", db_column_name: "shard_yml", crystal_variable_name: shard_yml, presence: true}, "readme" => {type: String | ::Nil, primary: false, converter: "String", db_column_name: "readme", crystal_variable_name: readme, presence: true}, "changelog" => {type: String | ::Nil, primary: false, converter: "String", db_column_name: "changelog", crystal_variable_name: changelog, presence: true}, "license" => {type: String | ::Nil, primary: false, converter: "String", db_column_name: "license", crystal_variable_name: license, presence: true}, "last_activity_at" => {type: Time, primary: false, converter: "Time", db_column_name: "last_activity_at", crystal_variable_name: last_activity_at, presence: true}, "stars_count" => {type: Int32, primary: false, converter: "Int32", db_column_name: "stars_count", crystal_variable_name: stars_count, presence: true}, "forks_count" => {type: Int32, primary: false, converter: "Int32", db_column_name: "forks_count", crystal_variable_name: forks_count, presence: true}, "open_issues_count" => {type: Int32 | ::Nil, primary: false, converter: "Int32", db_column_name: "open_issues_count", crystal_variable_name: open_issues_count, presence: true}, "archived" => {type: Bool, primary: false, converter: "Bool", db_column_name: "archived", crystal_variable_name: archived, presence: true}, "ignore" => {type: Bool, primary: false, converter: "Bool", db_column_name: "ignore", crystal_variable_name: ignore, presence: true}, "synced_at" => {type: Time, primary: false, converter: "Time", db_column_name: "synced_at", crystal_variable_name: synced_at, presence: true}, "created_at" => {type: Time | ::Nil, primary: false, converter: "Time", db_column_name: "created_at", crystal_variable_name: created_at, presence: true}, "updated_on" => {type: Time | ::Nil, primary: false, converter: "Time", db_column_name: "updated_on", crystal_variable_name: updated_on, presence: true}, "tsv" => {type: Clear::TSVector, primary: false, converter: "Clear::TSVector", db_column_name: "tsv", crystal_variable_name: tsv, presence: false}, "user_id" => {type: Int64, primary: false, converter: "Int64", db_column_name: "user_id", crystal_variable_name: user_id, presence: false}} of Nil => Nil
POLYMORPHISM_SETTINGS = {} of Nil => Nil

Constructors

Class Method Summary

Instance Method Summary

Constructor Detail

def self.create(x : Hash) : self #

[View source]
def self.create(x : NamedTuple) : self #

[View source]
def self.create(**args) : self #

Build and new model and save it. Returns the model.

The model may not be saved due to validation failure; check the returned model errors? and persisted? flags.


[View source]
def self.create!(a : Hash) : self #

[View source]
def self.create!(x : NamedTuple) : self #

[View source]
def self.create!(**args) : self #

Build and new model and save it. Returns the model.

Returns the newly inserted model Raises an exception if validation failed during the saving process.


[View source]
def self.new(h : Hash(String, _), cache : Clear::Model::QueryCache | Nil = nil, persisted = false, fetch_columns = false) #

[View source]
def self.new(json : JSON::Any, cache : Clear::Model::QueryCache | Nil = nil, persisted = false) #

[View source]
def self.new(t : NamedTuple, persisted = false) #

[View source]
def self.new #

[View source]

Class Method Detail

def self.build(**x : **T) forall T #

Build a new empty model and fill the columns using the NamedTuple in argument.

Returns the new model


[View source]
def self.columns #

[View source]
def self.connection : String #

Define on which connection the model is living. Useful in case of models living in different databases.

Is set to "default" by default.

See Clear::SQL#init(URI, *opts) for more information about multi-connections.

Example:

 Clear::SQL.init("postgres://postgres@localhost/database_1", connection_pool_size: 5)
 Clear::SQL.init("secondary", "postgres://postgres@localhost/database_2", connection_pool_size: 5)

class ModelA
  include Clear::Model

  # Performs all the queries on `database_1`
  # self.connection = "default"
  column id : Int32, primary: true, presence: false
  column title : String
end

class ModelB
  include Clear::Model

  # Performs all the queries on `database_2`
  self.connection = "secondary"

  column id : Int32, primary: true, presence: false
end

def self.connection=(connection : String) #

Define on which connection the model is living. Useful in case of models living in different databases.

Is set to "default" by default.

See Clear::SQL#init(URI, *opts) for more information about multi-connections.

Example:

 Clear::SQL.init("postgres://postgres@localhost/database_1", connection_pool_size: 5)
 Clear::SQL.init("secondary", "postgres://postgres@localhost/database_2", connection_pool_size: 5)

class ModelA
  include Clear::Model

  # Performs all the queries on `database_1`
  # self.connection = "default"
  column id : Int32, primary: true, presence: false
  column title : String
end

class ModelB
  include Clear::Model

  # Performs all the queries on `database_2`
  self.connection = "secondary"

  column id : Int32, primary: true, presence: false
end

def self.create(x : Array(NamedTuple)) : Array(self) #

Multi-models creation. See Collection#create(**args)

Returns the list of newly created model.

Each model will call an INSERT query. You may want to use Collection#import to insert multiple model more efficiently in one query.


[View source]
def self.create!(x : Array(NamedTuple)) : Array(self) #

Multi-models creation. See Collection#create!(**args)

Returns the list of newly created model. Raises exception if any of the model has validation error.


[View source]
def self.find(x) #

Returns a model using primary key equality Returns nil if not found.


[View source]
def self.find!(x) #

Returns a model using primary key equality. Raises error if the model is not found.


[View source]
def self.find_repository(user_login : String, repository_name : String, provider : String) : Repository | Nil #

[View source]
def self.import(array : Enumerable(self), on_conflict : Clear::SQL::InsertQuery -> | Nil = nil) #

Import a bulk of models in one SQL insert query. Each model must be non-persisted.

on_conflict callback can be optionnaly turned on to manage constraints of the database.

Note: Old models are not modified. This method return a copy of the models as saved in the database.

Example:


 users = [ User.new(id: 1), User.new(id: 2), User.new(id: 3)]
 users = User.import(users)

[View source]
def self.polymorphic? : Bool #

def self.published #

[View source]
def self.query #

Return a new empty query SELECT * FROM [my_model_table]. Can be refined after that.


[View source]
def self.read_only=(read_only : Bool) #

def self.read_only? : Bool #

def self.schema : Clear::SQL::Symbolic | Nil #

Define the current schema used in PostgreSQL. The value is nil by default, which lead to non-specified schema during the querying, and usage of "public" by PostgreSQL.

This property can be redefined on initialization. Example:

  class MyModel
    include Clear::Model

    self.schema = "my_schema"
  end
  MyModel.query.to_sql # SELECT * FROM "my_schema"."my_models"

def self.schema=(schema : Clear::SQL::Symbolic | Nil) #

Define the current schema used in PostgreSQL. The value is nil by default, which lead to non-specified schema during the querying, and usage of "public" by PostgreSQL.

This property can be redefined on initialization. Example:

  class MyModel
    include Clear::Model

    self.schema = "my_schema"
  end
  MyModel.query.to_sql # SELECT * FROM "my_schema"."my_models"

def self.search(str) #

def self.table : Clear::SQL::Symbolic #

Return the table name setup for this model. By convention, the class name is by default equals to the pluralized underscored string form of the model name. Example:

  MyModel => "my_models"
  Person => "people"
  Project::Info => "project_infos"

The property can be updated at initialization to a custom table name:

  class MyModel
    include Clear::Model

    self.table = "another_table_name"
  end
  MyModel.query.to_sql # SELECT * FROM "another_table_name"

def self.table=(table : Clear::SQL::Symbolic) #

Return the table name setup for this model. By convention, the class name is by default equals to the pluralized underscored string form of the model name. Example:

  MyModel => "my_models"
  Person => "people"
  Project::Info => "project_infos"

The property can be updated at initialization to a custom table name:

  class MyModel
    include Clear::Model

    self.table = "another_table_name"
  end
  MyModel.query.to_sql # SELECT * FROM "another_table_name"

def self.without_releases #

[View source]

Instance Method Detail

def _cached_user : User | Nil #

def archived : Bool #

Returns the value of #archived column or throw an exception if the column is not defined.


def archived=(x : Bool) #

Setter for #archived column.


def archived_column : Clear::Model::Column(Bool, Clear::Model::Converter::BoolConverter) #

Returns the column object used to manage #archived field

See Clear::Model::Column


def attributes : Hash(String, Clear::SQL::Any) #

Attributes, used when fetch_columns is true


def cache : Clear::Model::QueryCache | Nil #

def changed? #

Return true if the model is dirty (e.g. one or more fields have been changed.). Return false otherwise.


def changelog : String | Nil #

Returns the value of #changelog column or throw an exception if the column is not defined.


def changelog=(x : String | Nil) #

Setter for #changelog column.


def changelog_column : Clear::Model::Column(String | Nil, Clear::Model::Converter::StringConverter) #

Returns the column object used to manage #changelog field

See Clear::Model::Column


def clear_change_flags #

Reset the #changed? flag on all columns

The model behave like its not dirty anymore and call to save would apply no changes.

Returns self


def created_at : Time | Nil #

Returns the value of #created_at column or throw an exception if the column is not defined.


def created_at=(x : Time | Nil) #

Setter for #created_at column.


def created_at_column : Clear::Model::Column(Time | Nil, Clear::Model::Converter::TimeConverter) #

Returns the column object used to manage #created_at field

See Clear::Model::Column


def decorate #

[View source]
def dependencies : Repository::Collection #

def dependents : Repository::Collection #

def description : String | Nil #

Returns the value of #description column or throw an exception if the column is not defined.


def description=(x : String | Nil) #

Setter for #description column.


def description_column : Clear::Model::Column(String | Nil, Clear::Model::Converter::StringConverter) #

Returns the column object used to manage #description field

See Clear::Model::Column


def forks_count : Int32 #

Returns the value of #forks_count column or throw an exception if the column is not defined.


def forks_count=(x : Int32) #

Setter for #forks_count column.


def forks_count_column : Clear::Model::Column(Int32, Clear::Model::Converter::Int32Converter) #

Returns the column object used to manage #forks_count field

See Clear::Model::Column


def id : Int64 #

Returns the value of #id column or throw an exception if the column is not defined.


def id=(x : Int64) #

Setter for #id column.


def id_column : Clear::Model::Column(Int64, Clear::Model::Converter::Int64Converter) #

Returns the column object used to manage #id field

See Clear::Model::Column


def ignore : Bool #

Returns the value of #ignore column or throw an exception if the column is not defined.


def ignore=(x : Bool) #

Setter for #ignore column.


def ignore_column : Clear::Model::Column(Bool, Clear::Model::Converter::BoolConverter) #

Returns the column object used to manage #ignore field

See Clear::Model::Column


def last_activity_at : Time #

Returns the value of #last_activity_at column or throw an exception if the column is not defined.


def last_activity_at=(x : Time) #

Setter for #last_activity_at column.


def last_activity_at_column : Clear::Model::Column(Time, Clear::Model::Converter::TimeConverter) #

Returns the column object used to manage #last_activity_at field

See Clear::Model::Column


def license : String | Nil #

Returns the value of #license column or throw an exception if the column is not defined.


def license=(x : String | Nil) #

Setter for #license column.


def license_column : Clear::Model::Column(String | Nil, Clear::Model::Converter::StringConverter) #

Returns the column object used to manage #license field

See Clear::Model::Column


def name : String #

Returns the value of #name column or throw an exception if the column is not defined.


def name=(x : String) #

Setter for #name column.


def name_column : Clear::Model::Column(String, Clear::Model::Converter::StringConverter) #

Returns the column object used to manage #name field

See Clear::Model::Column


def open_issues_count : Int32 | Nil #

Returns the value of #open_issues_count column or throw an exception if the column is not defined.


def open_issues_count=(x : Int32 | Nil) #

Setter for #open_issues_count column.


def open_issues_count_column : Clear::Model::Column(Int32 | Nil, Clear::Model::Converter::Int32Converter) #

Returns the column object used to manage #open_issues_count field

See Clear::Model::Column


def provider : String #

Returns the value of #provider column or throw an exception if the column is not defined.


def provider=(x : String) #

Setter for #provider column.


def provider_column : Clear::Model::Column(String, Clear::Model::Converter::StringConverter) #

Returns the column object used to manage #provider field

See Clear::Model::Column


def provider_id : Int32 #

Returns the value of #provider_id column or throw an exception if the column is not defined.


def provider_id=(x : Int32) #

Setter for #provider_id column.


def provider_id_column : Clear::Model::Column(Int32, Clear::Model::Converter::Int32Converter) #

Returns the column object used to manage #provider_id field

See Clear::Model::Column


def readme : String | Nil #

Returns the value of #readme column or throw an exception if the column is not defined.


def readme=(x : String | Nil) #

Setter for #readme column.


def readme_column : Clear::Model::Column(String | Nil, Clear::Model::Converter::StringConverter) #

Returns the column object used to manage #readme field

See Clear::Model::Column


def relationships : Relationship::Collection #

The method relationships is a has_many relation to Relationship


def releases : Release::Collection #

The method releases is a has_many relation to Release


def reset(h : Hash(Symbol, _)) #

Set the columns from hash


def reset(h : Hash(String, _)) #

Set the model fields from hash


def reset(t : NamedTuple) #

def reset(from_json : JSON::Any) #

def reset(**t : **T) forall T #

reset flavors


def set(h : Hash(Symbol, _)) #

Set the columns from hash


def set(h : Hash(String, _)) #

Set the model fields from hash


def set(t : NamedTuple) #

def set(from_json : JSON::Any) #

def set(**t : **T) forall T #
Description copied from module Clear::Model::HasColumns

Set one or multiple columns to a specific value This two are equivalents:

model.set(a: 1)
model.a = 1

def shard_yml : String | Nil #

Returns the value of #shard_yml column or throw an exception if the column is not defined.


def shard_yml=(x : String | Nil) #

Setter for #shard_yml column.


def shard_yml_column : Clear::Model::Column(String | Nil, Clear::Model::Converter::StringConverter) #

Returns the column object used to manage #shard_yml field

See Clear::Model::Column


def stars_count : Int32 #

Returns the value of #stars_count column or throw an exception if the column is not defined.


def stars_count=(x : Int32) #

Setter for #stars_count column.


def stars_count_column : Clear::Model::Column(Int32, Clear::Model::Converter::Int32Converter) #

Returns the column object used to manage #stars_count field

See Clear::Model::Column


def synced_at : Time #

Returns the value of #synced_at column or throw an exception if the column is not defined.


def synced_at=(x : Time) #

Setter for #synced_at column.


def synced_at_column : Clear::Model::Column(Time, Clear::Model::Converter::TimeConverter) #

Returns the column object used to manage #synced_at field

See Clear::Model::Column


def tag_names #

[View source]
def tags : Tag::Collection #

def tags=(names : Array(String)) #

[View source]
def to_h(full = false) : Hash(String, Clear::SQL::Any) #

Return a hash version of the columns of this model.


def to_json(emit_nulls : Bool = false) #

def to_json(json, emit_nulls = false) #

def touch #

[View source]
def tsv : Clear::TSVector #

Returns the value of #tsv column or throw an exception if the column is not defined.


def tsv=(x : Clear::TSVector) #

Setter for #tsv column.


def tsv_column : Clear::Model::Column(Clear::TSVector, Clear::TSVector::Converter) #

Returns the column object used to manage #tsv field

See Clear::Model::Column


def update_h : Hash(String, Clear::SQL::Any) #

Generate the hash for update request (like during save)


def updated_on : Time | Nil #

Returns the value of #updated_on column or throw an exception if the column is not defined.


def updated_on=(x : Time | Nil) #

Setter for #updated_on column.


def updated_on_column : Clear::Model::Column(Time | Nil, Clear::Model::Converter::TimeConverter) #

Returns the column object used to manage #updated_on field

See Clear::Model::Column


def user : User #

The method user is a belongs_to relation to User


def user=(model : User) #

def user_id : Int64 #

Returns the value of #user_id column or throw an exception if the column is not defined.


def user_id=(x : Int64) #

Setter for #user_id column.


def user_id_column : Clear::Model::Column(Int64, Clear::Model::Converter::Int64Converter) #

Returns the column object used to manage #user_id field

See Clear::Model::Column


def validate_fields_presence #

For each column, ensure than when needed the column has present information into it.

This method is called on validation.