Mojolicious::Plugin::MoreUtilHelpers - Methods to format, count, sanitize, etc...
# Mojolicious $self->plugin('MoreUtilHelpers', %defaults); # Mojolicious::Lite plugin 'MoreUtilHelpers', %defaults; $self->count(10, 'user'); # 10 users $self->count([User->new]); # 1 user $self->paragraphs($text); # <p>line 1</p><p>line 2</p>... $self->maxwords('a, b, c', 2) # a, b... $self->sanitize($html); # remove all HTML # keep <a> and <p> tags $self->sanitize($html, tags => ['a','p']); # future calls to param($name[n]) return trimmed values $self->trim_param(@names); # DWIM Mojo::Collection $self->collection(@data); $self->collection($data);
Defaults can be set for certain methods when the plugin is loaded.
$self->plugin('MoreUtilHelpers', maxwords => { omit => ' [snip]' }, sanitize => { tags => ['code', 'pre', 'a'] });
By default and, unless stated otherwise, no defaults are set. See the method docs for more info.
$self->count(10, 'user'); # 10 users $self->count([User->new]); # 1 user $self->count([User->new], 'Luser'); # 1 Luser
Use the singular or plural form of the word based on the number given by the first argument. If a non-empty array of objects are given the lowercase form of the package's basename is used.
$self->collection(1,2,3) $self->collection([1,2,3]); $self->collection(undef); # empty collection
DWIM (Do What I Mean) Mojo::Collection creation. Currently Mojo::Collection does not differentiate between undef and array ref arguments. For example:
Mojo::Collection
undef
$self->c(1)->to_array; # [1] $self->c([1])->to_array; # [[1]] $self->c(undef)->to_array; # [undef] $self->c([1,2,[3]])->to_array; # [[1,2,[3]]]
Using collection to create a Mojo::Collection will give you the following:
collection
$self->collection(1)->to_array; # [1] $self->collection([1])->to_array; # [1] $self->collection(undef)->to_array; # [] $self->collection([1,2,[3]])->to_array; # [1,2,[3]]
To replace the c helper with collection:
c
$self->plugin('MoreUtilHelpers', collection => { patch => 1 });
This does not replace Mojo::Collection::c.
$self->maxwords($str, $n); $self->maxwords($str, $n, '…');
Truncate $str after $n words. If $str has more than $n words traling punctuation characters are stripped from the $nth word and '...' is appended. An alternate ommision character can be given as the third option.
$str
$n
'...'
$self->plugin('MoreUtilHelpers', maxwords => { omit => ' [snip]', max => 20 });
$self->paragraphs($text);
Wrap lines seperated by empty \r\n or \n lines in HTML paragraph tags (p). For example: A\r\n\r\nB\r\n would be turned into <p>A\r\n</p><p>B\r\n</p>.
\r\n
\n
p
A\r\n\r\nB\r\n
<p>A\r\n</p><p>B\r\n</p>
The returned HTML is assumed to be safe and is wrapped in a Mojo::ByteStream.
$self->sanitize($html); $self->sanitize($html, tags => ['a','p'], attr => ['href']);
Remove all HTML tags in the string given by $html. If tags and -optionally- attr are given, remove everything but those tags and attributes.
$html
tags
attr
$self->plugin('MoreUtilHelpers', sanitize => { tags => ['a','p'], attr => ['href'] });
$self->trim_param(@names); $self->trim_param(qr{user\.});
For each param name in @names, make future calls to "param" in Mojolicious::Controller return these params' values without leading and trailing whitespace. If an element of @names is a regexp all matching param names will be processed.
@names
In some cases it may be best to add this to your routes via "under" in Mojolicious::Routes:
my $account = $self->routes->under(sub { shift->trim_param('name', 'email', 'phone'); return 1; }); $account->post('save')->to('account#save'); $account->post('update')->to('account#update');
Now calling $self->param in these actions for 'name', 'email' or 'phone' will return a trimmed result.
$self->param
'name'
'email'
'phone'
Leading/trailing whitespace is removed by calling Mojo::Util::trim.
Lingua::EN::Inflect, Number::Format, List::Cycle, Mojolicious::Plugin::UtilHelpers
Skye Shaw (skye.shaw [AT] gmail.com)
Copyright (c) 2012-2014 Skye Shaw. This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install Mojolicious::Plugin::MoreUtilHelpers, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Mojolicious::Plugin::MoreUtilHelpers
CPAN shell
perl -MCPAN -e shell install Mojolicious::Plugin::MoreUtilHelpers
For more information on module installation, please visit the detailed CPAN module installation guide.