App-Repository

 view release on metacpan or  search on metacpan

lib/App/Repository.pm  view on Meta::CPAN

    return 0;
}

#############################################################################
# hashes_by_indexed_values()
#############################################################################

=head2 hashes_by_indexed_values()

    * Signature: &App::Repository::hashes_by_indexed_values($a,$b);
    * Param:     $a            []
    * Param:     $b            []
    * Return:    void
    * Throws:    App::Exception::Repository
    * Since:     0.01

    Sample Usage: 

    @data = (
        { size => 5, name => "Jim", color => "Red",    score => 13.5, ],
        { size => 3, name => "Bob", color => "Green",  score =>  4.2, ],
        { size => 9, name => "Ken", color => "Blue",   score => 27.4, ],
        { size => 2, name => "Kim", color => "Yellow", score => 11.7, ],
        { size => 7, name => "Jan", color => "Purple", score => 55.1, ],
    );

    @App::Repository::sort_keys = ( "size", "color", "name" );
    @App::Repository::sort_types = ("C", "N", "C");
    @App::Repository::sort_dirs = ("asc", "desc", "desc");
    # OR @App::Repository::sort_dirs = ("_asc", "_desc", "_desc");
    # OR @App::Repository::sort_dirs = ("UP", "DOWN", "DOWN");

    @sorted_data = sort hashes_by_indexed_values @data;

The hashes_by_indexed_values() function is used to sort rows of data
based on indexes, data types, and directions.

=cut

sub hashes_by_indexed_values {
    my ($pos, $key, $type, $dir, $sign);
    for ($pos = 0; $pos <= $#App::Repository::sort_keys; $pos++) {
        $key  = $App::Repository::sort_keys[$pos];
        $type = $App::Repository::sort_types[$pos];
        $dir  = $App::Repository::sort_dirs[$pos];
        if (defined $type && $type eq "N") {
            $sign = ($a->{$key} <=> $b->{$key});
        }
        else {
            $sign = ($a->{$key} cmp $b->{$key});
        }
        if ($sign) {
            $sign = -$sign if (defined $dir && $dir =~ /^_?[Dd]/); # ("DOWN", "desc", "_desc", etc.)
            return ($sign);
        }
    }
    return 0;
}

#############################################################################
# _get_timer()
#############################################################################

sub _get_timer {
    my ($self) = @_;
    my ($seconds_start, $microseconds_start) = gettimeofday;
    my $timer = { seconds_start => $seconds_start, microseconds_start => $microseconds_start };
    return($timer);
}

#############################################################################
# _read_timer()
#############################################################################

sub _read_timer {
    my ($self, $timer, $reset) = @_;
    my ($seconds_finish, $microseconds_finish) = gettimeofday;
    my $seconds_elapsed           = $seconds_finish      - $timer->{seconds_start};
    my $microseconds_elapsed      = $microseconds_finish - $timer->{microseconds_start};
    if ($microseconds_elapsed < 0) {
        $microseconds_elapsed += 1000000;
        $seconds_elapsed      -= 1;
    }
    my $time_elapsed = sprintf("%d.%06d", $seconds_elapsed, $microseconds_elapsed);
    if (defined $reset) {
        # store values. don't reset the timer.
        if ($reset == 0) {
            $timer->{seconds_start}      = $seconds_finish;
            $timer->{microseconds_start} = $microseconds_finish;
            delete $timer->{time_elapsed};
        }
        # reset the timer to be ready for another reading.
        elsif ($reset) {
            $timer->{seconds_finish}      = $seconds_finish;
            $timer->{microseconds_finish} = $microseconds_finish;
            $timer->{time_elapsed}        = $time_elapsed;
        }
    }
    return($time_elapsed);
}

#############################################################################
# DESTROY()
#############################################################################

=head2 DESTROY()

    * Signature: $self->DESTROY();
    * Param:     void
    * Return:    void
    * Throws:    App::Exception::Repository
    * Since:     0.01

    Sample Usage: 

    $self->DESTROY();   # never called explicitly. called by Perl itself.

The DESTROY() method is called when the repository object is release from
memory.  This happen when the calling program lets the variable holding the
object reference go out of scope, sets the variable to something else,
or exits the program without otherwise releasing the object.

The DESTROY() method simply calls disconnect() to make sure that all
connection-related resources are freed.  This is safe, assuming (correctly)
that the disconnect() method may be called without negative consequences
even when already disconnected from the repository.

=cut

sub DESTROY {
    my $self = shift;
    $self->_disconnect();
}

=head1 ACKNOWLEDGEMENTS

 * Author:  Stephen Adkins <spadkins@gmail.com>
 * License: This is free software. It is licensed under the same terms as Perl itself.

=head1 SEE ALSO

L<C<App::Context>|App::Context>,
L<C<App::Service>|App::Service>

=cut

1;

__END__

    if (0) {  # HASH (or object of some type)
        # determine which columns should be summable and which have expressions
        my $agg_columns  = [];
        my $sum_columns  = [];
        my $expr_columns = [];
        my $contains_expr = 0;



( run in 2.784 seconds using v1.01-cache-2.11-cpan-adec679a428 )