Skip to content

Commit

Permalink
removed filter from Mojo::Base
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Nov 4, 2008
1 parent ef957a2 commit f4b7782
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 49 deletions.
1 change: 1 addition & 0 deletions Changes
@@ -1,6 +1,7 @@
This file documents the revision history for Perl extension Mojo.

0.9 2008-11-03 00:00:00
- Removed filter from Mojo::Base and added warnings.
- Added caching to uploads in Mojo::Message. (Mark Stosberg)
- Fixed typos. (Robert Hicks)
- Added documentation.
Expand Down
61 changes: 20 additions & 41 deletions lib/Mojo/Base.pm
Expand Up @@ -14,30 +14,18 @@ require Scalar::Util;
sub new {
my $proto = shift;

# Create instance
my $class = ref $proto || $proto;
my $self = bless {}, $class;

# Shortcut
return $self unless @_;

# Check attributes
my $attrs;
if (exists $_[1]) {
my %attrs = (@_);
$attrs = \%attrs;
}
else { $attrs = $_[0] }
$attrs ||= {};

# Attributes
for my $attr (keys %$attrs) {

# Attribute
if ($self->can($attr)) { $self->$attr($attrs->{$attr}) }

# No attribute, pass through to instance hash
else { $self->{$attr} = $attrs->{$attr} }
}
# Create instance
my $class = ref $proto || $proto;
my $self = bless $attrs, $class;

return $self;
}
Expand All @@ -60,12 +48,8 @@ sub attr {
else { $options = $_[0] }
$options ||= {};

Carp::croak('Option "filter" has to be a coderef')
if ($options->{filter} && ref $options->{filter} ne 'CODE');

my $chained = delete $options->{chained};
my $default = delete $options->{default};
my $filter = delete $options->{filter};
my $weak = delete $options->{weak};

undef $options;
Expand All @@ -84,6 +68,15 @@ sub attr {
# Header
my $code = "sub {\n";

# Warning gets optimized away
unless ($ENV{MOJO_BASE_OPTIMIZE}) {

# Check invocant
$code .= "${ws}Carp::croak(\'";
$code .= 'Attribute has to be called with an object not a class';
$code .= "')\n ${ws}unless ref \$_[0];\n";
}

# No value
$code .= "${ws}if (\@_ == 1) {\n";
unless (defined $default) {
Expand All @@ -106,24 +99,15 @@ sub attr {
}
$code .= "$ws}\n";

# Value
if ($filter) {

# Filter and store argument
$code .= "${ws}local \$_ = \$_[1];\n";
$code .= "$ws\$_[0]->{'$attr'} = \$filter->(\$_[0], \$_);\n";
# Store argument optimized
if (!$weak && !$chained) {
$code .= "${ws}return \$_[0]->{'$attr'} = \$_[1];\n";
}
else {

# Store argument optimized
if (!$weak && !$chained) {
$code .= "${ws}return \$_[0]->{'$attr'} = \$_[1];\n";
}

# Store argument the old way
else {
$code .= "$ws\$_[0]->{'$attr'} = \$_[1];\n";
}
# Store argument the old way
else {
$code .= "$ws\$_[0]->{'$attr'} = \$_[1];\n";
}

# Weaken
Expand Down Expand Up @@ -166,10 +150,7 @@ Mojo::Base - Once Upon A Midnight Dreary!
use base 'Mojo::Base';
__PACKAGE__->attr('driver');
__PACKAGE__->attr('doors',
default => 2,
filter => sub { s/\D//g; $_ }
);
__PACKAGE__->attr('doors', default => 2);
__PACKAGE__->attr([qw/passengers seats/],
chained => 1,
default => sub { 2 }
Expand Down Expand Up @@ -231,8 +212,6 @@ Currently there are four options supported.
Note that the default value is "lazy", which means it only
gets assigned to the instance after the attribute has been
called.
filter: Filters the value before assigning it to the instance,
must be a coderef.
weak: Weakens the attribute value.
=cut
6 changes: 3 additions & 3 deletions t/mojo/base.t
Expand Up @@ -43,8 +43,8 @@ is($m->figs, 5);
for my $i (101 .. 150) {
$y = !$y;
$monkeys->[$i] = LoaderTest->new;
is($monkeys->[$i]->name('FooBarBAZ'), 'foobarbaz');
$monkeys->[$i]->heads('ab3cd') if $y;
is($monkeys->[$i]->name('foobarbaz'), 'foobarbaz');
$monkeys->[$i]->heads('3') if $y;
$y ? is($monkeys->[$i]->heads, 3) : is($monkeys->[$i]->heads, 1);
}

Expand All @@ -60,4 +60,4 @@ for my $i (151 .. 200) {
is($monkeys->[$i]->eyes(6)->eyes, 6);
}

1;
1;
7 changes: 2 additions & 5 deletions t/mojo/lib/LoaderTest.pm
Expand Up @@ -14,10 +14,7 @@ __PACKAGE__->attr('bananas');
__PACKAGE__->attr([qw/ears eyes/], default => sub { 2 }, chained => 1);
__PACKAGE__->attr('figs', default => 0);
__PACKAGE__->attr('friend', {weak => 1});
__PACKAGE__->attr('heads', {
default => 1,
filter => sub { s/\D//g; $_ }
});
__PACKAGE__->attr('name', filter => sub { lc });
__PACKAGE__->attr('heads', {default => 1});
__PACKAGE__->attr('name');

1;

0 comments on commit f4b7782

Please sign in to comment.