File Coverage

blib/lib/Treex/Core/WildAttr.pm
Criterion Covered Total %
statement 36 36 100.0
branch 10 10 100.0
condition n/a
subroutine 8 8 100.0
pod 3 3 100.0
total 57 57 100.0


line stmt bran cond sub pod time code
1             package Treex::Core::WildAttr;
2             $Treex::Core::WildAttr::VERSION = '2.20210102';
3 24     24   16833 use Moose::Role;
  24         86  
  24         261  
4              
5 24     24   140600 use Treex::Core::Log;
  24         67  
  24         1714  
6              
7 24     24   187 use Data::Dumper;
  24         71  
  24         11483  
8              
9             has wild => (
10             is => 'rw',
11              
12             # isa => 'HashRef',
13             reader => '_get_wild',
14             writer => 'set_wild',
15             default => sub { return {} },
16             );
17              
18             sub wild {
19 161     161 1 413 my ($self) = @_;
20 161 100       4990 if ( !$self->_get_wild ) {
21 10         288 $self->set_wild( {} );
22             }
23 161         4449 return $self->_get_wild;
24              
25             }
26              
27             sub _wild_dump {
28 89     89   292 my ($self) = @_;
29 89 100       630 if ( $self->isa('Treex::Core::Document') ) {
30 33         297 my $metadata = $self->metaData('pml_root');
31 33         741 my $meta = $metadata->{meta};
32 33         284 return $meta->{wild_dump};
33             }
34             else {
35 56         302 return $self->{wild_dump};
36             }
37             }
38              
39             sub _set_wild_dump {
40 64     64   413 my ( $self, $value ) = @_;
41              
42 64         114 my $storing_hash_ref = $self;
43 64 100       400 if ( $self->isa('Treex::Core::Document') ) {
44 10         68 $storing_hash_ref = $self->metaData('pml_root')->{meta};
45             }
46              
47 64         355 $storing_hash_ref->{wild_dump} = $value;
48 64         125 return;
49             }
50              
51             sub serialize_wild {
52 64     64 1 136 my ($self) = @_;
53 64 100       103 if ( %{ $self->wild } ) {
  64         181  
54 3         12 $self->_set_wild_dump( Dumper( $self->wild ) );
55             }
56             else {
57 61         245 $self->_set_wild_dump(undef);
58             }
59 64         171 return;
60             }
61              
62             sub deserialize_wild {
63 86     86 1 208 my ($self) = @_;
64 86 100       342 if ( $self->_wild_dump ) {
65 3         10 $self->set_wild( eval "my " . $self->_wild_dump . '; return $VAR1' ); ## no critic (ProhibitStringyEval)
66             }
67             else {
68 83         2749 $self->set_wild( {} );
69             }
70 86         225 return;
71             }
72              
73             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.20210102
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.