| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package GitInsight::Obj; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 4 |  |  | 4 |  | 18 | use strict; | 
|  | 4 |  |  |  |  | 4 |  | 
|  | 4 |  |  |  |  | 172 |  | 
| 4 | 4 |  |  | 4 |  | 25 | use warnings; | 
|  | 4 |  |  |  |  | 5 |  | 
|  | 4 |  |  |  |  | 152 |  | 
| 5 | 4 |  |  | 4 |  | 2577 | use utf8; | 
|  | 4 |  |  |  |  | 34 |  | 
|  | 4 |  |  |  |  | 15 |  | 
| 6 |  |  |  |  |  |  |  | 
| 7 |  |  |  |  |  |  | #a fork of Mojo::Base | 
| 8 |  |  |  |  |  |  |  | 
| 9 |  |  |  |  |  |  | our $feature = eval { | 
| 10 |  |  |  |  |  |  | require feature; | 
| 11 |  |  |  |  |  |  | feature->import(); | 
| 12 |  |  |  |  |  |  | 1; | 
| 13 |  |  |  |  |  |  | }; | 
| 14 |  |  |  |  |  |  |  | 
| 15 |  |  |  |  |  |  |  | 
| 16 |  |  |  |  |  |  | # Only Perl 5.14+ requires it on demand | 
| 17 | 4 |  |  | 4 |  | 2165 | use IO::Handle (); | 
|  | 4 |  |  |  |  | 20203 |  | 
|  | 4 |  |  |  |  | 475 |  | 
| 18 |  |  |  |  |  |  |  | 
| 19 |  |  |  |  |  |  | # Protect subclasses using AUTOLOAD | 
| 20 | 0 |  |  | 0 |  | 0 | sub DESTROY { } | 
| 21 |  |  |  |  |  |  |  | 
| 22 |  |  |  |  |  |  | sub import { | 
| 23 | 4 |  |  | 4 |  | 4 | my $class = shift; | 
| 24 | 4 | 50 |  |  |  | 14 | return unless my $flag = shift; | 
| 25 |  |  |  |  |  |  |  | 
| 26 |  |  |  |  |  |  | # Base | 
| 27 | 4 | 100 | 0 |  |  | 28 | if ( $flag eq '-base' ) { $flag = $class } | 
|  | 2 | 50 |  |  |  | 12 |  | 
|  |  | 0 |  |  |  |  |  | 
| 28 |  |  |  |  |  |  |  | 
| 29 |  |  |  |  |  |  | # Strict | 
| 30 | 2 |  |  |  |  | 3 | elsif ( $flag eq '-strict' ) { $flag = undef } | 
| 31 |  |  |  |  |  |  |  | 
| 32 |  |  |  |  |  |  | # Module | 
| 33 |  |  |  |  |  |  | elsif ( ( my $file = $flag ) && !$flag->can('new') ) { | 
| 34 | 0 |  |  |  |  | 0 | $file =~ s!::|'!/!g; | 
| 35 | 0 |  |  |  |  | 0 | require "$file.pm"; | 
| 36 |  |  |  |  |  |  | } | 
| 37 |  |  |  |  |  |  |  | 
| 38 |  |  |  |  |  |  | # ISA | 
| 39 | 4 | 100 |  |  |  | 9 | if ($flag) { | 
| 40 | 2 |  |  |  |  | 4 | my $caller = caller; | 
| 41 | 4 |  |  | 4 |  | 21 | no strict 'refs'; | 
|  | 4 |  |  |  |  | 4 |  | 
|  | 4 |  |  |  |  | 1785 |  | 
| 42 | 2 |  |  |  |  | 2 | push @{"${caller}::ISA"}, $flag; | 
|  | 2 |  |  |  |  | 17 |  | 
| 43 | 2 |  |  | 0 |  | 6 | *{"${caller}::has"} = sub { attr( $caller, @_ ) }; | 
|  | 2 |  |  |  |  | 8 |  | 
|  | 0 |  |  |  |  | 0 |  | 
| 44 |  |  |  |  |  |  | } | 
| 45 |  |  |  |  |  |  |  | 
| 46 |  |  |  |  |  |  | # Mojo modules are strict! | 
| 47 | 4 |  |  |  |  | 76 | $_->import for qw(strict warnings utf8); | 
| 48 | 4 | 50 |  |  |  | 108 | if ($feature) { | 
| 49 | 0 |  |  |  |  |  | feature->import(':5.10'); | 
| 50 |  |  |  |  |  |  | } | 
| 51 |  |  |  |  |  |  | } | 
| 52 |  |  |  |  |  |  |  | 
| 53 |  |  |  |  |  |  | sub attr { | 
| 54 | 0 |  |  | 0 | 0 |  | my ( $self, $attrs, $default ) = @_; | 
| 55 | 0 | 0 | 0 |  |  |  | return unless ( my $class = ref $self || $self ) && $attrs; | 
|  |  |  | 0 |  |  |  |  | 
| 56 |  |  |  |  |  |  |  | 
| 57 | 0 | 0 | 0 |  |  |  | die 'Default has to be a code reference or constant value' | 
| 58 |  |  |  |  |  |  | if ref $default && ref $default ne 'CODE'; | 
| 59 |  |  |  |  |  |  |  | 
| 60 | 0 | 0 |  |  |  |  | for my $attr ( @{ ref $attrs eq 'ARRAY' ? $attrs : [$attrs] } ) { | 
|  | 0 |  |  |  |  |  |  | 
| 61 | 0 | 0 |  |  |  |  | die qq{Attribute "$attr" invalid} | 
| 62 |  |  |  |  |  |  | unless $attr =~ /^[a-zA-Z_]\w*$/; | 
| 63 |  |  |  |  |  |  |  | 
| 64 |  |  |  |  |  |  | # Header (check arguments) | 
| 65 | 0 |  |  |  |  |  | my $code = "package $class;\nsub $attr {\n  if (\@_ == 1) {\n"; | 
| 66 |  |  |  |  |  |  |  | 
| 67 |  |  |  |  |  |  | # No default value (return value) | 
| 68 | 0 | 0 |  |  |  |  | unless ( defined $default ) { $code .= "    return \$_[0]{'$attr'};" } | 
|  | 0 |  |  |  |  |  |  | 
| 69 |  |  |  |  |  |  |  | 
| 70 |  |  |  |  |  |  | # Default value | 
| 71 |  |  |  |  |  |  | else { | 
| 72 |  |  |  |  |  |  |  | 
| 73 |  |  |  |  |  |  | # Return value | 
| 74 | 0 |  |  |  |  |  | $code | 
| 75 |  |  |  |  |  |  | .= "    return \$_[0]{'$attr'} if exists \$_[0]{'$attr'};\n"; | 
| 76 |  |  |  |  |  |  |  | 
| 77 |  |  |  |  |  |  | # Return default value | 
| 78 | 0 |  |  |  |  |  | $code .= "    return \$_[0]{'$attr'} = "; | 
| 79 | 0 | 0 |  |  |  |  | $code .= | 
| 80 |  |  |  |  |  |  | ref $default eq 'CODE' | 
| 81 |  |  |  |  |  |  | ? '$default->($_[0]);' | 
| 82 |  |  |  |  |  |  | : '$default;'; | 
| 83 |  |  |  |  |  |  | } | 
| 84 |  |  |  |  |  |  |  | 
| 85 |  |  |  |  |  |  | # Store value | 
| 86 | 0 |  |  |  |  |  | $code .= "\n  }\n  \$_[0]{'$attr'} = \$_[1];\n"; | 
| 87 |  |  |  |  |  |  |  | 
| 88 |  |  |  |  |  |  | # Footer (return invocant) | 
| 89 | 0 |  |  |  |  |  | $code .= "  \$_[0];\n}"; | 
| 90 |  |  |  |  |  |  |  | 
| 91 | 0 | 0 |  |  |  |  | warn "-- Attribute $attr in $class\n$code\n\n" | 
| 92 |  |  |  |  |  |  | if $ENV{GitInsight_OBJ_DEBUG}; | 
| 93 | 0 | 0 |  |  |  |  | die "GitInsight::Obj error: $@" unless eval "$code;1"; | 
| 94 |  |  |  |  |  |  | } | 
| 95 |  |  |  |  |  |  | } | 
| 96 |  |  |  |  |  |  |  | 
| 97 |  |  |  |  |  |  | sub new { | 
| 98 | 0 |  |  | 0 | 0 |  | my $class = shift; | 
| 99 | 0 | 0 | 0 |  |  |  | bless @_ ? @_ > 1 ? {@_} : { %{ $_[0] } } : {}, ref $class || $class; | 
|  | 0 | 0 |  |  |  |  |  | 
| 100 |  |  |  |  |  |  | } | 
| 101 |  |  |  |  |  |  |  | 
| 102 |  |  |  |  |  |  | sub tap { | 
| 103 | 0 |  |  | 0 | 0 |  | my ( $self, $cb ) = @_; | 
| 104 | 0 |  |  |  |  |  | $_->$cb for $self; | 
| 105 | 0 |  |  |  |  |  | return $self; | 
| 106 |  |  |  |  |  |  | } | 
| 107 |  |  |  |  |  |  |  | 
| 108 |  |  |  |  |  |  | 1; |