EntityModel

 view release on metacpan or  search on metacpan

lib/EntityModel/Storage.pm  view on Meta::CPAN

=head1 DESCRIPTION

See L<EntityModel> for more details.

=head1 METHODS

=cut

=head2 register

Register with L<EntityModel> so that callbacks trigger when further definitions are loaded/processed.

The base storage engine doesn't provide any callbacks - but we define the method anyway so that we don't
need to check for ->can.

=cut

sub register {
	my $class = shift;
}

=head2 apply_model

lib/EntityModel/Storage.pm  view on Meta::CPAN


=item * L</remove> - deletes an existing entry from storage, takes entity and ID

=back

Each of these applies to a single entity instance only. Since they operate on a callback
basis, multiple operations can be aggregated if desired:

 select * from storage where id in (x,y,z)

Two callbacks are required for each of the above operations:

=over 4

=item * on_complete - the operation completed successfully and the data is guaranteed to
have been written to storage. The strength of this guarantee depends on the storage engine
but it should be safe for calling code to assume that any further operations will not result
in losing the data - for example, a database engine would commit the data before sending this
event.

=item * on_fail - the operation was not successful and storage has been rolled back to

lib/EntityModel/Storage.pod  view on Meta::CPAN

See L<EntityModel>.

=head1 DESCRIPTION

See L<EntityModel> for more details.

=head1 METHODS

=head2 register

Register with L<EntityModel> so that callbacks trigger when further definitions are loaded/processed.

The base storage engine doesn't provide any callbacks - but we define the method anyway so that we don't
need to check for ->can.

=head2 apply_model

Apply the given model.

=head2 apply_model_and_schema

Apply the given model to the storage layer.

lib/EntityModel/Storage.pod  view on Meta::CPAN


=item * L</remove> - deletes an existing entry from storage, takes entity and ID

=back

Each of these applies to a single entity instance only. Since they operate on a callback
basis, multiple operations can be aggregated if desired:

 select * from storage where id in (x,y,z)

Two callbacks are required for each of the above operations:

=over 4

=item * on_complete - the operation completed successfully and the data is guaranteed to
have been written to storage. The strength of this guarantee depends on the storage engine
but it should be safe for calling code to assume that any further operations will not result
in losing the data - for example, a database engine would commit the data before sending this
event.

=item * on_fail - the operation was not successful and storage has been rolled back to

lib/EntityModel/Support.pm  view on Meta::CPAN

=head1 DESCRIPTION

See L<EntityModel>.

=head1 METHODS

=cut

=head2 register

Register with L<EntityModel> so that callbacks trigger when further definitions are loaded/processed.

=cut

sub register {
	my $class = shift;
}

=head2 apply_model

Apply the given model.

lib/EntityModel/Support.pod  view on Meta::CPAN

See L<EntityModel>.

=head1 DESCRIPTION

See L<EntityModel>.

=head1 METHODS

=head2 register

Register with L<EntityModel> so that callbacks trigger when further definitions are loaded/processed.

=head2 apply_model

Apply the given model.

=head1 INHERITED METHODS

=over 4

=item L<EntityModel::BaseClass>

lib/EntityModel/Support/Perl/Base.pm  view on Meta::CPAN

=back

These are handled by the L</_queue_task> and L</_attach_event> methods respectively.

=cut

use Time::HiRes qw{time};
use POSIX::strptime ();
use Tie::Cache::LRU;

sub _supported_callbacks { qw(before_commit after_load on_not_found on_create) }

=head2 new

Instantiate from an ID or a pre-fabricated object (hashref).

=over 4

=item * Create a new, empty object:

 EntityModel::Support::Perl::Base->new(1)

lib/EntityModel/Support/Perl/Base.pm  view on Meta::CPAN

 EntityModel::Support::Perl::Base->new(1)
 EntityModel::Support::Perl::Base->new('123-456')
 EntityModel::Support::Perl::Base->new([123,456])

=item * Create an object and assign initial values:

 EntityModel::Support::Perl::Base->new({ x => 1, y => 2 })

=back

Any remaining options indicate callbacks:

=over 4

=item * before_commit - just before commit

=item * after_commit - after this has been committed to the database

=item * on_load - when the data has been read from storage

=item * on_not_found - when storage reports that this item is not found

=back

The before_XXX callbacks are also aliased to on_XXX for convenience.

=cut

sub new {
	my $class = shift;
	my $spec = shift || {};
	my %args = @_;

	my %opt;
	my $self = bless {
		_incomplete	=> 1
	}, $class;
	return $self if $args{pending};

# Now we might want to provide some callbacks
	while(my ($k, $v) = each %args) {
		if($k eq 'create') {
			$opt{create} = $v ? 1 : 0;
		} elsif($k ~~ $class->_supported_callbacks) {
			$self->{_callback}->{$k} = $v;
		} else {
			warn "Unknown callback $k requested";
		}
	}

# An arrayref or plain value is used as an ID
	if(!ref($spec) || ref($spec) eq 'ARRAY') {
		$class->_storage->read(
			entity		=> $class->_entity,

lib/EntityModel/Support/Perl/Base.pm  view on Meta::CPAN

	my $self = shift;
	while(@_) {
		my ($evt, $task) = splice @_, 0, 2;
		push @{$self->{_task_pending}->{$evt}}, $task;
	}
	return $self;
}

=head2 _event

Pass the given event through to any defined callbacks.

=cut

sub _event {
	my $self = shift;
	my $ev = shift;
	if(my $task = shift @{$self->{_task_pending}->{$ev}}) {
		$task->($self, @_);
	}

lib/EntityModel/Support/Perl/Base.pod  view on Meta::CPAN

 EntityModel::Support::Perl::Base->new(1)
 EntityModel::Support::Perl::Base->new('123-456')
 EntityModel::Support::Perl::Base->new([123,456])

=item * Create an object and assign initial values:

 EntityModel::Support::Perl::Base->new({ x => 1, y => 2 })

=back

Any remaining options indicate callbacks:

=over 4

=item * before_commit - just before commit

=item * after_commit - after this has been committed to the database

=item * on_load - when the data has been read from storage

=item * on_not_found - when storage reports that this item is not found

=back

The before_XXX callbacks are also aliased to on_XXX for convenience.

=head2 _queue_task

Queues a new one-shot task for the given event type.

Supports the following event types:

=over 4

=item * on_load - data has been read from storage

lib/EntityModel/Support/Perl/Base.pod  view on Meta::CPAN

=item * on_update - values have been updated, but not necessarily written to storage

=item * on_remove - this entry has been removed from storage

=item * on_not_found - could not find this entry in backend storage

=back

=head2 _event

Pass the given event through to any defined callbacks.

=head2 _spec_from_hashref

Private method to generate hashref containing spec information suitable for bless to requested class,
given a hashref which represents the keys/values for the object.

This will flatten any Entity objects down to the required ID key+values.

=head2 create



( run in 0.389 second using v1.01-cache-2.11-cpan-9b1e4054eb1 )