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

..03-May-2022-

inc/H03-May-2012-5,7154,082

lib/MouseX/H03-May-2012-1,385506

t/H03-May-2012-2,3851,950

xt/H03-May-2012-3833

ChangesH A D03-May-2012668 2820

MANIFESTH A D03-May-20122.5 KiB105104

META.ymlH A D03-May-2012728 3332

Makefile.PLH A D03-May-2012917 3830

READMEH A D03-May-20123.1 KiB11383

README.podH A D03-May-20124.4 KiB176117

README

1NAME
2    MouseX::AttributeHelpers - Extend your attribute interfaces
3
4SYNOPSIS
5        package MyClass;
6
7        use Mouse;
8        use MouseX::AttributeHelpers;
9
10        has 'mapping' => (
11            metaclass => 'Collection::Hash',
12            is        => 'rw',
13            isa       => 'HashRef',
14            default   => sub { +{} },
15            provides  => {
16                exists => 'exists_in_mapping',
17                keys   => 'ids_in_mapping',
18                get    => 'get_mapping',
19                set    => 'set_mapping',
20            },
21        );
22
23        package main;
24
25        my $obj = MyClass->new;
26        $obj->set_quantity(10);      # quantity => 10
27        $obj->set_mapping(4, 'foo'); # 4 => 'foo'
28        $obj->set_mapping(5, 'bar'); # 5 => 'bar'
29        $obj->set_mapping(6, 'baz'); # 6 => 'baz'
30
31        # prints 'bar'
32        print $obj->get_mapping(5) if $obj->exists_in_mapping(5);
33
34        # prints '4, 5, 6'
35        print join ', ', $obj->ids_in_mapping;
36
37DESCRIPTION
38    MouseX::AttributeHelpers provides commonly used attribute helper methods
39    for more specific types of data.
40
41    As seen in the "SYNOPSIS", you specify the extension via the "metaclass"
42    parameter.
43
44PARAMETERS
45  provides
46    This points to a hashref that uses "provider" for the keys and "method"
47    for the values. The method will be added to the object itself and do
48    what you want.
49
50  curries
51    This points to a hashref that uses "provider" for the keys and has two
52    choices for the value:
53
54    You can supply "{ method => \@args }" for the values. The method will be
55    added to the object itself (always using @args as the beginning
56    arguments).
57
58    Another approach to curry a method provider is to supply a coderef
59    instead of an arrayref. The code ref takes $self, $body, and any
60    additional arguments passed to the final method.
61
62METHOD PROVIDERS
63  Counter
64    Methods for incrementing and decrementing a counter attribute.
65
66  Number
67    Common numerical operations.
68
69  String
70    Common methods for string values.
71
72  Bool
73    Common methods for boolean values.
74
75  Collection::List
76    Common list methods for array references.
77
78  Collection::Array
79    Common methods for array references.
80
81  Collection::ImmutableHash
82    Common methods for hash references.
83
84  Collection::Hash
85    Common additional methods for hash references.
86
87  Collection::Bag
88    Methods for incrementing and decrementing a value of collection.
89
90AUTHOR
91    NAKAGAWA Masaki <masaki@cpan.org>
92
93THANKS TO
94    "AUTHOR" in MooseX::AttributeHelpers
95
96LICENSE
97    This library is free software; you can redistribute it and/or modify it
98    under the same terms as Perl itself.
99
100SEE ALSO
101    Mouse
102
103    MouseX::AttributeHelpers::Counter, MouseX::AttributeHelpers::Number,
104    MouseX::AttributeHelpers::String, MouseX::AttributeHelpers::Bool,
105    MouseX::AttributeHelpers::Collection::List,
106    MouseX::AttributeHelpers::Collection::Array,
107    MouseX::AttributeHelpers::Collection::ImmutableHash,
108    MouseX::AttributeHelpers::Collection::Hash,
109    MouseX::AttributeHelpers::Collection::Bag
110
111    MooseX::AttributeHelpers
112
113

README.pod

1package MouseX::AttributeHelpers;
2
3use 5.006_002;
4our $VERSION = '0.07';
5
6use Mouse::Util qw(load_class);
7
8# These submodules are automatically loaded by register_implementation()
9#use MouseX::AttributeHelpers::Counter;
10#use MouseX::AttributeHelpers::Number;
11#use MouseX::AttributeHelpers::String;
12#use MouseX::AttributeHelpers::Bool;
13#use MouseX::AttributeHelpers::Collection::List;
14#use MouseX::AttributeHelpers::Collection::Array;
15#use MouseX::AttributeHelpers::Collection::ImmutableHash;
16#use MouseX::AttributeHelpers::Collection::Hash;
17#use MouseX::AttributeHelpers::Collection::Bag;
18
19# aliases
20
21foreach my $helper(qw(
22    Bool Counter Number String
23    Collection::List Collection::Array Collection::Bag
24    Collection::Hash Collection::ImmutableHash
25)){
26    my $from = 'MouseX::AttributeHelpers::' . $helper;
27    my $to   = 'Mouse::Meta::Attribute::Custom::'      . $helper;
28
29    my $alias = sub {
30        load_class($from);
31        return $from;
32    };
33    no strict 'refs';
34    *{$to . '::register_implementation'} = $alias;
35}
36
37
381;
39__END__
40
41=head1 NAME
42
43MouseX::AttributeHelpers - Extend your attribute interfaces
44
45=head1 SYNOPSIS
46
47    package MyClass;
48
49    use Mouse;
50    use MouseX::AttributeHelpers;
51
52    has 'mapping' => (
53        metaclass => 'Collection::Hash',
54        is        => 'rw',
55        isa       => 'HashRef',
56        default   => sub { +{} },
57        provides  => {
58            exists => 'exists_in_mapping',
59            keys   => 'ids_in_mapping',
60            get    => 'get_mapping',
61            set    => 'set_mapping',
62        },
63    );
64
65    package main;
66
67    my $obj = MyClass->new;
68    $obj->set_quantity(10);      # quantity => 10
69    $obj->set_mapping(4, 'foo'); # 4 => 'foo'
70    $obj->set_mapping(5, 'bar'); # 5 => 'bar'
71    $obj->set_mapping(6, 'baz'); # 6 => 'baz'
72
73    # prints 'bar'
74    print $obj->get_mapping(5) if $obj->exists_in_mapping(5);
75
76    # prints '4, 5, 6'
77    print join ', ', $obj->ids_in_mapping;
78
79=head1 DESCRIPTION
80
81MouseX::AttributeHelpers provides commonly used attribute helper
82methods for more specific types of data.
83
84As seen in the L</SYNOPSIS>, you specify the extension via the
85C<metaclass> parameter.
86
87=head1 PARAMETERS
88
89=head2 provides
90
91This points to a hashref that uses C<provider> for the keys and
92C<method> for the values. The method will be added to the object
93itself and do what you want.
94
95=head2 curries
96
97This points to a hashref that uses C<provider> for the keys and
98has two choices for the value:
99
100You can supply C<< { method => \@args } >> for the values.
101The method will be added to the object itself (always using C<@args>
102as the beginning arguments).
103
104Another approach to curry a method provider is to supply a coderef
105instead of an arrayref. The code ref takes C<$self>, C<$body>,
106and any additional arguments passed to the final method.
107
108=head1 METHOD PROVIDERS
109
110=head2 L<Counter|MouseX::AttributeHelpers::Counter>
111
112Methods for incrementing and decrementing a counter attribute.
113
114=head2 L<Number|MouseX::AttributeHelpers::Number>
115
116Common numerical operations.
117
118=head2 L<String|MouseX::AttributeHelpers::String>
119
120Common methods for string values.
121
122=head2 L<Bool|MouseX::AttributeHelpers::Bool>
123
124Common methods for boolean values.
125
126=head2 L<Collection::List|MouseX::AttributeHelpers::Collection::List>
127
128Common list methods for array references.
129
130=head2 L<Collection::Array|MouseX::AttributeHelpers::Collection::Array>
131
132Common methods for array references.
133
134=head2 L<Collection::ImmutableHash|MouseX::AttributeHelpers::Collection::ImmutableHash>
135
136Common methods for hash references.
137
138=head2 L<Collection::Hash|MouseX::AttributeHelpers::Collection::Hash>
139
140Common additional methods for hash references.
141
142=head2 L<Collection::Bag|MouseX::AttributeHelpers::Collection::Bag>
143
144Methods for incrementing and decrementing a value of collection.
145
146=head1 AUTHOR
147
148NAKAGAWA Masaki E<lt>masaki@cpan.orgE<gt>
149
150=head1 THANKS TO
151
152L<MooseX::AttributeHelpers/AUTHOR>
153
154=head1 LICENSE
155
156This library is free software; you can redistribute it and/or modify
157it under the same terms as Perl itself.
158
159=head1 SEE ALSO
160
161L<Mouse>
162
163L<MouseX::AttributeHelpers::Counter>,
164L<MouseX::AttributeHelpers::Number>,
165L<MouseX::AttributeHelpers::String>,
166L<MouseX::AttributeHelpers::Bool>,
167L<MouseX::AttributeHelpers::Collection::List>,
168L<MouseX::AttributeHelpers::Collection::Array>,
169L<MouseX::AttributeHelpers::Collection::ImmutableHash>,
170L<MouseX::AttributeHelpers::Collection::Hash>,
171L<MouseX::AttributeHelpers::Collection::Bag>
172
173L<MooseX::AttributeHelpers>
174
175=cut
176