package MooX::HandlesVia;
# ABSTRACT: NativeTrait-like behavior for Moo.
$MooX::HandlesVia::VERSION = '0.001009';
use strict;
use warnings;

use Moo ();
use Moo::Role ();
use Module::Runtime qw/require_module/;

# reserved hardcoded mappings for classname shortcuts.
my %RESERVED = (
  'Array' => 'Data::Perl::Collection::Array::MooseLike',
  'Hash' => 'Data::Perl::Collection::Hash::MooseLike',
  'String' => 'Data::Perl::String::MooseLike',
  'Bool' => 'Data::Perl::Bool::MooseLike',
  'Number' => 'Data::Perl::Number::MooseLike',
  'Code' => 'Data::Perl::Code',
my %REVERSED = reverse %RESERVED;

sub import {
  my ($class) = @_;

  no strict 'refs';
  no warnings 'redefine';

  my $target = caller;
  if (my $has = $target->can('has')) {
    my $newsub = sub {

    if (Moo::Role->is_role($target)) {
      Moo::Role::_install_tracked($target, "has", $newsub);
    else {
      Moo::_install_tracked($target, "has", $newsub);

sub process_has {
  my ($name, %opts) = @_;
  my $handles = $opts{handles};
  return ($name, %opts) if not $handles or ref $handles ne 'HASH';

  if (my $via = delete $opts{handles_via}) {
    $via = ref $via eq 'ARRAY' ? $via->[0] : $via;

    # try to load the reserved mapping, if it exists, else the full name
    $via = $RESERVED{$via} || $via;

    # clone handles for HandlesMoose support
    my %handles_clone = %$handles;

    while (my ($target, $delegation) = each %$handles) {
      # if passed an array, handle the curry
      if (ref $delegation eq 'ARRAY') {
        my ($method, @curry) = @$delegation;
        if ($via->can($method)) {
          $handles->{$target} = ['${\\'.$via.'->can("'.$method.'")}', @curry];
      elsif (ref $delegation eq '') {
        if ($via->can($delegation)) {
          $handles->{$target} = '${\\'.$via.'->can("'.$delegation.'")}';

    # install our support for moose upgrading of class/role
    # we deleted the handles_via key above, but install it as a native trait
    my $inflator = $opts{moosify};
    $opts{moosify} = sub {
      my ($spec) = @_;

      $spec->{handles} = \%handles_clone;
      $spec->{traits} = [$REVERSED{$via} || $via];

      # pass through if needed
      $inflator->($spec) if ref($inflator) eq 'CODE';

  ($name, %opts);



=encoding UTF-8

=head1 NAME

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

=head1 VERSION

version 0.001009


    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


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


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

=over 4

=item * 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 B<WILL NOT>

=item * Moo attribute method delegations are passed the attribute value. This
is fine for references (objects, arrays, hashrefs..) it means simple scalar
types are B<READ ONLY>. This unfortunately means Number, String, Counter, Bool
cannot modify the attributes value, rendering them largely useless.


If these are issues for you, consider L<Sub::HandlesVia>, which uses a
different architecture, respecting triggers and coercions, and allowing
read-write access to non-reference values. It should be possible to use
Sub::HandlesVia as a drop-in replacement for MooX::HandlesVia.


=over 4

=item B<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

  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

=over 4

=item * B<Hash> maps to L<Data::Perl::Collection::Hash::MooseLike>

=item * B<Array> maps to L<Data::Perl::Collection::Array::MooseLike>

=item * B<String> maps to L<Data::Perl::String::MooseLike>

=item * B<Number> maps to L<Data::Perl::Number::MooseLike>

=item * B<Bool> maps to L<Data::Perl::Bool::MooseLike>

=item * B<Code> maps to L<Data::Perl::Code>


=head1 SEE ALSO

=over 4

=item * L<Moo>

=item * L<MooX::late>

=item * L<Sub::HandlesVia>



Matthew Phillips <>

=head1 AUTHOR

Toby Inkster <>


This software is copyright (c) 2020 by Matthew Phillips <>.

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