| 1 | 2 | ▶ | use utf8; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 2 | |||||||||||
| 3 | package Interchange6::Schema::Result::Inventory; | ||||||||||
| 4 | |||||||||||
| 5 | =head1 NAME | ||||||||||
| 6 | |||||||||||
| 7 | Interchange6::Schema::Result::Inventory | ||||||||||
| 8 | |||||||||||
| 9 | =cut | ||||||||||
| 10 | |||||||||||
| 11 | 2 | ▶ | use Interchange6::Schema::Candy; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | |||||||||||
| 13 | =head1 DESCRIPTION | ||||||||||
| 14 | |||||||||||
| 15 | The Inventory class is used to store current stock levels for products. | ||||||||||
| 16 | |||||||||||
| 17 | =head1 ACCESSORS | ||||||||||
| 18 | |||||||||||
| 19 | =head2 sku | ||||||||||
| 20 | |||||||||||
| 21 | The SKU of the product. | ||||||||||
| 22 | |||||||||||
| 23 | Primary key and foreign constraint on | ||||||||||
| 24 | L<Interchange6::Schema::Result::Product/sku> via L</product> relationship. | ||||||||||
| 25 | |||||||||||
| 26 | =cut | ||||||||||
| 27 | |||||||||||
| 28 | 2 | primary_column sku => | |||||||||
| 29 | { data_type => "varchar", size => 64 }; | ||||||||||
| 30 | |||||||||||
| 31 | =head2 quantity | ||||||||||
| 32 | |||||||||||
| 33 | This is the quantity currently held in stock. | ||||||||||
| 34 | |||||||||||
| 35 | Defaults to 0. | ||||||||||
| 36 | |||||||||||
| 37 | =cut | ||||||||||
| 38 | |||||||||||
| 39 | 2 | column quantity => { data_type => "integer", default_value => 0 }; | |||||||||
| 40 | |||||||||||
| 41 | =head1 RELATIONS | ||||||||||
| 42 | |||||||||||
| 43 | =head2 product | ||||||||||
| 44 | |||||||||||
| 45 | Type: belongs_to | ||||||||||
| 46 | |||||||||||
| 47 | Related object: L<Interchange6::Schema::Result::Product> | ||||||||||
| 48 | |||||||||||
| 49 | =cut | ||||||||||
| 50 | |||||||||||
| 51 | 2 | belongs_to | |||||||||
| 52 | product => "Interchange6::Schema::Result::Product", | ||||||||||
| 53 | "sku", | ||||||||||
| 54 | { is_deferrable => 1, on_delete => "CASCADE", on_update => "CASCADE" }; | ||||||||||
| 55 | |||||||||||
| 56 | =head1 METHODS | ||||||||||
| 57 | |||||||||||
| 58 | |||||||||||
| 59 | =head2 decrement( $quantity ) | ||||||||||
| 60 | |||||||||||
| 61 | Atomically reduce L</quantity> by argument or by 1 if argument is not defined. | ||||||||||
| 62 | Returns new value of L</quantity>. | ||||||||||
| 63 | |||||||||||
| 64 | =cut | ||||||||||
| 65 | |||||||||||
| 66 | sub decrement { | ||||||||||
| 67 | 2 | ▶ | my ( $self, $quantity ) = @_; | ||||||||
|
Subroutine
decrement: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 68 | 2 | ▶ | $quantity = 1 unless defined $quantity; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 69 | 2 | $self->update( { quantity => \[ 'quantity - ?', $quantity ] } ); | |||||||||
| 70 | 2 | $self->discard_changes; | |||||||||
| 71 | 2 | return $self->quantity; | |||||||||
| 72 | } | ||||||||||
| 73 | |||||||||||
| 74 | =head2 increment( $quantity ) | ||||||||||
| 75 | |||||||||||
| 76 | Atomically increase L</quantity> by argument or by 1 if argument is not defined. | ||||||||||
| 77 | Returns new value of L</quantity>. | ||||||||||
| 78 | |||||||||||
| 79 | =cut | ||||||||||
| 80 | |||||||||||
| 81 | sub increment { | ||||||||||
| 82 | 2 | ▶ | my ( $self, $quantity ) = @_; | ||||||||
|
Subroutine
increment: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 83 | 2 | ▶ | $quantity = 1 unless defined $quantity; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 84 | 2 | $self->update( { quantity => \[ 'quantity + ?', $quantity ] } ); | |||||||||
| 85 | 2 | $self->discard_changes; | |||||||||
| 86 | 2 | return $self->quantity; | |||||||||
| 87 | } | ||||||||||
| 88 | |||||||||||
| 89 | 2 | 1; | |||||||||