| 1 | package Treex::Core::RememberArgs; | ||||||||||
| 2 | 1 | $Treex::Core::RememberArgs::VERSION = '2.20151210'; | |||||||||
| 3 | 1 | ▶ | use Moose::Role; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | |||||||||||
| 5 | has args => ( | ||||||||||
| 6 | is => 'ro', | ||||||||||
| 7 | isa => 'HashRef', | ||||||||||
| 8 | writer => '_set_args', | ||||||||||
| 9 | 0 | ▶ | default => sub {return {};}, | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 10 | 1 | ); | |||||||||
| 11 | |||||||||||
| 12 | 1 | has args_str => ( | |||||||||
| 13 | is => 'ro', | ||||||||||
| 14 | isa => 'Str', | ||||||||||
| 15 | writer => '_set_args_str', | ||||||||||
| 16 | default => '', | ||||||||||
| 17 | ); | ||||||||||
| 18 | |||||||||||
| 19 | # Empty BUILD is needed, so we can use "after BUILD". | ||||||||||
| 20 | # "after BUILD" is needed because we cannot (and don't want to) | ||||||||||
| 21 | # override the BUILD which may be present in the class which will consume this role. | ||||||||||
| 22 | # See http://rjbs.manxome.org/rubric/entry/1864 | ||||||||||
| 23 | ▶ | sub BUILD {} | |||||||||
|
Subroutine
BUILD: not called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 24 | after BUILD => sub { | ||||||||||
| 25 | 0 | ▶ | my ($self, $args) = @_; | ||||||||
|
Subroutine
__ANON__: not called CC=7 SCAR=40.3
| |||||||||||
| 26 | 0 | $self->_set_args($args); | |||||||||
| 27 | 0 | my $str = ''; | |||||||||
| 28 | |||||||||||
| 29 | # The method which consumes this role may implement | ||||||||||
| 30 | # its BUILD where a default values of some attributes | ||||||||||
| 31 | # are set. Let's put back these values into $args. | ||||||||||
| 32 | 0 | foreach my $key (keys %{$self}) { | |||||||||
| 33 | 0 | ▶ | next if $key =~ /^args(_str)?$/; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 34 | 0 | my $value = $self->{$key}; | |||||||||
| 35 | 0 | ▶ | next if !defined $value; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 36 | 0 | ▶ | next if ref $value; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 37 | 0 | $args->{$key} = $value; | |||||||||
| 38 | } | ||||||||||
| 39 | |||||||||||
| 40 | # Prepare a single-quoted string representation of all args. | ||||||||||
| 41 | 0 | ▶ | while (my ($key, $value) = each %{$args}){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 42 | 0 | ▶ | if ($value =~ /\s/){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 43 | 0 | $value =~ s/'/\\'/g; | |||||||||
| 44 | 0 | $value = "'$value'"; | |||||||||
| 45 | } | ||||||||||
| 46 | 0 | $str .= "$key=$value "; | |||||||||
| 47 | } | ||||||||||
| 48 | 0 | $str =~ s/ $//; | |||||||||
| 49 | 0 | $self->_set_args_str($str); | |||||||||
| 50 | 0 | return; | |||||||||
| 51 | 1 | }; | |||||||||
| 52 | |||||||||||
| 53 | 1 | 1; | |||||||||
| 54 | |||||||||||
| 55 | __END__ | ||||||||||
| 56 | |||||||||||
| 57 | =encoding utf-8 | ||||||||||
| 58 | |||||||||||
| 59 | =head1 NAME | ||||||||||
| 60 | |||||||||||
| 61 | Treex::Core::RememberArgs - role for remembering constructor's arguments | ||||||||||
| 62 | |||||||||||
| 63 | =head1 VERSION | ||||||||||
| 64 | |||||||||||
| 65 | version 2.20151210 | ||||||||||
| 66 | |||||||||||
| 67 | =head1 SYNOPSIS | ||||||||||
| 68 | |||||||||||
| 69 | package MyClass; | ||||||||||
| 70 | use Moose; | ||||||||||
| 71 | with 'Treex::Core::RememberArgs'; | ||||||||||
| 72 | has num => (is=>'rw'); | ||||||||||
| 73 | 1; | ||||||||||
| 74 | |||||||||||
| 75 | package Main; | ||||||||||
| 76 | use MyClass; | ||||||||||
| 77 | my $object = MyClass->({foo=>'bar baz', num=>42}); | ||||||||||
| 78 | say $object->args_str; | ||||||||||
| 79 | # prints | ||||||||||
| 80 | # foo='bar baz' num=42 | ||||||||||
| 81 | my $foo = $object->args->{foo}; | ||||||||||
| 82 | |||||||||||
| 83 | |||||||||||
| 84 | =head1 DESCRIPTION | ||||||||||
| 85 | |||||||||||
| 86 | Moose role that saves all arguments that were passed to the constructor. | ||||||||||
| 87 | These arguments are then available either as a hashref in attribute C<args> | ||||||||||
| 88 | or as a string in form I<arg1=value1 arg2=val2 ...> in attribute C<args_str>. | ||||||||||
| 89 | Values containing spaces are enclosed in single quotes (and quotes inside the value escaped). | ||||||||||
| 90 | |||||||||||
| 91 | Unlike L<MooseX::SlurpyConstructor>, this role stores also the arguments | ||||||||||
| 92 | which are defined as attributes of the class (C<num> in the Synopsis). | ||||||||||
| 93 | |||||||||||
| 94 | Expected use cases: | ||||||||||
| 95 | Treex scenarios need to serialize all the parameters | ||||||||||
| 96 | and propagate them to nested scenarios. | ||||||||||
| 97 | See e.g. L<Treex::Scen::EN2CS>. | ||||||||||
| 98 | |||||||||||
| 99 | =head1 ATTRIBUTES | ||||||||||
| 100 | |||||||||||
| 101 | =head2 args | ||||||||||
| 102 | |||||||||||
| 103 | hash ref with the original arguments | ||||||||||
| 104 | |||||||||||
| 105 | =head2 args_str | ||||||||||
| 106 | |||||||||||
| 107 | stringified original arguments | ||||||||||
| 108 | |||||||||||
| 109 | =head1 SEE ALSO | ||||||||||
| 110 | |||||||||||
| 111 | L<MooseX::SlurpyConstructor> | ||||||||||
| 112 | |||||||||||
| 113 | L<MooseX::StrictConstructor> | ||||||||||
| 114 | |||||||||||
| 115 | =head1 AUTHOR | ||||||||||
| 116 | |||||||||||
| 117 | Martin Popel <popel@ufal.mff.cuni.cz> | ||||||||||
| 118 | |||||||||||
| 119 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 120 | |||||||||||
| 121 | Copyright © 2015 by Institute of Formal and Applied Linguistics, Charles University in Prague | ||||||||||
| 122 | This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. | ||||||||||