EntityModel

 view release on metacpan or  search on metacpan

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


see L<EntityModel>.

=head1 DESCRIPTION

see L<EntityModel>.

=cut

use EntityModel;
use Scalar::Util qw(weaken);

=head1 METHODS

=cut

=pod

(the following is likely to be very inaccurate)

Each plugin can register for a type:

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


=cut

sub new {
	my $class = shift;
	my $model = shift;
	my $self = bless {
		model => $model
	}, $class;

	weaken $self->{model};
#	$self->setup(@_);
	return $self;
}

sub setup { die "Virtual method setup called for " . $_[0]; }

sub unload { $_[0]; }

sub publish {
	my $self = shift;

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

=head2 transaction_start

Mark the start of a transaction.

=cut

sub transaction_start {
	my $self = shift;
	my $tran = shift;

# TODO weaken?
	$self->transaction->push($tran);
	return $self;
}

=head2 transaction_rollback

Roll back a transaction.

=cut

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


Subclassed instantiation, requires an L<IO::Async::Loop> passed as the C<loop> named parameter.

=cut

sub setup {
	my $self = shift;
	my %args = %{+shift};
	my $loop = delete $args{loop} or die "No IO::Async::Loop provided?";
	$self->SUPER::setup(\%args);
	Scalar::Util::weaken($self->{loop} = $loop);
	return $self;
}

=head2 read

Reads the data for the given entity and returns hashref with the appropriate data.

Parameters:

=over 4

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

	$self
}

sub DESTROY {
	my $self = shift;
	warn "did not finish" unless $self->{committed} || $self->{rolled_back};
}

sub sap {
	my ($self, $sub) = @_;
	Scalar::Util::weaken $self;
	return sub {
		$self->$sub(@_);
	};
}

1;

__END__

=head1 AUTHOR

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

);

sub with_model(&;@) {
	my $code = shift;
	my %args = @_;
	my $def = $model_data{$args{model} || 'simple'} or die 'unknown model - ' . $args{model};

	my $weak_model;
	{
		my $model = EntityModel->default_model;
		Scalar::Util::weaken($weak_model = $model);
		$model->add_storage(
			# TODO extract this
			'PerlAsync' => { loop => my $loop = IO::Async::Loop->new },
		);
		$model->name($def->{name});
		foreach my $entity_def (@{$def->{entity} || []}) {
			$model->create_entity(%$entity_def);
		}
		$code->($model);
		$model->remove_entity($_) for $model->entity->list;

t/collection.t  view on Meta::CPAN

use EntityModel::Collection;

note 'Basic tests first';
# We keep a copy of this test in $basic so we can reuse it for subclasses later
subtest 'Basic EntityModel::Collection functionality' => (my $basic = sub {
	my ($class, $extra) = @_;
	sub {
		plan tests => 26 + $extra;
		# Instantiate, check some methods and overloads exist
		my $c = new_ok($class => [ ]);
		Scalar::Util::weaken(my $weak_c = $c);
		can_ok($c, qw{done fail commit each add_handler has_pending});
		is(ref(\&{$c}), 'CODE', 'can use as a coderef');

		my $v = 17;
		my $committed = 0;
		my $fail = 0;
		my $post_check = sub {
			is($v, 17, '$v is unchanged before commit');
			is($committed, 0, 'not committed yet');
			is($fail, 0, 'no failures seen');

t/transaction.t  view on Meta::CPAN

 }, goodbye => sub {
  pass("next step");
 };
};

subtest 'refcounts' => sub {
 plan tests => 26;
 {
  my $weak_tran;
  my $tran = as_transaction {
   Scalar::Util::weaken($weak_tran = shift);
  };
  is_oneref($tran, 'have single ref for transaction');
  ok($weak_tran, 'weak transaction still alive');
  ok($weak_tran, 'weak transaction still alive');
  $weak_tran->();
  is_oneref($tran, 'still single ref for transaction');
  $tran->commit;
  is_oneref($tran, 'still single ref for transaction');
  undef $tran;
  is($weak_tran, undef, 'weak copy disappeared');
 }

 {
  my $weak_tran;
  my $tran = as_transaction {
   is_refcount($_[0], 2, 'have expected refcount for transaction in transaction handling code');
   Scalar::Util::weaken($weak_tran = shift);
  }  success => sub { is_refcount($_[0], 3, 'refcount correct in success callback') }
  ,  failure => sub { fail("why the failure?") }
  ,  goodbye => sub { is_refcount($_[0], 3, 'refcount correct in goodbye callback') };
  is_oneref($tran, 'have single ref for transaction');
  ok($weak_tran, 'weak transaction still alive');
  ok($weak_tran->(), 'can apply transaction');
  ok($weak_tran, 'weak transaction still alive');
  is_oneref($tran, 'still single ref for transaction');
  ok($tran->commit, 'can commit');
  is_oneref($tran, 'still single ref for transaction');
 }
 {
  my $weak_tran;
  my $tran = as_transaction {
   is_refcount($_[0], 2, 'have expected refcount for transaction in transaction handling code');
   Scalar::Util::weaken($weak_tran = shift);
   die;
  }  success => sub { fail("should not succeed?") },
  ,  failure => sub { is_refcount($_[0], 3, 'refcount correct in failure callback') }
  ,  goodbye => sub { is_refcount($_[0], 3, 'refcount correct in goodbye callback') };
  is_oneref($tran, 'have single ref for transaction');
  ok($weak_tran, 'weak transaction still alive');
  ok($weak_tran->(), 'can apply transaction');
  ok($weak_tran, 'weak transaction still alive');
  is_oneref($tran, 'still single ref for transaction');
  ok($tran->commit, 'can commit');



( run in 0.478 second using v1.01-cache-2.11-cpan-65fba6d93b7 )