CatalystX::Usul::File - Read and write configuration files


CatalystX-Usul documentation Contained in the CatalystX-Usul distribution.

Index


Code Index:

Name

Top

CatalystX::Usul::File - Read and write configuration files

Version

Top

0.3.$Revision: 576 $

Synopsis

Top

   use CatalystX::Usul::File;

Description

Top

Provides CRUD methods for read and write configuration files. For each schema a subclass is defined that inherits from this class

Subroutines/Methods

Top

new

Creates a new result source

add_to_attribute_list

   $c->model( q(Config::*) )->add_to_attribute_list( $args );

Add new items to an attribute list. The $args hash requires these keys; file the name of the file to edit, name the name of the element to edit, list the attribute of the named element containing the list of existing items, req the request object and field the field on the request object containing the list of new items

create

   $c->model( q(Config::*) )->create( $args );

Creates a new element. The $args hash requires these keys; file the name of the file to edit, name the name of the element to edit and fields is a hash containing the attributes of the new element. Missing attributes are defaulted from the defaults attribute of the CatalystX::Usul::File::Schema object

delete

   $c->model( q(Config::*) )->delete( $args );

Deletes an element

find

   $c->model( q(Config::*) )->find( $args );

get_list

   $c->model( q(Config::*) )->get_list( $args );

Retrieves the named element and a list of elements

load_files

remove_from_attribute_list

   $c->model( q(Config::*) )->remove_from_attribute_list( $args );

Removes items from an attribute list

   $c->model( q(Config::*) )->search( $args );

Search for elements that match the supplied criteria

update

   $c->model( q(Config::*) )->update( $args );

Updates the named element

Diagnostics

Top

None

Configuration and Environment

Top

None

Dependencies

Top

CatalystX::Usul::Usul
CatalystX::Usul::File::ResultSource

Incompatibilities

Top

There are no known incompatibilities in this module

Bugs and Limitations

Top

There are no known bugs in this module. Please report problems to the address below. Patches are welcome

Author

Top

Peter Flanigan, <Support at RoxSoft.co.uk>

License and Copyright

Top


CatalystX-Usul documentation Contained in the CatalystX-Usul distribution.

# @(#)$Id: File.pm 576 2009-06-09 23:23:46Z pjf $

package CatalystX::Usul::File;

use strict;
use warnings;
use version; our $VERSION = qv( sprintf '0.3.%d', q$Rev: 576 $ =~ /\d+/gmx );
use parent qw(CatalystX::Usul);

use CatalystX::Usul::File::ResultSource;
use Class::C3;

__PACKAGE__->config
   ( result_source_class => q(CatalystX::Usul::File::ResultSource),
     schema_attributes   => {} );

__PACKAGE__->mk_accessors( qw(result_source result_source_class
                              schema_attributes ) );

sub new {
   my ($self, $app, @rest) = @_;

   my $new   = $self->next::method( $app, @rest );
   my $attrs = { schema_attributes => $new->schema_attributes };

   $new->result_source( $new->result_source_class->new( $app, $attrs ) );

   return $new;
}

sub add_to_attribute_list {
   my ($self, $args) = @_; my ($added, $attrs, $list);

   my ($rs, $name) = $self->_validate_params( $args );

   $self->throw( 'No list name specified' ) unless ($list = $args->{list});

   my $items = $args->{items};

   $self->throw( 'List contains no items' ) unless ($items->[0]);

   $self->_txn_do( $args->{path}, sub {
      ($attrs, $added) = $rs->push_attribute( $name, $list, $items );
      $rs->find_and_update( $name, $attrs );
   } );

   return $added;
}

sub create {
   my ($self, $args) = @_;

   my ($rs, $name) = $self->_validate_params( $args );

   $args->{fields}->{name} = $name;

   $self->_txn_do( $args->{path}, sub {
      $rs->create( $args->{fields} )->insert;
   } );

   return $name;
}

sub delete {
   my ($self, $args) = @_;

   my ($rs, $name) = $self->_validate_params( $args );

   $self->_txn_do( $args->{path}, sub {
      my $element = $rs->find( $name );
      my $msg     = 'Element [_1] does not exist';

      $self->throw( error => $msg, args => [ $name ] ) unless ($element);

      $element->delete;
   } );

   return $name;
}

sub find {
   my ($self, $args) = @_;

   my ($rs, $name) = $self->_validate_params( $args );

   return $self->_txn_do( $args->{path}, sub { $rs->find( $name ) } );
}

sub get_list {
   my ($self, $args) = @_; my $path;

   $self->throw( 'No file path specified' ) unless ($path = $args->{path});

   my $rs = $self->result_source->resultset( $path, $args->{lang} );

   return $self->_txn_do( $args->{path},
                          sub { $rs->get_list( $args->{name} ) } );
}

sub load_files {
   my ($self, @paths) = @_;

   my $rs = $self->result_source->resultset;

   return $rs->storage->load_files( @paths ) || {};
}

sub remove_from_attribute_list {
   my ($self, $args) = @_; my ($attrs, $list, $removed);

   my ($rs, $name) = $self->_validate_params( $args );

   $self->throw( 'No list name specified' ) unless ($list = $args->{list});

   my $items = $args->{items};

   $self->throw( 'List contains no items' ) unless ($items->[0]);

   $self->_txn_do( $args->{path}, sub {
      ($attrs, $removed) = $rs->splice_attribute( $name, $list, $items );
      $rs->find_and_update( $name, $attrs );
   } );

   return $removed;
}

sub search {
   my ($self, $args) = @_; my ($lang, $path);

   $self->throw( 'No file path specified' ) unless ($path = $args->{path});
   $self->throw( 'No language specified'  ) unless ($lang = $args->{lang});

   my $rs = $self->result_source->resultset( $path, $lang );

   return $self->_txn_do( $path, sub { $rs->search( $args->{criterion} ) } );
}

sub update {
   my ($self, $args) = @_;

   my ($rs, $name) = $self->_validate_params( $args );

   $self->_txn_do( $args->{path}, sub {
      $rs->find_and_update( $name, $args->{fields} );
   } );

   return $name;
}

# Private methods

sub _txn_do {
   my ($self, $path, $code_ref) = @_; my ($e, $res);

   my $key = q(txn:).$path->pathname;

   $self->lock->set( k => $key );

   if (wantarray) { @{ $res } = eval { $code_ref->() } }
   else { $res = eval { $code_ref->() } }

   if ($e = $self->catch) {
      $self->lock->reset( k => $key ); $self->throw( $e );
   }

   $self->lock->reset( k => $key );

   return wantarray ? @{ $res } : $res;
}

sub _validate_params {
   my ($self, $args) = @_; my ($name, $path, $rs);

   $self->throw( 'No file path specified'    ) unless ($path = $args->{path});
   $self->throw( 'No element name specified' ) unless ($name = $args->{name});

   $rs = $self->result_source->resultset( $path, $args->{lang} );

   return ($rs, $name);
}

1;

__END__

# Local Variables:
# mode: perl
# tab-width: 3
# End: