| 1 | # -*- perl -*- | ||||||||||
| 2 | ##---------------------------------------------------------------------------- | ||||||||||
| 3 | ## Telegram API - ~/lib/Net/API/Telegram/Number.pm | ||||||||||
| 4 | ## Version 0.1 | ||||||||||
| 5 | ## Copyright(c) 2019 Jacques Deguest | ||||||||||
| 6 | ## Author: Jacques Deguest <jack@deguest.jp> | ||||||||||
| 7 | ## Created 2019/06/02 | ||||||||||
| 8 | ## Modified 2019/06/02 | ||||||||||
| 9 | ## All rights reserved | ||||||||||
| 10 | ## | ||||||||||
| 11 | ## This program is free software; you can redistribute it and/or modify it | ||||||||||
| 12 | ## under the same terms as Perl itself. | ||||||||||
| 13 | ##---------------------------------------------------------------------------- | ||||||||||
| 14 | package Net::API::Telegram::Number; | ||||||||||
| 15 | BEGIN | ||||||||||
| 16 | { | ||||||||||
| 17 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 18 | 1 | ▶ | use parent qw( Module::Generic ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 19 | 1 | ▶ | use Number::Format; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 20 | 1 | ▶ | our( $VERSION ) = '0.1'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 21 | }; | ||||||||||
| 22 | |||||||||||
| 23 | use overload ( | ||||||||||
| 24 | '""' => sub | ||||||||||
| 25 | { | ||||||||||
| 26 | 0 | ▶ | my $self = shift( @_ ); | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 27 | 0 | return( $self->{ '_number' } ); | |||||||||
| 28 | }, | ||||||||||
| 29 | '-' => sub | ||||||||||
| 30 | { | ||||||||||
| 31 | 0 | ▶ | my( $self, $other, $swap ) = @_; | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||
| 32 | 0 | my $result = $self->{ '_number' } - $other; | |||||||||
| 33 | 0 | ▶ | $result = -$result if( $swap ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 34 | 0 | return( $result ); | |||||||||
| 35 | }, | ||||||||||
| 36 | '+' => sub | ||||||||||
| 37 | { | ||||||||||
| 38 | 0 | ▶ | my( $self, $other, $swap ) = @_; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 39 | 0 | my $result = $self->{ '_number' } + $other; | |||||||||
| 40 | 0 | return( $result ); | |||||||||
| 41 | }, | ||||||||||
| 42 | '*' => sub | ||||||||||
| 43 | { | ||||||||||
| 44 | 0 | ▶ | my( $self, $other, $swap ) = @_; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 45 | 0 | my $result = $self->{ '_number' } * $other; | |||||||||
| 46 | 0 | return( $result ); | |||||||||
| 47 | }, | ||||||||||
| 48 | '/' => sub | ||||||||||
| 49 | { | ||||||||||
| 50 | 0 | ▶ | my( $self, $other, $swap ) = @_; | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||
| 51 | 0 | ▶ | if( $swap ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 52 | { | ||||||||||
| 53 | 0 | return( $other / $self->{ '_number' } ); | |||||||||
| 54 | } | ||||||||||
| 55 | else | ||||||||||
| 56 | { | ||||||||||
| 57 | 0 | return( $self->{ '_number' } / $other ); | |||||||||
| 58 | } | ||||||||||
| 59 | }, | ||||||||||
| 60 | '<' => sub | ||||||||||
| 61 | { | ||||||||||
| 62 | 0 | ▶ | my( $self, $other, $swap ) = @_; | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||
| 63 | 0 | ▶ | return( $swap ? $other < $self->{ '_number' } : $self->{ '_number' } < $other ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 64 | }, | ||||||||||
| 65 | '<=' => sub | ||||||||||
| 66 | { | ||||||||||
| 67 | 0 | ▶ | my( $self, $other, $swap ) = @_; | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||
| 68 | 0 | ▶ | return( $swap ? $other <= $self->{ '_number' } : $self->{ '_number' } <= $other ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 69 | }, | ||||||||||
| 70 | '>' => sub | ||||||||||
| 71 | { | ||||||||||
| 72 | 0 | ▶ | my( $self, $other, $swap ) = @_; | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||
| 73 | 0 | ▶ | return( $swap ? $other > $self->{ '_number' } : $self->{ '_number' } > $other ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 74 | }, | ||||||||||
| 75 | '>=' => sub | ||||||||||
| 76 | { | ||||||||||
| 77 | 0 | ▶ | my( $self, $other, $swap ) = @_; | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||
| 78 | 0 | ▶ | return( $swap ? $other >= $self->{ '_number' } : $self->{ '_number' } >= $other ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 79 | }, | ||||||||||
| 80 | '<=>' => sub | ||||||||||
| 81 | { | ||||||||||
| 82 | 0 | ▶ | my( $self, $other, $swap ) = @_; | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||
| 83 | 0 | ▶ | return( $swap ? $other <=> $self->{ '_number' } : $self->{ '_number' } <=> $other ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 84 | }, | ||||||||||
| 85 | '==' => sub | ||||||||||
| 86 | { | ||||||||||
| 87 | 0 | ▶ | my( $self, $other, $swap ) = @_; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 88 | 0 | return( $self->{ '_number' } == $other ); | |||||||||
| 89 | }, | ||||||||||
| 90 | '!=' => sub | ||||||||||
| 91 | { | ||||||||||
| 92 | 0 | ▶ | my( $self, $other, $swap ) = @_; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 93 | 0 | return( $self->{ '_number' } == $other ); | |||||||||
| 94 | }, | ||||||||||
| 95 | 'eq' => sub | ||||||||||
| 96 | { | ||||||||||
| 97 | 0 | ▶ | my( $self, $other, $swap ) = @_; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 98 | 0 | return( $self->{ '_number' } eq $other ); | |||||||||
| 99 | }, | ||||||||||
| 100 | 'ne' => sub | ||||||||||
| 101 | { | ||||||||||
| 102 | 0 | ▶ | my( $self, $other, $swap ) = @_; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 103 | 0 | return( $self->{ '_number' } ne $other ); | |||||||||
| 104 | } | ||||||||||
| 105 | 1 | ▶ | ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 106 | |||||||||||
| 107 | sub init | ||||||||||
| 108 | { | ||||||||||
| 109 | 0 | ▶ | my $self = shift( @_ ); | ||||||||
|
Subroutine
init: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 110 | 0 | my $num = shift( @_ ); | |||||||||
| 111 | 0 | $self->SUPER::init; | |||||||||
| 112 | 0 | $self->{ '_fmt' } = Number::Format->new( | |||||||||
| 113 | -thousands_sep => ',', | ||||||||||
| 114 | -decimal_point => '.', | ||||||||||
| 115 | -int_curr_symbol => '¥', | ||||||||||
| 116 | ); | ||||||||||
| 117 | 0 | $self->{ '_number' } = $num; | |||||||||
| 118 | 0 | return( $self ); | |||||||||
| 119 | } | ||||||||||
| 120 | |||||||||||
| 121 | 0 | ▶ | sub as_string { return( shift->{ '_number' } ) } | ||||||||
|
Subroutine
as_string: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 122 | |||||||||||
| 123 | sub format | ||||||||||
| 124 | { | ||||||||||
| 125 | 0 | ▶ | my $self = shift( @_ ); | ||||||||
|
Subroutine
format: not called CC=2 SCAR=12.2
Pod
documented
| |||||||||||
| 126 | 1 | ▶ | no overloading; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 127 | 0 | my $num = $self->{ '_number' }; | |||||||||
| 128 | ## If value provided was undefined, we leave it undefined, otherwise we would be at risk of returning 0, and 0 is very different from undefined | ||||||||||
| 129 | 0 | ▶ | return( $num ) if( !defined( $num ) ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 130 | 0 | my $fmt = $self->{ '_fmt' }; | |||||||||
| 131 | 0 | return( $fmt->format_number( $num, @_ ) ); | |||||||||
| 132 | } | ||||||||||
| 133 | |||||||||||
| 134 | sub format_money | ||||||||||
| 135 | { | ||||||||||
| 136 | 0 | ▶ | my $self = shift( @_ ); | ||||||||
|
Subroutine
format_money: not called CC=2 SCAR=12.2
Pod
undocumented
| |||||||||||
| 137 | 1 | ▶ | no overloading; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 138 | 0 | my $num = $self->{ '_number' }; | |||||||||
| 139 | ## See comment in format() method | ||||||||||
| 140 | 0 | ▶ | return( $num ) if( !defined( $num ) ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 141 | 0 | my $fmt = $self->{ '_fmt' }; | |||||||||
| 142 | 0 | return( $fmt->format_price( $num, @_ ) ); | |||||||||
| 143 | } | ||||||||||
| 144 | |||||||||||
| 145 | 1 | 1; | |||||||||
| 146 | |||||||||||
| 147 | __END__ | ||||||||||
| 148 | |||||||||||