File Coverage

blib/lib/Object/GMP.pm
Criterion Covered Total %
statement 38 46 82.6
branch 9 16 56.2
condition 6 11 54.5
subroutine 7 9 77.7
pod 0 5 0.0
total 60 87 68.9


line stmt bran cond sub pod time code
1             package Object::GMP;
2             $Object::GMP::VERSION = '0.006';
3             =head1 NAME
4              
5             Object::GMP - Moo Role for any object has GMP field
6              
7             =head2 USAGE
8              
9             This module is a moo role
10              
11             =head3 Example
12              
13             package Foo;
14             use Moo;
15             with "Object::GMP";
16             has a => ( is => 'ro' );
17             has b => ( is => 'ro' );
18             has prime => ( is => 'rw' );
19             around BUILDARGS => __PACKAGE__->BUILDARGS_val2gmp('prime');
20             1;
21              
22             The above exmaple to declare the field 'prime' is a GMP value.
23              
24             my $prime =
25             '0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F';
26             my $foo = Foo->new( a => 0, b => 7, prime => $prime );
27             isnt( ref( $foo->a ), undef, 'a is not gmp' );
28             isnt( ref( $foo->b ), undef, 'b is not gmp' );
29             isa_ok( $foo->prime, 'Math::BigInt', 'prime is gmp' );
30              
31             So when you create an object, a and b will be normal value
32             and prime will be a GMP value.
33              
34             =head1 LINKS
35              
36             B: L
37              
38             B: L
39              
40             =cut
41              
42 2     2   11408 use Moo::Role;
  2         17395  
  2         11  
43 2     2   3219 use Math::BigInt lib => 'GMP';
  2         56022  
  2         12  
44             require JSON::XS;
45              
46             my %GMP_FIELDS = ();
47              
48             sub BUILDARGS_val2gmp {
49 1     1 0 23861 my ($class, @fields) = @_;
50              
51 1         3 map { $GMP_FIELDS{$_} = 1 } @fields;
  1         4  
52              
53             return sub {
54 2     2   3932 my ($orig, $class, %args) = @_;
55              
56 2         6 foreach my $key(@fields) {
57 2         9 $args{$key} = $class->val2gmp($args{$key});
58             }
59              
60 2         1373 return $class->$orig(%args);
61             }
62 1         9 }
63              
64             sub val2gmp {
65 2     2 0 5 my ($class, $val) = @_;
66 2 50       7 return $val if !defined $val;
67 2 100       15 return $val if UNIVERSAL::isa($val, 'Math::BigInt');
68 1         6 return Math::BigInt->from_hex($val);
69             }
70              
71             sub copy {
72 1     1 0 1539 my ($self, %args) = @_;
73              
74 1         3 my %new_me = ();
75              
76 1         18 while (my ($key, $val) = each %$self) {
77 3   66     44 $new_me{$key} = $args{$key} // $val;
78              
79 3 100 66     55 if ($GMP_FIELDS{$key} && defined $new_me{$key}) {
80 1         9 $new_me{$key} = $new_me{$key}->copy;
81             }
82             }
83              
84 1         31 return ref($self)->new(%new_me);
85             }
86              
87             sub hashref {
88 3     3 0 354 my ($self, %options) = @_;
89              
90 3         6 my %hash;
91              
92 3   100     6 my %keep = map { $_ => 1 } @{$options{keep} // []};
  1         5  
  3         17  
93              
94 3         14 while (my ($key, $val) = each %$self) {
95 9 100       115 if ($GMP_FIELDS{$key}) {
96 3 100       9 if ($keep{$key}) {
97 1         5 $hash{$key} = "$val";
98             }
99             else {
100 2         9 $hash{$key} = uc $val->as_hex;
101 2         2212 $hash{$key} =~ s/^([\-\+]?0)X/${1}x/;
102             }
103             }
104             else {
105 6         18 $hash{$key} = $val;
106             }
107             }
108              
109 3         44 return \%hash;
110             }
111              
112             sub to_json {
113 0     0 0   my ($self, %options) = @_;
114 0           return JSON::XS->new->pretty->encode($self->hashref(%options));
115             }
116              
117             sub _debug {
118 0     0     my ($self, $pre_note, %options) = @_;
119 0 0         return if !$ENV{DEBUG};
120 0 0 0       return if $ENV{ONLY_SHOW} && $pre_note !~/$ENV{ONLY_SHOW}/;
121 0 0         print "$pre_note: " if $pre_note;
122 0           print $self->to_json(%options);
123 0           print "\n";
124             }
125              
126             1;