• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

lib/H03-May-2022-655142

t/H16-Oct-2020-2,2521,402

ChangesH A D16-Oct-20201.3 KiB3626

LICENSEH A D16-Oct-202018 KiB380292

MANIFESTH A D16-Oct-2020781 3534

META.jsonH A D16-Oct-20201.5 KiB6058

META.ymlH A D16-Oct-2020782 3029

Makefile.PLH A D16-Oct-20201.4 KiB6149

README.mkdnH A D16-Oct-20203.8 KiB11482

TODOH A D16-Oct-2020287 75

dist.iniH A D16-Oct-2020923 4840

README.mkdn

1# NAME
2
3MooX::HandlesVia - NativeTrait-like behavior for Moo.
4
5# VERSION
6
7version 0.001009
8
9# SYNOPSIS
10
11    {
12      package Hashy;
13      use Moo;
14      use MooX::HandlesVia;
15
16      has hash => (
17        is => 'rw',
18        handles_via => 'Hash',
19        handles => {
20          get_val => 'get',
21          set_val => 'set',
22          all_keys => 'keys'
23        }
24      );
25    }
26
27    my $h = Hashy->new(hash => { a => 1, b => 2});
28
29    $h->get_val('b'); # 2
30
31    $h->set_val('a', 'BAR'); # sets a to BAR
32
33    my @keys = $h->all_keys; # returns a, b
34
35# DESCRIPTION
36
37MooX::HandlesVia is an extension of Moo's 'handles' attribute functionality. It
38provides a means of proxying functionality from an external class to the given
39atttribute. This is most commonly used as a way to emulate 'Native Trait'
40behavior that has become commonplace in Moose code, for which there was no Moo
41alternative.
42
43# SHORTCOMINGS
44
45Due to current Moo implementation details there are some deficiencies in how
46MooX::HandlesVia in comparison to what you would expect from Moose native
47traits.
48
49- methods delegated via the Moo 'handles' interface are passed the
50attribue value directly. and there is no way to access the parent class. This
51means if an attribute is updated any triggers or type coercions **WILL NOT**
52fire.
53- Moo attribute method delegations are passed the attribute value. This
54is fine for references (objects, arrays, hashrefs..) it means simple scalar
55types are **READ ONLY**. This unfortunately means Number, String, Counter, Bool
56cannot modify the attributes value, rendering them largely useless.
57
58If these are issues for you, consider [Sub::HandlesVia](https://metacpan.org/pod/Sub::HandlesVia), which uses a
59different architecture, respecting triggers and coercions, and allowing
60read-write access to non-reference values. It should be possible to use
61Sub::HandlesVia as a drop-in replacement for MooX::HandlesVia.
62
63# PROVIDED INTERFACE/FUNCTIONS
64
65- **process\_has(@\_)**
66
67    MooX::HandlesVia preprocesses arguments passed to has() attribute declarations
68    via the process\_has function. In a given Moo class, If 'handles\_via' is set to
69    a ClassName string, and 'handles' is set with a hashref mapping of desired moo
70    class methods that should map to ClassName methods, process\_has() will create
71    the appropriate binding to create the mapping IF ClassName provides that named
72    method.
73
74        has options => (
75          is => 'rw',
76          handles_via => 'Array',
77          handles => {
78            mixup => 'shuffle',
79            unique_options => 'uniq',
80            all_options => 'elements'
81          }
82        );
83
84The following handles\_via keywords are reserved as shorthand for mapping to
85[Data::Perl](https://metacpan.org/pod/Data::Perl):
86
87- **Hash** maps to [Data::Perl::Collection::Hash::MooseLike](https://metacpan.org/pod/Data::Perl::Collection::Hash::MooseLike)
88- **Array** maps to [Data::Perl::Collection::Array::MooseLike](https://metacpan.org/pod/Data::Perl::Collection::Array::MooseLike)
89- **String** maps to [Data::Perl::String::MooseLike](https://metacpan.org/pod/Data::Perl::String::MooseLike)
90- **Number** maps to [Data::Perl::Number::MooseLike](https://metacpan.org/pod/Data::Perl::Number::MooseLike)
91- **Bool** maps to [Data::Perl::Bool::MooseLike](https://metacpan.org/pod/Data::Perl::Bool::MooseLike)
92- **Code** maps to [Data::Perl::Code](https://metacpan.org/pod/Data::Perl::Code)
93
94# SEE ALSO
95
96- [Moo](https://metacpan.org/pod/Moo)
97- [MooX::late](https://metacpan.org/pod/MooX::late)
98- [Sub::HandlesVia](https://metacpan.org/pod/Sub::HandlesVia)
99
100# ORIGINAL AUTHOR
101
102Matthew Phillips <mattp@cpan.org>
103
104# AUTHOR
105
106Toby Inkster <tobyink@cpan.org>
107
108# COPYRIGHT AND LICENSE
109
110This software is copyright (c) 2020 by Matthew Phillips <mattp@cpan.org>.
111
112This is free software; you can redistribute it and/or modify it under
113the same terms as the Perl 5 programming language system itself.
114