Moose Likes Classes



  • Its the last result_set day here in the Moose-pen.

    Going to try and get the final 'da_result_set' 'Class' to work today, and by now it should not take very long.

    The first thing I did was update '20_dad_load.t' yet again but this time I will not bother to show the tests as they should be like an old hat by now.

    I then added in a new attribute

    
            has da_result_class => (
                is  => 'rw',
                isa => 'Str|Undef',
                default     => undef,
            );
    
    and an update to pass that value down to the DAD.
    
            da_key_case          => $self->da_key_case,
    ++      da_result_class      => $self->da_result_class,
            identity_index       => $self->_identity_index
    
    and when I run my test case I get;
    
    ...
    ok 22 - Role DAD can da_result_class
    …
    ok 56 - result Class is undef
    
    so that looks ok.

    Now on the test case for Driver::DBI 20_result_sets.t I decided it was best not to add this set type to my loop, but have it as a standalone;


    $da->da_result_class("Xtest::DA::Person");
    $da->da_result_set("Class");
    $da->da_key_case("Lower");
    $da->retrieve($dbh);
    my $class = $da->result()->set->[0];
    ok(ref($class) eq "Xtest::DA::Person","Result set is correct class");
    cmp_deeply({%$class}, $expected->{"Lower"},"Class properties all set");

    as a class will only really work with the correct attributes matching and Lower is the best I have for that Person class.

    As one can see the above tests sets the class to 'Xtest::DA::Person', the result set to 'Class' and key to 'Lower'. I then do a retrieve and check that the returned set is the correct class then I check an unblessed version of the class against by expected.

    Now to code that up in Driver::DBI and I think all I need there is


    ++ if ($self->is_Class() and $self->da_result_class()){
    ++ my $class=$self->da_result_class();
    ++ my $new = $class->new($hash_ref);
    ++ push(@{$results},$new);
    ++ }
    ++ elsif ($self->is_JSON()){
    -- if ($self->is_JSON()){

    and on my first test run I get;

    not ok 17 - Class properties all set
    # Failed test 'Class properties all set'
    # at 20_result_sets.t line 74.
    # Comparing hash keys of $data
    # Missing: 'time zone'

    Ok I did expected that as I have an alias with a space in it for that field

    {name=>'description',
    alias=>'time zone',
    view=>'time_zone',}

    and that of course will never match up with an accessor so I will work with that by addting this to the test

    ok(ref($class) eq "Xtest::DA::Person","Result set is correct class");
    ++ ok(!$class->time_zone,"time_zone not set");
    ++ delete( $expected->{"Lower"}->{'time zone'});
    cmp_deeply({%$class}, $expected->{"Lower"},"Class properties all set");

    I make sure 'time_zone' is undef and a remove the 'time zone' key from the expected results and this time round I get;

    ok 16 - Result set is correct class
    ok 17 - time_zone not set
    ok 18 - Class properties all set

    so that solves that little problem;

    Something different for tomorrow I guess;

    DSC08261a.jpg



    http://blogs.perl.org/users/byterock/2018/10/moose-likes-classes.html

 

© Lightnetics 2018