Red.git | docs/coverage/report-data/ | Red-Traits.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>RaCoCo result: Red::Traits</title>
<link rel="stylesheet"
href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"
integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh"
crossorigin="anonymous">
<style>
pre.code::before {
counter-reset: listing;
}
pre.code span {
counter-increment: listing;
}
pre.code span::before {
content: counter(listing) ". ";
}
.coverage-green {
background-color: #32CD32;
/*color-blind
background-color: #00CED1;
/**/
}
.coverage-red {
background-color: #fa8072;
}
.coverage-purple {
background-color: #9370DB;
}
</style>
</head>
<body>
<div class="container">
<div class="row mt-5 mb-3">
<div class="col-12">
<div class="page-header">
<h1>Red::Traits 86.1%</h1>
</div>
</div>
</div>
<div class="row justify-content-md-center">
<div class="col-md-10">
<pre class="code">
<span class="coverage-green">use Red::Column;</span>
<span class="coverage-no">use Red::Attr::Column;</span>
<span class="coverage-no">use Red::ResultSeq;</span>
<span class="coverage-no">use Red::Phaser;</span>
<span class="coverage-green">unit module Red::Traits;</span>
<span class="coverage-no"></span>
<span class="coverage-no">=head2 Red::Traits</span>
<span class="coverage-no"></span>
<span class="coverage-no">=head3 is temp</span>
<span class="coverage-no"></span>
<span class="coverage-no">#| This trait marks the corresponding table of the</span>
<span class="coverage-no">#| model as TEMPORARY (so it only exists for the time</span>
<span class="coverage-no">#| of Red being connected to the database).</span>
<span class="coverage-red">multi trait_mod:<is>(Mu:U $model, Bool :$temp! --> Empty) {</span>
<span class="coverage-red"> $model.^temp = True;</span>
<span class="coverage-no">}</span>
<span class="coverage-no"></span>
<span class="coverage-no">=head3 is rs-class</span>
<span class="coverage-no"></span>
<span class="coverage-no">#| This trait defines the name of the class to be used as</span>
<span class="coverage-no">#| ResultSet to this model.</span>
<span class="coverage-green">multi trait_mod:<is>(Mu:U $model, Str:D :$rs-class! --> Empty) {</span>
<span class="coverage-green"> trait_mod:<is>($model, :rs-class(::($rs-class)))</span>
<span class="coverage-no">}</span>
<span class="coverage-no"></span>
<span class="coverage-no">=head3 is rs-class</span>
<span class="coverage-no"></span>
<span class="coverage-no">#| This trait defines the class to be used as ResultSet to this model.</span>
<span class="coverage-green">multi trait_mod:<is>(Mu:U $model, Mu:U :$rs-class! --> Empty) {</span>
<span class="coverage-green"> die "{$rs-class.^name} should do the Red::ResultSeq role" unless $rs-class ~~ Red::ResultSeq;</span>
<span class="coverage-green"> my role RSClass[Mu:U $rclass] { method rs-class(|) { $rclass<> } }</span>
<span class="coverage-green"> $model.HOW does RSClass[$rs-class];</span>
<span class="coverage-no">}</span>
<span class="coverage-no"></span>
<span class="coverage-no">=head3 is nullable</span>
<span class="coverage-no"></span>
<span class="coverage-no">#| This trait configures all model attributes (columns) to be NULLABLE by default, when used as `is nullable`.</span>
<span class="coverage-no">#| Without this trait applied, default for every attribute (column) is NOT NULL,</span>
<span class="coverage-no">#| though it can be stated explicitly with writing `is !nullable` for the model.</span>
<span class="coverage-no">#| Defaults can be overridden using `is nullable` or `is !nullable` for the attribute (column) itself.</span>
<span class="coverage-green">multi trait_mod:<is>(Mu:U $model, Bool :$nullable! --> Empty) {</span>
<span class="coverage-green"> $model.HOW does role :: { method default-nullable(|) { $nullable } }</span>
<span class="coverage-no">}</span>
<span class="coverage-no"></span>
<span class="coverage-no">=head3 is column</span>
<span class="coverage-no"></span>
<span class="coverage-no">#| Defines the attribute as a column without any custom configuration.</span>
<span class="coverage-green">multi trait_mod:<is>(Attribute $attr, Bool :$column! --> Empty) is export {</span>
<span class="coverage-green"> trait_mod:<is>($attr, :column{}) if $column</span>
<span class="coverage-no">}</span>
<span class="coverage-no"></span>
<span class="coverage-no">=head3 is column</span>
<span class="coverage-no"></span>
<span class="coverage-no">#| Defines the attribute as a column receiving a string to be used as the column name.</span>
<span class="coverage-green">multi trait_mod:<is>(Attribute $attr, Str :$column! --> Empty) is export {</span>
<span class="coverage-green"> trait_mod:<is>($attr, :column{:name($column)}) if $column</span>
<span class="coverage-no">}</span>
<span class="coverage-no"></span>
<span class="coverage-no">=head3 is unique</span>
<span class="coverage-no"></span>
<span class="coverage-no">#| This trait marks an attribute (column) as UNIQUE.</span>
<span class="coverage-green">multi trait_mod:<is>(Attribute $attr, Bool :$unique! where $_ == True --> Empty) is export {</span>
<span class="coverage-green"> trait_mod:<is>($attr, :column{:unique})</span>
<span class="coverage-no">}</span>
<span class="coverage-no"></span>
<span class="coverage-no">=head3 is unique</span>
<span class="coverage-no"></span>
<span class="coverage-no">#| This trait marks an attribute (column) as UNIQUE receiving data to ve used on column definition.</span>
<span class="coverage-green">multi trait_mod:<is>(Attribute $attr, :$unique! --> Empty) is export {</span>
<span class="coverage-green"> trait_mod:<is>($attr, :column{:$unique})</span>
<span class="coverage-no">}</span>
<span class="coverage-no"></span>
<span class="coverage-no">=head3 is id</span>
<span class="coverage-no"></span>
<span class="coverage-no">#| This trait marks an attribute (column) as SQL PRIMARY KEY.</span>
<span class="coverage-green">multi trait_mod:<is>(Attribute $attr, Bool :$id! where $_ == True --> Empty) is export {</span>
<span class="coverage-green"> trait_mod:<is>($attr, :column{:id, :!nullable})</span>
<span class="coverage-no">}</span>
<span class="coverage-no"></span>
<span class="coverage-no">=head3 is serial</span>
<span class="coverage-no"></span>
<span class="coverage-no">#| This trait marks an attribute (column) as SQL PRIMARY KEY with SERIAL data type, which</span>
<span class="coverage-no">#| means it auto-increments on each insertion.</span>
<span class="coverage-green">multi trait_mod:<is>(Attribute $attr, Bool :$serial! where $_ == True --> Empty) is export {</span>
<span class="coverage-green"> trait_mod:<is>($attr, :column{:id, :!nullable, :auto-increment})</span>
<span class="coverage-no">}</span>
<span class="coverage-no"></span>
<span class="coverage-no">=head3 is column</span>
<span class="coverage-no"></span>
<span class="coverage-no">#| A generic trait used for customizing a column. It accepts a hash of Bool keys.</span>
<span class="coverage-no">#| Possible values include:</span>
<span class="coverage-no">#| * id - marks a column PRIMARY KEY</span>
<span class="coverage-no">#| * auto-increment - marks a column AUTO INCREMENT</span>
<span class="coverage-no">#| * nullable - marks a column as NULLABLE</span>
<span class="coverage-no">#| * TBD</span>
<span class="coverage-green">multi trait_mod:<is>(Attribute $attr, :%column! --> Empty) is export {</span>
<span class="coverage-green"> if %column<references>:exists && (%column{<model-name model-type>.none}:exists) {</span>
<span class="coverage-green"> die "On Red:api<2> references must declaire :model-name (or :model-type) and the references block must receive the model as reference"</span>
<span class="coverage-no"> }</span>
<span class="coverage-green"> $attr does Red::Attr::Column(%column);</span>
<span class="coverage-no">}</span>
<span class="coverage-no"></span>
<span class="coverage-red">multi trait_mod:<is>(Attribute $attr, :&referencing! --> Empty) is export {</span>
<span class="coverage-red"> die 'On Red:api<2> ":model" is required on "is referencing"'</span>
<span class="coverage-no">}</span>
<span class="coverage-no"></span>
<span class="coverage-no">=head3 is referencing</span>
<span class="coverage-no"></span>
<span class="coverage-no">#| Trait that defines a reference receiving a code block, a model name, optional require string and nullable.</span>
<span class="coverage-green">multi trait_mod:<is>(Attribute $attr, :$referencing! (&referencing!, Str :$model!, Str :$require = $model, Bool :$nullable = True ) --> Empty) is export {</span>
<span class="coverage-green"> trait_mod:<is>($attr, :column{ :$nullable, :references(&referencing), model-name => $model, :$require })</span>
<span class="coverage-no">}</span>
<span class="coverage-no"></span>
<span class="coverage-no">=head3 is referencing</span>
<span class="coverage-no"></span>
<span class="coverage-no">#| Trait that defines a reference receiving a model name, a column name, and optional require string and nulabble.</span>
<span class="coverage-green">multi trait_mod:<is>(Attribute $attr, :$referencing! (Str :$model!, Str :$column!, Str :$require = $model, Bool :$nullable = True ) --> Empty) is export {</span>
<span class="coverage-green"> trait_mod:<is>($attr, :column{ :$nullable, model-name => $model, column-name => $column, :$require })</span>
<span class="coverage-no">}</span>
<span class="coverage-no"></span>
<span class="coverage-no"></span>
<span class="coverage-no">=head3 is referencing</span>
<span class="coverage-no"></span>
<span class="coverage-no">#| Trait that defines a reference receiving a code block, a model type object and an optional nullable.</span>
<span class="coverage-green">multi trait_mod:<is>(Attribute $attr, :$referencing! (&referencing!, Mu:U :$model!, Bool :$nullable = True ) --> Empty) is export {</span>
<span class="coverage-green"> $model.^add_role: Red::Model;</span>
<span class="coverage-green"> trait_mod:<is>($attr, :column{ :$nullable, :references(&referencing), model-type => $model })</span>
<span class="coverage-no">}</span>
<span class="coverage-no"></span>
<span class="coverage-no">=head3 is referencing</span>
<span class="coverage-no"></span>
<span class="coverage-no">#| Trait that defines a reference receiving a model type object, a column name, and optional nulabble.</span>
<span class="coverage-red">multi trait_mod:<is>(Attribute $attr, :$referencing! (Mu:U :$model!, Str :$column!, Bool :$nullable = True ) --> Empty) is export {</span>
<span class="coverage-red"> trait_mod:<is>($attr, :column{ :$nullable, model-type => $model, column-name => $column })</span>
<span class="coverage-no">}</span>
<span class="coverage-no"></span>
<span class="coverage-no">=head3 is table</span>
<span class="coverage-no"></span>
<span class="coverage-no">#| This trait allows setting a custom name for a table corresponding to a model.</span>
<span class="coverage-no">#| For example, `model MyModel is table<custom_table_name> {}` will use `custom_table_name`</span>
<span class="coverage-no">#| as the name of the underlying database table.</span>
<span class="coverage-green">multi trait_mod:<is>(Mu:U $model, Str :$table! is copy where .chars > 0 --> Empty) {</span>
<span class="coverage-green"> $model.HOW.^attributes.first({ .name eq '$!table' }).set_value($model.HOW, $table)</span>
<span class="coverage-no">}</span>
<span class="coverage-no"></span>
<span class="coverage-no">=head3 is relationship</span>
<span class="coverage-no"></span>
<span class="coverage-no">#| Trait that defines a relationship receiving a code block.</span>
<span class="coverage-green">multi trait_mod:<is>(Attribute $attr, :&relationship! --> Empty) is export {</span>
<span class="coverage-green"> $attr.package.^add-relationship: $attr, &relationship</span>
<span class="coverage-no">}</span>
<span class="coverage-no"></span>
<span class="coverage-no">=head3 is relationship</span>
<span class="coverage-no"></span>
<span class="coverage-no">#| DEPRECATED: Trait that defines a relationship receiving 2 code blocks.</span>
<span class="coverage-green">multi trait_mod:<is>(Attribute $attr, :@relationship! where { .all ~~ Callable and .elems == 2 } --> Empty) is DEPRECATED is export {</span>
<span class="coverage-red"> $attr.package.^add-relationship: $attr, |@relationship</span>
<span class="coverage-no">}</span>
<span class="coverage-no"></span>
<span class="coverage-no">=head3 is relationship</span>
<span class="coverage-no"></span>
<span class="coverage-no">#| Trait that defines a relationship receiving a code block, a model name, and opitionaly require string, optional, no-prefetch and has-one.</span>
<span class="coverage-green">multi trait_mod:<is>(Attribute $attr, :$relationship! (&relationship, Str :$model, Str :$require = $model, Bool :$optional, Bool :$no-prefetch, Bool :$has-one) --> Empty) is export {</span>
<span class="coverage-green"> die "Please, use the has-one experimental feature (use Red <has-one>) to allow using it on relationships"</span>
<span class="coverage-no"> if $has-one && !%Red::experimentals<has-one>;</span>
<span class="coverage-green"> $attr.package.^add-relationship: $attr, &relationship, |(:$model with $model), |(:$require with $require), :$optional, :$no-prefetch, |(:$has-one if $has-one)</span>
<span class="coverage-no">}</span>
<span class="coverage-no"></span>
<span class="coverage-no">=head3 is relationship</span>
<span class="coverage-no"></span>
<span class="coverage-no">#| Trait that defines a relationship receiving a code block, a model type object, and opitionaly require string, optional, no-prefetch and has-one.</span>
<span class="coverage-green">multi trait_mod:<is>(Attribute $attr, :$relationship! (&relationship, Mu:U :$model!, Bool :$optional, Bool :$no-prefetch, Bool :$has-one) --> Empty) is export {</span>
<span class="coverage-green"> die "Please, use the has-one experimental feature (use Red <has-one>) to allow using it on relationships"</span>
<span class="coverage-no"> if $has-one && !%Red::experimentals<has-one>;</span>
<span class="coverage-green"> $attr.package.^add-relationship: $attr, &relationship, :model-type($model), :$optional, :$no-prefetch, |(:$has-one if $has-one)</span>
<span class="coverage-no">}</span>
<span class="coverage-no"></span>
<span class="coverage-no">=head3 is relationship</span>
<span class="coverage-no"></span>
<span class="coverage-no">#| Trait that defines a relationship receiving a column name, a model name and opitionaly a require, optional, no-prefetch and has-one.</span>
<span class="coverage-green">multi trait_mod:<is>(Attribute $attr, :$relationship! (Str :$column!, Str :$model!, Str :$require = $model, Bool :$optional, Bool :$no-prefetch, Bool :$has-one) --> Empty) is export {</span>
<span class="coverage-green"> die "Please, use the has-one experimental feature (use Red <has-one>) to allow using it on relationships"</span>
<span class="coverage-no"> if $has-one && !%Red::experimentals<has-one>;</span>
<span class="coverage-green"> $attr.package.^add-relationship: $attr, :$column, :$model, :$require, :$optional, :$no-prefetch, |(:$has-one if $has-one)</span>
<span class="coverage-no">}</span>
<span class="coverage-no"></span>
<span class="coverage-red">multi trait_mod:<is>(Attribute $attr, Callable :$relationship! ( @relationship! where *.elems == 2, Str :$model!, Str :$require = $model, Bool :$optional, Bool :$no-prefetch, Bool :$has-one) --> Empty) {</span>
<span class="coverage-red"> die "Please, use the has-one experimental feature (use Red <has-one>) to allow using it on relationships"</span>
<span class="coverage-no"> if $has-one && !%Red::experimentals<has-one>;</span>
<span class="coverage-red"> $attr.package.^add-relationship: $attr, |@relationship, :$model, :$require, :$optional, :$no-prefetch, |(:$has-one if $has-one)</span>
<span class="coverage-no">}</span>
<span class="coverage-no"></span>
<span class="coverage-no">=head3 is before-create</span>
<span class="coverage-no"></span>
<span class="coverage-no">#| Trait to define a phaser to run before create a new record</span>
<span class="coverage-green">multi trait_mod:<is>(Method $m, :$before-create! --> Empty) {</span>
<span class="coverage-green"> $m does BeforeCreate;</span>
<span class="coverage-no">}</span>
<span class="coverage-no"></span>
<span class="coverage-no">=head3 is after-create</span>
<span class="coverage-no"></span>
<span class="coverage-no">#| Trait to define a phaser to run after create a new record</span>
<span class="coverage-green">multi trait_mod:<is>(Method $m, :$after-create! --> Empty) {</span>
<span class="coverage-green"> $m does AfterCreate;</span>
<span class="coverage-no">}</span>
<span class="coverage-no"></span>
<span class="coverage-no">=head3 is before-update</span>
<span class="coverage-no"></span>
<span class="coverage-no">#| Trait to define a phaser to run before update a record</span>
<span class="coverage-green">multi trait_mod:<is>(Method $m, :$before-update! --> Empty) {</span>
<span class="coverage-green"> $m does BeforeUpdate;</span>
<span class="coverage-no">}</span>
<span class="coverage-no"></span>
<span class="coverage-no">=head3 is after-update</span>
<span class="coverage-no"></span>
<span class="coverage-no">#| Trait to define a phaser to run after update record</span>
<span class="coverage-green">multi trait_mod:<is>(Method $m, :$after-update! --> Empty) {</span>
<span class="coverage-green"> $m does AfterUpdate;</span>
<span class="coverage-no">}</span>
<span class="coverage-no"></span>
<span class="coverage-no">=head3 is before-delete</span>
<span class="coverage-no"></span>
<span class="coverage-no">#| Trait to define a phaser to run before delete a record</span>
<span class="coverage-green">multi trait_mod:<is>(Method $m, :$before-delete! --> Empty) {</span>
<span class="coverage-green"> $m does BeforeDelete;</span>
<span class="coverage-no">}</span>
<span class="coverage-no"></span>
<span class="coverage-no">=head3 is after-delete</span>
<span class="coverage-no"></span>
<span class="coverage-no">#| Trait to define a phaser to run after delete a record</span>
<span class="coverage-green">multi trait_mod:<is>(Method $m, :$after-delete! --> Empty) {</span>
<span class="coverage-green"> $m does AfterDelete;</span>
<span class="coverage-no">}</span>
<span class="coverage-no"></span>
<span class="coverage-no">=head is sub-module</span>
<span class="coverage-no"></span>
<span class="coverage-no">#| Trait to transform subset into sub-model</span>
<span class="coverage-green">multi trait_mod:<is>($subset where { .HOW ~~ Metamodel::SubsetHOW }, Bool :$sub-model) {</span>
<span class="coverage-no"> use MetamodelX::Red::SubModelHOW;</span>
<span class="coverage-green"> $subset.HOW does MetamodelX::Red::SubModelHOW;</span>
<span class="coverage-green"> $subset</span>
<span class="coverage-no">}</span>
</pre>
</div>
</div>
</div>
</body>
</html>