File: README.mkdn

package info (click to toggle)
libmoox-handlesvia-perl 0.001005-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 248 kB
  • ctags: 8
  • sloc: perl: 1,087; makefile: 2
file content (103 lines) | stat: -rw-r--r-- 3,485 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# NAME

MooX::HandlesVia - NativeTrait-like behavior for Moo.

# VERSION

version 0.001005

# SYNOPSIS

    {
      package Hashy;
      use Moo;
      use MooX::HandlesVia;

      has hash => (
        is => 'rw',
        handles_via => 'Hash',
        handles => {
          get_val => 'get',
          set_val => 'set',
          all_keys => 'keys'
        }
      );
    }

    my $h = Hashy->new(hash => { a => 1, b => 2});

    $h->get_val('b'); # 2

    $h->set_val('a', 'BAR'); # sets a to BAR

    my @keys = $h->all_keys; # returns a, b

# DESCRIPTION

MooX::HandlesVia is an extension of Moo's 'handles' attribute functionality. It
provides a means of proxying functionality from an external class to the given
atttribute. This is most commonly used as a way to emulate 'Native Trait'
behavior that has become commonplace in Moose code, for which there was no Moo
alternative.

# SHORTCOMINGS

Due to current Moo implementation details there are some deficiencies in how
MooX::HandlesVia in comparison to what you would expect from Moose native
traits.

- methods delegated via the Moo 'handles' interface are passed the
attribue value directly. and there is no way to access the parent class. This
means if an attribute is updated any triggers or type coercions __WILL NOT__
fire.
- Moo attribute method delegations are passed the attribute value. This
is fine for references (objects, arrays, hashrefs..) it means simple scalar
types are __READ ONLY__. This unfortunately means Number, String, Counter, Bool
cannot modify the attributes value, rendering them largely useless.

# PROVIDED INTERFACE/FUNCTIONS

- __process\_has(@\_)__

    MooX::HandlesVia preprocesses arguments passed to has() attribute declarations
    via the process\_has function. In a given Moo class, If 'handles\_via' is set to
    a ClassName string, and 'handles' is set with a hashref mapping of desired moo
    class methods that should map to ClassName methods, process\_has() will create
    the appropriate binding to create the mapping IF ClassName provides that named
    method.

        has options => (
          is => 'rw',
          handles_via => 'Array',
          handles => {
            mixup => 'shuffle',
            unique_options => 'uniq',
            all_options => 'elements'
          }
        );

The following handles\_via keywords are reserved as shorthand for mapping to
[Data::Perl](https://metacpan.org/pod/Data::Perl):

- __Hash__ maps to [Data::Perl::Collection::Hash::MooseLike](https://metacpan.org/pod/Data::Perl::Collection::Hash::MooseLike)
- __Array__ maps to [Data::Perl::Collection::Array::MooseLike](https://metacpan.org/pod/Data::Perl::Collection::Array::MooseLike)
- __String__ maps to [Data::Perl::String::MooseLike](https://metacpan.org/pod/Data::Perl::String::MooseLike)
- __Number__ maps to [Data::Perl::Number::MooseLike](https://metacpan.org/pod/Data::Perl::Number::MooseLike)
- __Bool__ maps to [Data::Perl::Bool::MooseLike](https://metacpan.org/pod/Data::Perl::Bool::MooseLike)
- __Code__ maps to [Data::Perl::Code](https://metacpan.org/pod/Data::Perl::Code)

# SEE ALSO

- [Moo](https://metacpan.org/pod/Moo)
- [MooX::late](https://metacpan.org/pod/MooX::late)

# AUTHOR

Matthew Phillips <mattp@cpan.org>

# COPYRIGHT AND LICENSE

This software is copyright (c) 2013 by Matthew Phillips <mattp@cpan.org>.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.