Skip to content

Commit

Permalink
identify lazy attrs before stringify
Browse files Browse the repository at this point in the history
  • Loading branch information
ginesr committed Apr 30, 2014
1 parent 5cd2d8f commit dd77cbb
Show file tree
Hide file tree
Showing 8 changed files with 181 additions and 34 deletions.
58 changes: 33 additions & 25 deletions lib/Gideon.pm
Original file line number Diff line number Diff line change
Expand Up @@ -766,7 +766,8 @@ sub get_value_for_attribute_key {
my $class = shift;
my $attribute = shift;
my $key = shift;
my $meta = $__meta->{$class} || $class->get_all_meta;
my $package = ref $class ? ref $class : $class;
my $meta = $__meta->{$package} || $class->get_all_meta;

if ( exists $meta->{attributes}->{$attribute} ) {
if ( exists $meta->{attributes}->{$attribute}->{$key} ) {
Expand Down Expand Up @@ -808,6 +809,10 @@ sub get_all_meta {
}
}

if ($attribute->is_lazy) {
$meta_attr->{lazy} = 1;
}

$cache_meta->{$class_name}->{attributes}->{$name} = $meta_attr;
}

Expand Down Expand Up @@ -882,36 +887,39 @@ sub strigify {
my %params = ();

foreach my $attr (@attrs) {
if ($self->can($attr)) {
if( blessed($self->$attr) ) {
if ($self->$attr->can('to_string')) {
$params{$attr} = $self->$attr->to_string
}
elsif ($self->$attr->can('stringify')) {
$params{$attr} = $self->$attr->stringify
}
elsif ($self->$attr->can('as_string')) {
$params{$attr} = $self->$attr->as_string
}
else {
if (defined $self->$attr) {
$params{$attr} = $self->$attr . ''
}
else {
$params{$attr} = undef
}
}
}
elsif ($self->$attr) {
if ($self->get_value_for_attribute_key($attr,'lazy')) {
if ( $self->is_stored ) {
$params{$attr} = $self->$attr;
}
elsif (not defined $self->$attr) {
$params{$attr} = undef;
else {
# do not trigger lazy attributes
$params{$attr."[lazy]"} = undef;
next;
}
}
if ( blessed($self->$attr) ) {
# convert objects to strings
if ($self->$attr->can('to_string')) {
$params{$attr} = $self->$attr->to_string
}
elsif ($self->$attr->can('stringify')) {
$params{$attr} = $self->$attr->stringify
}
elsif ($self->$attr->can('as_string')) {
$params{$attr} = $self->$attr->as_string
}
else {
$params{$attr} = '';
if (defined $self->$attr) {
$params{$attr} = $self->$attr . ''
}
else {
$params{$attr} = undef
}
}
}
else {
$params{$attr} = $self->$attr
}
}

my $pkg_name = $class;
Expand Down
103 changes: 103 additions & 0 deletions t/07mysq_lazy.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#!perl

use lib 'xlib';
use strict;
use Try::Tiny;
use Test::More;
use Data::Dumper qw(Dumper);
use Cwd;
use DBI;
use Test::Exception;

if ( mysql_not_installed() ) {
plan skip_all => 'MySQL driver not installed';
}

if ( mysql_cant_connect() ) {
plan skip_all => 'Can\'t connect to local mysql using `test` user & db';
}

plan tests => 10;

use_ok(qw(Example::Driver::MySQL))
;use_ok(qw(Example::Lazy));

# Prepare test data ------------------------------------------------------------
prepare_test_data();
# ------------------------------------------------------------------------------

my $driver = Example::Driver::MySQL->new(
db => 'test',
username => 'test',
host => 'localhost'
);

Gideon->register_store( 'mysql', $driver );

my $johnson = Example::Lazy->find( name => 'Johnson' );

is($johnson->id,2,'ID OK');
is($johnson->order,1,'Order OK');

like($johnson,qr/^Example::Lazy \(2\)/,'String with class and id');
like($johnson,qr/"order": "1"/,'String with lazy attribute value');

my $doe = Example::Lazy->new( name => 'Doe');

like($doe,qr/^Example::Lazy \{/,'String with class and no id');
like($doe,qr/"order\[lazy\]": null/,'String with lazy attribute not intialized');

is($doe->order,10,'New order OK');

$doe->save;

like($doe,qr/"order": 10/,'String with lazy attribute after save');

# Auxiliary test functions -----------------------------------------------------

sub prepare_test_data {

#standard mysql install has test db and test user, try to use that
my $dbh = DBI->connect( "dbi:mysql:database=test;host=;port=", "test", "" );
my @names = qw(Smith Johnson Williams Brown Jones Miller Davis Garcia
Rodriguez Wilson);
my $create_t1 = qq~create table gideon_t4 (
`id` int not null auto_increment,
`name` varchar(20),
`order` int null,
primary key (`id`), key (`name`))~;

$dbh->do('drop table if exists gideon_t4');
$dbh->do($create_t1);

for ( 0 .. 9 ) {
$dbh->do( "insert into gideon_t4 (`name`,`order`) values(?,?)",
undef, $names[$_], $_ );
}

}

sub mysql_not_installed {
try { use DBD::mysql; return undef }
catch { return 1 }
}

sub mysql_cant_connect {

my $test_db = 'database=test;host=;port=';
my $test_user = 'test';
my $test_pass = '';

if ($ENV{'GIDEON_DBI_TEST_DB'}) {
$test_db = $ENV{GIDEON_DBI_TEST_DB};
}
if ($ENV{'GIDEON_DBI_TEST_USER'}) {
$test_user = $ENV{GIDEON_DBI_TEST_USER};
}
if ($ENV{'GIDEON_DBI_TEST_PASS'}) {
$test_user = $ENV{GIDEON_DBI_TEST_PASS};
}

try { DBI->connect( "dbi:mysql:$test_db", $test_user, $test_pass ) or die; return undef }
catch { return 1 }
}
2 changes: 1 addition & 1 deletion t/09mongo.t
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ if ( mongo_not_installed() ) {
} elsif ( mongo_not_running() ) {
plan skip_all => 'Mongo daemon not running on localhost';
} else {
plan tests => 25;
plan tests => 27;
}

use_ok(qw(Example::Driver::Mongo));
Expand Down
40 changes: 40 additions & 0 deletions xlib/Example/Lazy.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package Example::Lazy;

use Gideon::DBI;
use Gideon::Meta::Attribute::DBI;
use Moose;

extends 'Gideon::DBI';
store 'mysql:gideon_t4';

has 'id' => (
is => 'rw',
isa => 'Num',
column => 'id',
serial => 1,
primary_key => 1,
metaclass => 'Gideon'
);

has 'name' => (
is => 'rw',
required => 1,
isa => 'Str',
column => 'name',
metaclass => 'Gideon'
);

has 'order' => (
is => 'rw',
isa => 'Num',
column => 'order',
metaclass => 'Gideon',
lazy => 1,
default => sub {
my $self = shift;
my $max = __PACKAGE__->function('max', 'order' );
return $max + 1;
}
);

__PACKAGE__->meta->make_immutable();
3 changes: 1 addition & 2 deletions xlib/Example/Test.pm
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

package Example::Test;

use strict;
Expand Down Expand Up @@ -34,4 +33,4 @@ has 'value' => (
metaclass => 'Gideon'
);

1;
__PACKAGE__->meta->make_immutable();
3 changes: 1 addition & 2 deletions xlib/Example/Test2.pm
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

package Example::Test2;

use strict;
Expand Down Expand Up @@ -34,4 +33,4 @@ has 'value' => (
metaclass => 'Gideon'
);

1;
__PACKAGE__->meta->make_immutable();
3 changes: 1 addition & 2 deletions xlib/Example/TestInvalid.pm
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

package Example::TestInvalid;

use strict;
Expand Down Expand Up @@ -34,4 +33,4 @@ has 'value' => (
metaclass => 'Gideon'
);

1;
__PACKAGE__->meta->make_immutable();
3 changes: 1 addition & 2 deletions xlib/Mongo/Person.pm
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

package Mongo::Person;

use strict;
Expand Down Expand Up @@ -27,4 +26,4 @@ has 'city' => ( is => 'rw', isa => 'Str', metaclass => 'Gideon' );
has 'country' => ( is => 'rw', isa => 'Str', metaclass => 'Gideon' );
has 'type' => ( is => 'rw', isa => 'Num', metaclass => 'Gideon' );

1;
__PACKAGE__->meta->make_immutable();

0 comments on commit dd77cbb

Please sign in to comment.