This repository has been archived by the owner on Jun 21, 2022. It is now read-only.
/
Unit.pm
165 lines (130 loc) Β· 5.01 KB
/
Unit.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package Coocook::Schema::Result::Unit;
use Moose;
use MooseX::MarkAsMethods autoclean => 1;
extends 'Coocook::Schema::Result';
__PACKAGE__->table('units');
__PACKAGE__->add_columns(
id => { data_type => 'integer', is_auto_increment => 1 },
project_id => { data_type => 'integer' },
quantity_id => { data_type => 'integer', is_nullable => 0 },
to_quantity_default => { data_type => 'real', is_nullable => 1 },
space => { data_type => 'boolean' },
short_name => { data_type => 'text' },
long_name => { data_type => 'text' },
);
__PACKAGE__->set_primary_key('id');
__PACKAGE__->add_unique_constraints( [ 'project_id', 'long_name' ] );
__PACKAGE__->belongs_to( project => 'Coocook::Schema::Result::Project', 'project_id' );
__PACKAGE__->belongs_to( quantity => 'Coocook::Schema::Result::Quantity', 'quantity_id' );
# returns other convertible units of same quantity but not $self,
# for doc see https://metacpan.org/pod/DBIx::Class::Relationship::Base#Custom-join-conditions
__PACKAGE__->has_many(
convertible_into => 'Coocook::Schema::Result::Unit',
sub {
my $args = shift;
return {
"$args->{foreign_alias}.id" => { '!=' => { -ident => "$args->{self_alias}.id" } },
"$args->{foreign_alias}.quantity_id" => { -ident => "$args->{self_alias}.quantity_id" },
"$args->{foreign_alias}.to_quantity_default" => { '!=' => undef },
};
}
);
# returns other units of same quantity except $self--regardless if convertible or not
__PACKAGE__->has_many(
other_units_of_same_quantity => 'Coocook::Schema::Result::Unit',
sub {
my $args = shift;
return {
"$args->{foreign_alias}.id" => { '!=' => { -ident => "$args->{self_alias}.id" } },
"$args->{foreign_alias}.quantity_id" => { -ident => "$args->{self_alias}.quantity_id" },
};
}
);
__PACKAGE__->has_many(
articles_units => 'Coocook::Schema::Result::ArticleUnit',
'unit_id',
{
cascade_delete => 0, # units with articles_units may not be deleted
}
);
__PACKAGE__->many_to_many( articles => articles_units => 'article' );
__PACKAGE__->has_many(
dish_ingredients => 'Coocook::Schema::Result::DishIngredient',
'unit_id',
{
cascade_delete => 0, # units with dish_ingredients may not be deleted
}
);
__PACKAGE__->many_to_many( dishes => dish_ingredients => 'dish' );
__PACKAGE__->has_many(
recipe_ingredients => 'Coocook::Schema::Result::RecipeIngredient',
'unit_id',
{
cascade_delete => 0, # units with recipe_ingredients may not be deleted
}
);
__PACKAGE__->many_to_many( recipes => recipe_ingredients => 'recipe' );
__PACKAGE__->has_many(
items => 'Coocook::Schema::Result::Item',
'unit_id',
{
cascade_delete => 0, # units with items may not be deleted
}
);
# before deleting a single unit
# we need to unset default_unit for the quantity if it's the last unit
# because then the quantitiy's default unit cannot be switched to any other unit
before delete => sub {
my $self = shift;
if ( $self->is_quantity_default ) {
$self->other_units_of_same_quantity->results_exist
or $self->quantity->update( { default_unit_id => undef } );
}
};
__PACKAGE__->meta->make_immutable;
sub can_be_quantity_default {
my $self = shift;
$self->quantity_id or return;
$self->get_to_quantity_default or return;
return 1;
}
sub is_quantity_default {
my $self = shift;
return ( $self->id == $self->quantity->default_unit_id );
}
# marks this unit as its quantity's default and adjusts conversion factors of all units
sub make_quantity_default {
my $self = shift;
my $quantity = $self->quantity or die "No quantity";
my $factor = $self->to_quantity_default or die "No to_quantity_default";
my $orig = $quantity->default_unit;
if ( not $orig ) { # no previous default->just select this unit
return $quantity->update( { default_unit_id => $self->id } );
}
$orig->id == $self->id and return 1; # already default
$orig->to_quantity_default == 1
or die "Original default unit needs factor 1";
# collect convertible units of this quantity except $self and $orig
my $others = $quantity->units->search(
{
id => { -not_in => [ $self->id, $orig->id ] },
to_quantity_default => { '!=' => undef }, # IS NOT NULL
}
);
$self->txn_do(
sub {
for my $unit ( $others->all ) {
$unit->update(
{
to_quantity_default => $unit->to_quantity_default / $factor
}
);
}
$orig->update( { to_quantity_default => 1 / $factor } );
$self->update( { to_quantity_default => 1 } );
$quantity->update( { default_unit_id => $self->id } );
}
);
return 1;
}
1;