JSORB::Client::Compiler - A base role for JSORB client compilers


JSORB documentation Contained in the JSORB distribution.

Index


Code Index:

NAME

Top

JSORB::Client::Compiler - A base role for JSORB client compilers

DESCRIPTION

Top

TODO

METHODS

Top

compile ( namespace = $namespace, ?to => $to )>
get_all_errors

REQUIRED METHODS

Top

compile_root_namespace ( $root_namespace )
compile_namespace ( $namespace )
compile_interface ( $interface )
compile_procedure ( $procedure )

UTILITY METHODS FOR SUBCLASSES

Top

perform_unit_of_work ( \&work )
print_to_buffer ( @data )

BUGS

Top

All complex software has bugs lurking in it, and this module is no exception. If you find a bug please either email me, or add the bug to cpan-RT.

AUTHOR

Top

Stevan Little <stevan.little@iinteractive.com>

COPYRIGHT AND LICENSE

Top


JSORB documentation Contained in the JSORB distribution.

package JSORB::Client::Compiler;
use Moose::Role;
use MooseX::AttributeHelpers;
use MooseX::Params::Validate;
use MooseX::Types::Path::Class;

use Buffer::Transactional;
use IO::Scalar;
use Try::Tiny;

our $VERSION   = '0.04';
our $AUTHORITY = 'cpan:STEVAN';

has '_buffer' => (
    init_arg => undef,
    is       => 'rw',
    isa      => 'Buffer::Transactional'
);

has '_errors' => (
    metaclass => 'Collection::Array',
    init_arg  => undef,
    is        => 'ro',
    isa       => 'ArrayRef[Str]',
    lazy      => 1,
    default   => sub { [] },
    provides  => {
        'push'     => '_add_error',
        'elements' => 'get_all_errors',
    }
);

sub print_to_buffer {
    my ($self, @data) = @_;
    $self->_buffer->print( map { "$_\n" } @data );
}

sub perform_unit_of_work {
    my ($self, $work) = @_;
    try {
        $self->_buffer->begin_work;
        $work->();
        $self->_buffer->commit;
    } catch {
        $self->_buffer->rollback;
        $self->_add_error( $_ );
    };
}


sub compile {
    my ($self, $namespace, $output) = validated_list(\@_,
        namespace => { isa => 'JSORB::Namespace' },
        to        => { isa => 'Path::Class::File', coerce => 1, optional => 1 },
    );

    confess "Currently only compiling from root namespace is supported"
        if $namespace->has_parent;

    # FIXME:
    # this dual usage of $source is
    # yucky, I should fix it at some
    # point.
    # - SL

    my $source;

    unless (defined $output) {
        $self->_buffer( Buffer::Transactional->new( out => IO::Scalar->new( \$source ) ) );
    }
    else {
        $source = $output->openw;
        $self->_buffer( Buffer::Transactional->new( out => $source ) );
    }

    $self->perform_unit_of_work(sub {
        $self->compile_root_namespace( $namespace );
    });

    unless (defined $output) {
        return $source;
    }
    else {
        $source->close;
    }
}

requires 'compile_root_namespace';
requires 'compile_namespace';
requires 'compile_interface';
requires 'compile_procedure';

no Moose::Role; 1;

__END__