App-CELL

 view release on metacpan or  search on metacpan

lib/App/CELL/Message.pm  view on Meta::CPAN

    my $dl = $def_lang || 'en';
    return $dl;
}


=head2 new
  
Construct a message object. Takes a PARAMHASH containing, at least,
a 'code' attribute as well as, optionally, other attributes such as
'args' (a reference to an array of arguments). Returns a status object. If
the status is ok, then the message object will be in the payload. See
L</SYNOPSIS>.

=cut

sub new {

    my ( $class, %ARGS ) = @_; 
    my $stringified_args = stringify_args( \%ARGS );
    my $my_caller;
    my $msgobj = {};

    #$log->debug( "Entering Message->new called from " . (caller)[1] . " line " . (caller)[2]);
    if ( $ARGS{called_from_status} ) {
        $my_caller = $ARGS{caller};
    } else {
        $my_caller = [ CORE::caller() ];
    }
   
    if ( not exists( $ARGS{'code'} ) ) {
        return App::CELL::Status->new( level => 'ERR', 
            code => 'CELL_MESSAGE_NO_CODE', 
            caller => $my_caller,
        );
    }
    if ( not $ARGS{'code'} ) {
        return App::CELL::Status->new( level => 'ERR', 
            code => 'CELL_MESSAGE_CODE_UNDEFINED',
            caller => $my_caller,
        );
    }
    $msgobj->{'code'} = $ARGS{code};

    if ( $ARGS{lang} ) {
        $log->debug( $ARGS{code} . ": " . $mesg->{ $ARGS{code} }->{ $ARGS{lang} }->{ 'Text' }, 
                     cell => 1 );
    }
    $msgobj->{'lang'} = $ARGS{lang} || $def_lang || 'en';
    $msgobj->{'file'} = $mesg->
			{ $msgobj->{code} }->
 			{ $msgobj->{lang} }->
			{ 'File' } || '<NONE>';
    $msgobj->{'line'} = $mesg->
			{ $msgobj->{code} }->
 			{ $msgobj->{lang} }->
			{ 'Line' } || '<NONE>';

    # This next line is important: it may happen that the developer wants
    # to quickly code some messages/statuses without formally assigning
    # codes in the site configuration. In these cases, the $mesg lookup
    # will fail. Instead of throwing an error, we just generate a message
    # text from the value of 'code'.
    my $text = $mesg->
               { $msgobj->{code} }->
               { $msgobj->{lang} }->
               { 'Text' } 
               || $msgobj->{code};

    # strip out anything that resembles a newline
    $text =~ s/\n//g;
    $text =~ s/\012/ -- /g;

    my $stringy = stringify_args( $ARGS{args} ) || '';
    if ( defined $ARGS{args} and @{ $ARGS{args} } and not $text =~ m/%s/ ) {
        $ARGS{text} = $text . " ARGS: $stringy";
    } else {

        # insert the arguments into the message text -- needs to be in an eval
        # block because we have no control over what crap the application
        # programmer might send us
        try { 
            local $SIG{__WARN__} = sub {
                die @_;
            };
            $ARGS{text} = sprintf( $text, @{ $ARGS{args} || [] } ); 
        }
        catch {
            my $errmsg = $_;
            $errmsg =~ s/\012/ -- /g;
            $ARGS{text} = "CELL_MESSAGE_ARGUMENT_MISMATCH on $ARGS{code}, error was: $errmsg"; 
            $log->err( $ARGS{text}, cell => 1);
        };

    }
    $msgobj->{'text'} = $ARGS{text};

    # uncomment if needed
    #$log->debug( "Creating message object ->" . $ARGS{code} . 
    #             "<- with args ->$stringified_args<-", 
    #             caller => $my_caller, cell => 1);

    # bless into objecthood
    my $self = bless $msgobj, __PACKAGE__;

    # return ok status with created object in payload
    return App::CELL::Status->new( level => 'OK',
        payload => $self,
    );
}


=head2 lang

Clones the message into another language. Returns a status object. On
success, the new message object will be in the payload.

=cut

sub lang {
    my ( $self, $lang ) = @_;
    my $status = __PACKAGE__->new( 



( run in 0.382 second using v1.01-cache-2.11-cpan-39bf76dae61 )