MooseX-Param-0.02/0000755000076500007650000000000010730004030014522 5ustar stevanstevan00000000000000MooseX-Param-0.02/Build.PL0000444000076500007650000000077310730004030016023 0ustar stevanstevan00000000000000use Module::Build; use strict; my $build = Module::Build->new( module_name => 'MooseX::Param', license => 'perl', requires => { 'Moose' => '0.32', }, optional => { }, build_requires => { 'Test::More' => '0.62', 'Test::Exception' => '0.21', }, create_makefile_pl => 'traditional', recursive_test_files => 1, add_to_cleanup => [ 'META.yml', '*.bak', '*.gz', 'Makefile.PL', ], ); $build->create_build_script; MooseX-Param-0.02/ChangeLog0000444000076500007650000000041410730004030016271 0ustar stevanstevan00000000000000Revision history for Perl extension MooseX-Param 0.02 Wed. Dec. 12, 2007 - added init_params builder method - added tests for this 0.01 Tues. March 13, 2007 - I got tired of writing this code over and over and over, so I made it a role :)MooseX-Param-0.02/lib/0000755000076500007650000000000010730004030015270 5ustar stevanstevan00000000000000MooseX-Param-0.02/lib/MooseX/0000755000076500007650000000000010730004030016502 5ustar stevanstevan00000000000000MooseX-Param-0.02/lib/MooseX/Param.pm0000444000076500007650000000741110730004030020101 0ustar stevanstevan00000000000000 package MooseX::Param; use Moose::Role; our $VERSION = '0.02'; our $AUTHORITY = 'cpan:STEVAN'; has 'params' => ( is => 'rw', isa => 'HashRef', lazy => 1, builder => 'init_params', ); sub init_params { +{} } sub param { my $self = shift; # if they want the list of keys ... return keys %{$self->params} if scalar @_ == 0; # if they want to fetch a particular key ... return $self->params->{$_[0]} if scalar @_ == 1; ((scalar @_ % 2) == 0) || confess "parameter assignment must be an even numbered list"; my %new = @_; while (my ($key, $value) = each %new) { $self->params->{$key} = $value; } return; } 1; __END__ =pod =head1 NAME MooseX::Param - Simple role to provide a standard param method =head1 SYNOPSIS package My::Template::System; use Moose; with 'MooseX::Param'; # ... my $template = My::Template::System->new( params => { foo => 10, bar => 20, baz => 30, } ); # fetching params $template->param('foo'); # 10 # getting list of params $template->param(); # foo, bar, baz # setting params $template->param(foo => 30, bar => 100); =head1 DESCRIPTION This is a very simple Moose role which provides a L like C method. I found that I had written this code over and over and over and over again, and each time it was the same. So I thought, why not put it in a role? =head1 ATTRIBUTES =over 4 =item I This role provides a C attribute which has a read-only accessor, and a HashRef type constraint. It also adds a builder method (see C method below) to properly initalize it. =back =head1 METHODS =over 4 =item B Return the HASH ref in which the parameters are stored. =item B This is your standard L style C method. If passed no arguments, it will return a list of param names. If passed a single name argument it will return the param associated with that name. If passed a key value pair (or set of key value pairs) it will assign them into the params. =item I This is the I attribute C option, so it is called the params are initialized. B You can override this by defining your own version in your class, because local class methods beat role methods in composition. =item B Returns the role metaclass. =back =head1 SIMILAR MODULES The C method can be found in several other modules, such as L, L and L to name a few. This is such a common Perl idiom that I felt it really deserved it's own role (if for nothing more than I was sick of re-writing and copy-pasting it all the time). There are also a few modules which attempt to solve the same problem as this module. Those are: =over 4 =item L This module is much more ambitious than mine, and provides much deeper functionality. For most of my purposes, this module would have been overkill, but if you need really sophisticated param handling and the ability to provide several different APIs (tied, etc), this module is probably the way to go. =item L This module is very similar to mine, but for a different framework. It works with the L framework. =back =head1 BUGS All complex software has bugs lurking in it, and this module is no exception. If you find a bug please either email me, or add the bug to cpan-RT. =head1 AUTHOR Stevan Little Estevan@iinteractive.comE =head1 COPYRIGHT AND LICENSE Copyright 2007 by Infinity Interactive, Inc. L This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =cutMooseX-Param-0.02/Makefile.PL0000444000076500007650000000100210730004030016463 0ustar stevanstevan00000000000000# Note: this file was auto-generated by Module::Build::Compat version 0.03 use ExtUtils::MakeMaker; WriteMakefile ( 'NAME' => 'MooseX::Param', 'VERSION_FROM' => 'lib/MooseX/Param.pm', 'PREREQ_PM' => { 'Moose' => '0.32', 'Test::Exception' => '0.21', 'Test::More' => '0.62' }, 'INSTALLDIRS' => 'site', 'EXE_FILES' => [], 'PL_FILES' => {} ) ; MooseX-Param-0.02/MANIFEST0000444000076500007650000000024710730004030015654 0ustar stevanstevan00000000000000Build.PL ChangeLog Makefile.PL META.yml MANIFEST MANIFEST.SKIP README lib/MooseX/Param.pm t/000_load.t t/001_basic.t t/002_with_init_params.t t/pod.t t/pod_coverage.t MooseX-Param-0.02/MANIFEST.SKIP0000444000076500007650000000024510730004030016417 0ustar stevanstevan00000000000000^_build ^Build$ ^blib ~$ \.bak$ ^MANIFEST\.SKIP$ CVS \.svn \.DS_Store cover_db \..*\.sw.?$ ^Makefile$ ^pm_to_blib$ ^MakeMaker-\d ^blibdirs$ \.old$ ^#.*#$ ^\.# ^TODO$MooseX-Param-0.02/META.yml0000444000076500007650000000100010730004030015760 0ustar stevanstevan00000000000000--- name: MooseX-Param version: 0.02 author: - 'Stevan Little Estevan@iinteractive.comE' abstract: Simple role to provide a standard param method license: perl resources: license: http://dev.perl.org/licenses/ requires: Moose: 0.32 build_requires: Test::Exception: 0.21 Test::More: 0.62 provides: MooseX::Param: file: lib/MooseX/Param.pm version: 0.02 generated_by: Module::Build version 0.2808 meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.2.html version: 1.2 MooseX-Param-0.02/README0000444000076500007650000000101610730004030015376 0ustar stevanstevan00000000000000MooseX::Param version 0.02 =========================== See the individual module documentation for more information INSTALLATION To install this module type the following: perl Makefile.PL make make test make install DEPENDENCIES This module requires these other modules and libraries: Moose COPYRIGHT AND LICENCE Copyright (C) 2007 Infinity Interactive, Inc. http://www.iinteractive.com This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. MooseX-Param-0.02/t/0000755000076500007650000000000010730004030014765 5ustar stevanstevan00000000000000MooseX-Param-0.02/t/000_load.t0000444000076500007650000000015710730004030016451 0ustar stevanstevan00000000000000#!/usr/bin/perl use strict; use warnings; use Test::More tests => 1; BEGIN { use_ok('MooseX::Param'); } MooseX-Param-0.02/t/001_basic.t0000444000076500007650000000241310730004030016611 0ustar stevanstevan00000000000000#!/usr/bin/perl use strict; use warnings; use Test::More tests => 18; use Test::Exception; BEGIN { use_ok('MooseX::Param'); } { package My::Request; use Moose; with 'MooseX::Param'; } my $r = My::Request->new; isa_ok($r, 'My::Request'); ok($r->does('MooseX::Param'), '... this does the MooseX::Param role'); is_deeply( {}, $r->params, '... no params yet'); is_deeply( [], [ $r->param ], '... no param keys'); ok(!defined($r->param('foo')), '... no foo param'); lives_ok { $r->param(foo => 10); } '... set the foo param ok'; is_deeply( { foo => 10 }, $r->params, '... one param now'); is_deeply( [ 'foo' ], [ $r->param ], '... one param key'); is($r->param('foo'), 10, '... we have a foo param'); lives_ok { $r->param(bar => 20, baz => 30); } '... set the bar and baz param ok'; is_deeply( { foo => 10, bar => 20, baz => 30 }, $r->params, '... many params now'); is_deeply( [ 'bar', 'baz', 'foo' ], [ sort $r->param ], '... 3 param keys'); is($r->param('foo'), 10, '... we have a foo param (still)'); is($r->param('bar'), 20, '... we have a bar param'); is($r->param('baz'), 30, '... we have a baz param'); lives_ok { $r->param(foo => undef); } '... unset the foo param ok'; ok(!defined($r->param('foo')), '... no more foo param'); MooseX-Param-0.02/t/002_with_init_params.t0000444000076500007650000000274210730004030021077 0ustar stevanstevan00000000000000#!/usr/bin/perl use strict; use warnings; use Test::More tests => 19; use Test::Exception; BEGIN { use_ok('MooseX::Param'); } { package My::Request; use Moose; with 'MooseX::Param'; sub init_params { +{ hello => 'world' } } } my $r = My::Request->new; isa_ok($r, 'My::Request'); ok($r->does('MooseX::Param'), '... this does the MooseX::Param role'); is_deeply( { hello => 'world' }, $r->params, '... some params yet'); is_deeply( [ 'hello' ], [ $r->param ], '... some param keys'); ok(defined($r->param('hello')), '... have hello param'); ok(!defined($r->param('foo')), '... no foo param'); lives_ok { $r->param(foo => 10); } '... set the foo param ok'; is_deeply( { foo => 10, hello => 'world' }, $r->params, '... one param now'); is_deeply( [ sort 'foo', 'hello' ], [ sort $r->param ], '... one param key'); is($r->param('foo'), 10, '... we have a foo param'); lives_ok { $r->param(bar => 20, baz => 30); } '... set the bar and baz param ok'; is_deeply( { foo => 10, bar => 20, baz => 30, hello => 'world' }, $r->params, '... many params now'); is_deeply( [ sort 'bar', 'baz', 'foo', 'hello' ], [ sort $r->param ], '... 3 param keys'); is($r->param('foo'), 10, '... we have a foo param (still)'); is($r->param('bar'), 20, '... we have a bar param'); is($r->param('baz'), 30, '... we have a baz param'); lives_ok { $r->param(foo => undef); } '... unset the foo param ok'; ok(!defined($r->param('foo')), '... no more foo param'); MooseX-Param-0.02/t/pod.t0000444000076500007650000000025710730004030015736 0ustar stevanstevan00000000000000#!/usr/bin/perl use strict; use warnings; use Test::More; eval "use Test::Pod 1.14"; plan skip_all => "Test::Pod 1.14 required for testing POD" if $@; all_pod_files_ok(); MooseX-Param-0.02/t/pod_coverage.t0000444000076500007650000000031710730004030017606 0ustar stevanstevan00000000000000#!/usr/bin/perl use strict; use warnings; use Test::More; eval "use Test::Pod::Coverage 1.04"; plan skip_all => "Test::Pod::Coverage 1.04 required for testing POD coverage" if $@; all_pod_coverage_ok();