Rose::HTML::Object::Message - Text message object.


Rose-HTML-Objects documentation Contained in the Rose-HTML-Objects distribution.

Index


Code Index:

NAME

Top

Rose::HTML::Object::Message - Text message object.

SYNOPSIS

Top

  $msg = Rose::HTML::Object::Message->new('Hello world');
  print $msg->text; # Hello world

DESCRIPTION

Top

Rose::HTML::Object::Message objects encapsulate a text message with an optional integer id.

This class inherits from, and follows the conventions of, Rose::Object. See the Rose::Object documentation for more information.

OVERLOADING

Top

Stringification is overloaded to call the text method. In numeric and boolean contexts, Rose::HTML::Object::Message objects always evaluate to true.

CONSTRUCTOR

Top

new [ PARAMS | TEXT ]

Constructs a new Rose::HTML::Object::Message object. If a single argument is passed, it is taken as the value for the text parameter. Otherwise, PARAMS name/value pairs are expected. Any object method is a valid parameter name.

OBJECT METHODS

Top

id [INT]

Get or set the message's integer identifier.

text [ TEXT | OBJECT ]

Get or set the message's text. If the message text is set to a TEXT string (rather than a Rose::HTML::Object::Message-derived OBJECT), the id is set to the value of the constant Rose::HTML::Object::Message::CUSTOM_MESSAGE.

AUTHOR

Top

John C. Siracusa (siracusa@gmail.com)

LICENSE

Top

Copyright (c) 2010 by John C. Siracusa. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.


Rose-HTML-Objects documentation Contained in the Rose-HTML-Objects distribution.

package Rose::HTML::Object::Message;

use strict;

use Carp;
use Clone::PP;
use Scalar::Util();

use Rose::HTML::Object::Messages qw(CUSTOM_MESSAGE);

use base 'Rose::Object';

our $VERSION = '0.606';

#our $Debug = 0;

use overload
(
  '""'   => sub { shift->text },
  'bool' => sub { 1 },
  '0+'   => sub { 1 },
   fallback => 1,
);

use Rose::Object::MakeMethods::Generic
(
  scalar => 
  [
    'id',
    'variant',
  ],
);

sub as_string { no warnings 'uninitialized'; "$_[0]" }

sub init
{
  my($self) = shift;
  @_ = (text => @_)  if(@_ == 1);
  $self->SUPER::init(@_);
}

sub args
{
  my($self) = shift;

  if(@_)
  {
    my %args;

    if(@_ == 1 && ref $_[0] eq 'ARRAY')
    {
      my $i = 1;
      %args = (map { $i++ => $_ } @{$_[0]});
    }
    elsif(@_ == 1 && ref $_[0] eq 'HASH')
    {
      %args = %{$_[0]};

      my $i = 1;

      foreach my $key (sort keys %args)
      {
        $args{$i} = $args{$key}  unless(exists $args{$i});
        $i++;
      }
    }
    else
    {
      my $i = 1;
      %args = map { $i++ => $_ } @_;
    }

    $self->{'args'} = \%args;

    return wantarray ? %{$self->{'args'}} : $self->{'args'};
  }

  return wantarray ? %{$self->{'args'} || {}} : ($self->{'args'} ||= {});
}

sub parent
{
  my($self) = shift; 
  return Scalar::Util::weaken($self->{'parent'} = shift)  if(@_);
  return $self->{'parent'};
}

sub clone
{
  my($self) = shift;
  my $clone = Clone::PP::clone($self);
  $clone->parent(undef);
  return $clone;
}

sub text
{
  my($self) = shift;

  if(@_)
  {
    if(UNIVERSAL::isa($_[0], __PACKAGE__))
    {
      $self->id($_[0]->id);
      return $self->{'text'} = $_[0]->text;
    }

    $self->id(CUSTOM_MESSAGE);
    return $self->{'text'} = $_[0];
  }

  return $self->{'text'};
}

sub is_custom { no warnings; shift->id == CUSTOM_MESSAGE }

1;

__END__