[Dbix-class] Filtering module
jackal
me at jackal.in
Fri Oct 24 13:32:07 BST 2008
On Thursday 23 October 2008 20:18:49 Jess Robinson wrote:
> On Tue, 21 Oct 2008, jackal wrote:
> > This is proposal for a filtering component, similar to
> > DBIx::Class::InflateColumn
> > It can -modify- columns transparently. When you read column - it
> > inflates, when you write - deflates.
> > On creating a new column, whole cycle passed - deflating for DB and
> > inflating for usage.
> > Module can handle scalars, because of it's destination - to modify data.
>
> I think I see where you are going, but can you give some actual examples
> with data, how to use it etc?
>
> Someone did start/implement a ::Filter module, but I'm not sure where that
> went or why it disappeared.. That one was *just* for scalar to scalar
> though.
One example is in a module documentation:
package DB::Test;
use String::Util ':all';
__PACKAGE__->modify_column('html', {
inflate => sub { htmlesc(shift) }, # escaping html entities
deflate => sub { crunch(shift); } # removing extra whitespaces
});
in a script:
$r = $schema->resultset('Test')->create({html => ' a & b '}); # it will be
deflated into DB as 'a & b'
$r->html; # inflated as 'a & b'
$r->html(' c & d '); # deflated into DB as 'c & d'
$r->update;
$r->html; # inflated as 'c & b'
Here you must send data without html entities, but if you use data in your
html templates only and you don't want to modify data in controllers - you
shouldn't worry about this, because browsers will decode html entities by
themselves.
There's another example:
package DB::Test;
use String::Util ':all';
__PACKAGE__->modify_column('homepage', {
inflate => sub { $_[0] }, # as is
deflate => sub {
my $value = trim(shift);
if ($value !~ m!^([a-z]+://)?[a-z0-9\-\.]+([\/\?].*)?$!si) {
$value = undef;
} else {
$value .= '/' if ($value =~ m!^([a-z]+://)?[a-z0-9\-\.]+$!si);
$value = 'http://'.$value if ($value !~ m!^[a-z]+://!si);
}
return $value;
}
});
in a script:
$r = $schema->resultset('Test')->create({homepage => ' site.ru '}); # it will
be deflated into DB as 'http://site.ru/'
$r->homepage; # inflated as 'http://site.ru/' (as is)
$r->homepage('ftp://site.ru '); # deflated into DB as 'ftp://site.ru/'
$r->update;
$r->homepage; # inflated as 'ftp://site.ru/' (as is)
>
> > Module can works together with InflateColumn:
> > __PACKAGE__->modify_column('passw', ...);
> > __PACKAGE__->inflate_column('passw', ...);
> >
> > In that case, when you read - it inflates into object by InflateColumn,
> > then object will be modified by ModifyColumn. When wou write - there's
> > reverse process.
>
> Again, examples? ;)
There's example of using module with InflateColumn:
package DB::Test;
use URI;
__PACKAGE__->inflate_column('homepage', {
inflate => sub { URI->new($_[0]) },
deflate => sub { shift->as_string }
});
__PACKAGE__->modify_column('homepage', {
inflate => sub { $_[0] }, # as is
deflate => sub { shift->canonical } # normalize
});
in a script:
# it will be deflated into canonical version by ModifyColumn, then deflated
into scalar by InflateColumn
$r = $schema->resultset('Test')->create({homepage =>
URI->new('HTTP://WWW.perl.com:80')}); # deflated into DB
as 'http://www.perl.com/'
# it will be inflated into URI object by InflateColumn, then it can be
modified by ModifyColumn (vice-versa)
$r->homepage->as_string; # inflated as 'http://www.perl.com/' (as is)
>
> Jess
--
Kind regards, Eugeny.
More information about the DBIx-Class
mailing list