| 1 | package Treex::Core::WildAttr; | ||||||||||
| 2 | 50 | $Treex::Core::WildAttr::VERSION = '2.20151210'; | |||||||||
| 3 | 50 | ▶ | use Moose::Role; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | |||||||||||
| 5 | 50 | ▶ | use Treex::Core::Log; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 50 | ▶ | use Data::Dumper; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | has wild => ( | ||||||||||
| 10 | is => 'rw', | ||||||||||
| 11 | |||||||||||
| 12 | # isa => 'HashRef', | ||||||||||
| 13 | reader => '_get_wild', | ||||||||||
| 14 | writer => 'set_wild', | ||||||||||
| 15 | 291 | ▶ | default => sub { return {} }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 16 | 50 | ); | |||||||||
| 17 | |||||||||||
| 18 | sub wild { | ||||||||||
| 19 | 77 | ▶ | my ($self) = @_; | ||||||||
|
Subroutine
wild: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 20 | 77 | ▶ | if ( !$self->_get_wild ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 21 | 10 | $self->set_wild( {} ); | |||||||||
| 22 | } | ||||||||||
| 23 | 77 | return $self->_get_wild; | |||||||||
| 24 | |||||||||||
| 25 | } | ||||||||||
| 26 | |||||||||||
| 27 | sub _wild_dump { | ||||||||||
| 28 | 89 | ▶ | my ($self) = @_; | ||||||||
|
Subroutine
_wild_dump: called CC=2 SCAR=6.9
| |||||||||||
| 29 | 89 | ▶ | if ( $self->isa('Treex::Core::Document') ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 30 | 33 | my $metadata = $self->metaData('pml_root'); | |||||||||
| 31 | 33 | my $meta = $metadata->{meta}; | |||||||||
| 32 | 33 | return $meta->{wild_dump}; | |||||||||
| 33 | } | ||||||||||
| 34 | else { | ||||||||||
| 35 | 56 | return $self->{wild_dump}; | |||||||||
| 36 | } | ||||||||||
| 37 | } | ||||||||||
| 38 | |||||||||||
| 39 | sub _set_wild_dump { | ||||||||||
| 40 | 64 | ▶ | my ( $self, $value ) = @_; | ||||||||
|
Subroutine
_set_wild_dump: called CC=2 SCAR=6.9
| |||||||||||
| 41 | |||||||||||
| 42 | 64 | my $storing_hash_ref = $self; | |||||||||
| 43 | 64 | ▶ | if ( $self->isa('Treex::Core::Document') ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 44 | 10 | $storing_hash_ref = $self->metaData('pml_root')->{meta}; | |||||||||
| 45 | } | ||||||||||
| 46 | |||||||||||
| 47 | 64 | $storing_hash_ref->{wild_dump} = $value; | |||||||||
| 48 | 64 | return; | |||||||||
| 49 | } | ||||||||||
| 50 | |||||||||||
| 51 | sub serialize_wild { | ||||||||||
| 52 | 64 | ▶ | my ($self) = @_; | ||||||||
|
Subroutine
serialize_wild: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 53 | 64 | ▶ | if ( %{ $self->wild } ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 54 | 3 | $self->_set_wild_dump( Dumper( $self->wild ) ); | |||||||||
| 55 | } | ||||||||||
| 56 | else { | ||||||||||
| 57 | 61 | $self->_set_wild_dump(undef); | |||||||||
| 58 | } | ||||||||||
| 59 | 64 | return; | |||||||||
| 60 | } | ||||||||||
| 61 | |||||||||||
| 62 | sub deserialize_wild { | ||||||||||
| 63 | 86 | ▶ | my ($self) = @_; | ||||||||
|
Subroutine
deserialize_wild: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 64 | 86 | ▶ | if ( $self->_wild_dump ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 65 | 3 | $self->set_wild( eval "my " . $self->_wild_dump . '; return $VAR1' ); ## no critic (ProhibitStringyEval) | |||||||||
| 66 | } | ||||||||||
| 67 | else { | ||||||||||
| 68 | 83 | $self->set_wild( {} ); | |||||||||
| 69 | } | ||||||||||
| 70 | 86 | return; | |||||||||
| 71 | } | ||||||||||
| 72 | |||||||||||
| 73 | 50 | 1; | |||||||||
| 74 | |||||||||||
| 75 | __END__ | ||||||||||
| 76 | |||||||||||
| 77 | =encoding utf-8 | ||||||||||
| 78 | |||||||||||
| 79 | =head1 NAME | ||||||||||
| 80 | |||||||||||
| 81 | Treex::Core::WildAttr - role for arbitrary attributes of Treex objects | ||||||||||
| 82 | |||||||||||
| 83 | =head1 VERSION | ||||||||||
| 84 | |||||||||||
| 85 | version 2.20151210 | ||||||||||
| 86 | |||||||||||
| 87 | =for test_synopsis my ($node, $value); | ||||||||||
| 88 | =head1 SYNOPSIS | ||||||||||
| 89 | |||||||||||
| 90 | $node->wild->{name_of_my_new_attribute} = $value; | ||||||||||
| 91 | $value = $node->wild->{name_of_my_new_attribute}; | ||||||||||
| 92 | |||||||||||
| 93 | =head1 DESCRIPTION | ||||||||||
| 94 | |||||||||||
| 95 | Moose role for Treex objects that can possess any attributes | ||||||||||
| 96 | without defining them in the PML schema. Such 'wild' | ||||||||||
| 97 | attributes are stored in trees data files as strings | ||||||||||
| 98 | serialized by Data::Dumper. | ||||||||||
| 99 | |||||||||||
| 100 | |||||||||||
| 101 | Expected use cases: you need to store some data structures which are not defined | ||||||||||
| 102 | by the Treex PML schema because | ||||||||||
| 103 | (1) you do not want to change the schema | ||||||||||
| 104 | (e.g. the new attributes are still very unstable, or they are likely to serve only | ||||||||||
| 105 | for tentative purposes, or you do not feel competent to touch the PML schema), or | ||||||||||
| 106 | (2) you cannot change the schema, because you do not have write permissions for the | ||||||||||
| 107 | location in which L<Treex::Core> is installed. | ||||||||||
| 108 | |||||||||||
| 109 | =head1 ATTRIBUTES | ||||||||||
| 110 | |||||||||||
| 111 | =over | ||||||||||
| 112 | |||||||||||
| 113 | =item wild | ||||||||||
| 114 | |||||||||||
| 115 | Reference to a hash for storing wild attributes. | ||||||||||
| 116 | The attributes are to be accessed as follows: | ||||||||||
| 117 | |||||||||||
| 118 | $object->wild->{$wild_attr_name} = $wild_attr_value; | ||||||||||
| 119 | |||||||||||
| 120 | =item wild_dump | ||||||||||
| 121 | |||||||||||
| 122 | PML-standard attribute which stores stringified | ||||||||||
| 123 | content of the attribute C<wild>. C<wild> and C<wild_dump> | ||||||||||
| 124 | are synchronized by methods C<serialize_wild> and | ||||||||||
| 125 | C<deserialize_wild>; C<wild_dump> should not be | ||||||||||
| 126 | accessed otherwise. | ||||||||||
| 127 | |||||||||||
| 128 | =back | ||||||||||
| 129 | |||||||||||
| 130 | =head1 METHODS | ||||||||||
| 131 | |||||||||||
| 132 | =over | ||||||||||
| 133 | |||||||||||
| 134 | =item serialize_wild(); | ||||||||||
| 135 | |||||||||||
| 136 | Stores the content of the C<wild> hash into the C<wild_dump> string. | ||||||||||
| 137 | |||||||||||
| 138 | =item deserialize_wild(); | ||||||||||
| 139 | |||||||||||
| 140 | Loads the content from the C<wild_dump> string into the C<wild> hash. | ||||||||||
| 141 | |||||||||||
| 142 | =back | ||||||||||
| 143 | |||||||||||
| 144 | |||||||||||
| 145 | =head1 AUTHOR | ||||||||||
| 146 | |||||||||||
| 147 | Zdeněk Žabokrtský <zabokrtsky@ufal.mff.cuni.cz> | ||||||||||
| 148 | |||||||||||
| 149 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 150 | |||||||||||
| 151 | Copyright © 2011 by Institute of Formal and Applied Linguistics, Charles University in Prague | ||||||||||
| 152 | |||||||||||
| 153 | This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. | ||||||||||