| 1 | 7 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 2 | 7 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | |||||||||||
| 4 | package XML::Rabbit::Sugar; | ||||||||||
| 5 | 7 | $XML::Rabbit::Sugar::VERSION = '0.4.1'; | |||||||||
| 6 | # ABSTRACT: Sugar functions for easier declaration of xpath attributes | ||||||||||
| 7 | |||||||||||
| 8 | 7 | ▶ | use Scalar::Util qw(blessed); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 7 | ▶ | use Carp qw(confess); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 7 | ▶ | use Moose 0.89 (); # no magic, just load | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 7 | ▶ | use Moose::Exporter; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | |||||||||||
| 14 | 7 | Moose::Exporter->setup_import_methods( | |||||||||
| 15 | with_meta => [qw( | ||||||||||
| 16 | add_xpath_namespace | ||||||||||
| 17 | has_xpath_value | ||||||||||
| 18 | has_xpath_value_list | ||||||||||
| 19 | has_xpath_value_map | ||||||||||
| 20 | has_xpath_object | ||||||||||
| 21 | has_xpath_object_list | ||||||||||
| 22 | has_xpath_object_map | ||||||||||
| 23 | finalize_class | ||||||||||
| 24 | )], | ||||||||||
| 25 | also => 'Moose', | ||||||||||
| 26 | ); | ||||||||||
| 27 | |||||||||||
| 28 | |||||||||||
| 29 | 5 | ▶ | sub add_xpath_namespace { | ||||||||
|
Subroutine
add_xpath_namespace: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 30 | 5 | my ($meta, $namespace, $url) = @_; | |||||||||
| 31 | 5 | my $attr = $meta->find_attribute_by_name('namespace_map'); | |||||||||
| 32 | 5 | ▶ | confess("namespace_map attribute not present") unless blessed($attr); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 33 | 5 | my $default = $attr->default; | |||||||||
| 34 | 5 | ▶ | my $new_default = sub { my $hash = $default->(@_); $hash->{$namespace} = $url; return $hash; }; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 35 | 5 | my $new_attr = $attr->clone_and_inherit_options(default => $new_default); | |||||||||
| 36 | 5 | $meta->add_attribute($new_attr); | |||||||||
| 37 | 5 | return 1; | |||||||||
| 38 | } | ||||||||||
| 39 | |||||||||||
| 40 | |||||||||||
| 41 | 13 | ▶ | sub has_xpath_value { | ||||||||
|
Subroutine
has_xpath_value: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 42 | 13 | my ($meta, $attr_name, $xpath_query, @moose_params) = @_; | |||||||||
| 43 | 13 | $meta->add_attribute($attr_name, | |||||||||
| 44 | is => 'ro', | ||||||||||
| 45 | isa => 'Str', | ||||||||||
| 46 | traits => [qw( XPathValue String )], | ||||||||||
| 47 | xpath_query => $xpath_query, | ||||||||||
| 48 | default => '', | ||||||||||
| 49 | @moose_params, | ||||||||||
| 50 | ); | ||||||||||
| 51 | 13 | return 1; | |||||||||
| 52 | } | ||||||||||
| 53 | |||||||||||
| 54 | |||||||||||
| 55 | 2 | ▶ | sub has_xpath_value_list { | ||||||||
|
Subroutine
has_xpath_value_list: called CC=1 SCAR=0.2
Pod
documented
| |||||||||||
| 56 | 2 | my ($meta, $attr_name, $xpath_query, @moose_params) = @_; | |||||||||
| 57 | $meta->add_attribute($attr_name, | ||||||||||
| 58 | isa => 'ArrayRef[Str]', | ||||||||||
| 59 | traits => [qw( XPathValueList Array )], | ||||||||||
| 60 | xpath_query => $xpath_query, | ||||||||||
| 61 | 0 | ▶ | default => sub { [] }, | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 62 | 2 | @moose_params, | |||||||||
| 63 | ); | ||||||||||
| 64 | 2 | return 1; | |||||||||
| 65 | } | ||||||||||
| 66 | |||||||||||
| 67 | |||||||||||
| 68 | 5 | ▶ | sub has_xpath_value_map { | ||||||||
|
Subroutine
has_xpath_value_map: called CC=1 SCAR=0.2
Pod
documented
| |||||||||||
| 69 | 5 | my ($meta, $attr_name, $xpath_query, $xpath_key, $xpath_value, @moose_params) = @_; | |||||||||
| 70 | $meta->add_attribute($attr_name, | ||||||||||
| 71 | isa => 'HashRef[Str]', | ||||||||||
| 72 | traits => [qw( XPathValueMap Hash )], | ||||||||||
| 73 | xpath_query => $xpath_query, | ||||||||||
| 74 | xpath_key => $xpath_key, | ||||||||||
| 75 | xpath_value => $xpath_value, | ||||||||||
| 76 | 0 | ▶ | default => sub { +{} }, | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 77 | 5 | @moose_params, | |||||||||
| 78 | ); | ||||||||||
| 79 | 5 | return 1; | |||||||||
| 80 | } | ||||||||||
| 81 | |||||||||||
| 82 | |||||||||||
| 83 | |||||||||||
| 84 | 6 | ▶ | sub has_xpath_object { | ||||||||
|
Subroutine
has_xpath_object: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||
| 85 | 6 | my ($meta, $attr_name, $xpath_query, $isa, @moose_params) = @_; | |||||||||
| 86 | 6 | ▶ | my @isa = ref($isa) eq 'HASH' | ||||||||
|
Branch: 1 branch
| |||||||||||
| 87 | ? ( isa_map => $isa ) | ||||||||||
| 88 | : ( isa => $isa ) | ||||||||||
| 89 | ; | ||||||||||
| 90 | 6 | $meta->add_attribute($attr_name, @isa, | |||||||||
| 91 | traits => [qw( XPathObject )], | ||||||||||
| 92 | xpath_query => $xpath_query, | ||||||||||
| 93 | @moose_params, | ||||||||||
| 94 | ); | ||||||||||
| 95 | 6 | return 1; | |||||||||
| 96 | } | ||||||||||
| 97 | |||||||||||
| 98 | |||||||||||
| 99 | |||||||||||
| 100 | 6 | ▶ | sub has_xpath_object_list { | ||||||||
|
Subroutine
has_xpath_object_list: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||
| 101 | 6 | my ($meta, $attr_name, $xpath_query, $isa, @moose_params) = @_; | |||||||||
| 102 | 6 | ▶ | my @isa = ref($isa) eq 'HASH' | ||||||||
|
Branch: 1 branch
| |||||||||||
| 103 | ? ( isa_map => $isa ) | ||||||||||
| 104 | : ( isa => 'ArrayRef[' . $isa . ']' ) | ||||||||||
| 105 | ; | ||||||||||
| 106 | $meta->add_attribute($attr_name, @isa, | ||||||||||
| 107 | traits => [qw( XPathObjectList Array )], | ||||||||||
| 108 | xpath_query => $xpath_query, | ||||||||||
| 109 | 0 | ▶ | default => sub { +[] }, | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 110 | 6 | @moose_params, | |||||||||
| 111 | ); | ||||||||||
| 112 | 6 | return 1; | |||||||||
| 113 | } | ||||||||||
| 114 | |||||||||||
| 115 | |||||||||||
| 116 | |||||||||||
| 117 | 6 | ▶ | sub has_xpath_object_map { | ||||||||
|
Subroutine
has_xpath_object_map: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||
| 118 | 6 | my ($meta, $attr_name, $xpath_query, $xpath_key, $isa, @moose_params) = @_; | |||||||||
| 119 | 6 | ▶ | my @isa = ref($isa) eq 'HASH' | ||||||||
|
Branch: 1 branch
| |||||||||||
| 120 | ? ( isa_map => $isa ) | ||||||||||
| 121 | : ( isa => 'HashRef[' . $isa . ']' ) | ||||||||||
| 122 | ; | ||||||||||
| 123 | $meta->add_attribute($attr_name, @isa, | ||||||||||
| 124 | traits => [qw( XPathObjectMap Hash )], | ||||||||||
| 125 | xpath_query => $xpath_query, | ||||||||||
| 126 | xpath_key => $xpath_key, | ||||||||||
| 127 | 0 | ▶ | default => sub { +{} }, | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 128 | 6 | @moose_params, | |||||||||
| 129 | ); | ||||||||||
| 130 | 6 | return 1; | |||||||||
| 131 | } | ||||||||||
| 132 | |||||||||||
| 133 | |||||||||||
| 134 | 17 | ▶ | sub finalize_class { | ||||||||
|
Subroutine
finalize_class: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 135 | 17 | my ($meta) = @_; | |||||||||
| 136 | 17 | $meta->make_immutable(); | |||||||||
| 137 | 17 | return 1; # so we can avoid the 1; at the end of the file | |||||||||
| 138 | } | ||||||||||
| 139 | |||||||||||
| 140 | 7 | ▶ | no Moose::Exporter; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 141 | |||||||||||
| 142 | 7 | 1; | |||||||||
| 143 | |||||||||||
| 144 | __END__ | ||||||||||