NAME

SPOPS::Utility - Utility methods for SPOPS objects

SYNOPSIS

# In configuration file
 'isa' => [ qw/ SPOPS::Utility SPOPS::DBI / ],

# Create an object and run a utility
my $user = MyApp::User->fetch( $id );
$user->{crypted_password} = $user->crypt_it( $new_password );

# Also use them separately
use SPOPS::Utility qw();

my $now = SPOPS::Utility->now;
my $random = SPOPS::Utility->generate_random_code( 16 );
my ( $lower, $upper ) = SPOPS::Utility->determine_limit( '50,100' );

DESCRIPTION

This class has a number of utility methods that can be used from SPOPS objects or from the SPOPS classes. They were previously in the main SPOPS module but were removed to make the classes more consistent and focused.

The different methods are fairly unrelated.

METHODS

determine_limit( $limit )

This supports the fetch() implementation of SPOPS subclasses. It is used to help figure out what records to fetch. Pass in a $limit string and get back a two-item list with the offset and max.

The $limit string can be in one of two formats:

'x,y'  --> offset = x, max = y
'x'    --> offset = 0, max = x

Example:

$p->{limit} = "20,30";
my ( $offset, $max ) = SPOPS::Utility->determine_limit( $p->{limit} );

# Offset is 20, max is 30, so you should get back records 20 - 30.

If no $limit is passed in, the values of both items in the two-value list are 0.

generate_random_code( $length )

Generates a random code of $length length consisting of upper-case characters in the english alphabet.

crypt_it( $text )

Returns a crypt()ed version of $text. If $text not passed in, returns undef.

now( \% )

Return the current time, formatted: yyyy-mm-dd hh:mm:ss. Since we use the Class::Date module (which in turn uses standard strftime formatting strings), you can pass in a format for the date/time to fit your needs.

Parameters:

  • format: strftime format

  • time: return of time command (or manipulation thereof); see perldoc -f time

today()

Return a date (yyyy-mm-dd) for today.

now_between_dates( { begin => $dateinfo, end => $dateinfo } )

Where $dateinfo is either a simple scalar ('yyyy-mm-dd') or an arrayref ([yyyy,mm,dd]).

Note that you can also just pass one of the dates and the check will still perform ok.

Returns 1 if 'now' is between the two dates (inclusive), undef otherwise.

Examples:

# Today is '2000-10-31' in all examples

SPOPS::Utility->now_between_days( { begin => '2000-11-01' } );
( returns 'undef' )

SPOPS::Utility->now_between_days( { end => '1999-10-31' } );
( returns 'undef' )

SPOPS::Utility->now_between_days( { begin => [2000, 10, 1 ] } );
( returns 1 )

SPOPS::Utility->now_between_days( { begin => '2000-10-01',
                                    end   => '2001-10-01' } );
( returns 1 )

list_process( \@existing, \@new )

Returns: hashref with three keys, each with an arrayref as the value:

keep:   items found in both \@existing and \@new
add:    items found in \@new but not \@existing
remove: items found in \@existing but not \@new

Mainly used for determining one-to-many relationship changes, but you can probably think of other applications.

COPYRIGHT

Copyright (c) 2001-2004 intes.net, inc.. All rights reserved.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

AUTHORS

Chris Winters <chris@cwinters.com>

See the SPOPS module for the full author list.