view release on metacpan or search on metacpan
lib/Elastic/Model/Role/Model.pm view on Meta::CPAN
$Elastic::Model::Role::Model::VERSION = '0.52';
use Moose::Role;
use Carp;
use Elastic::Model::Types qw(ES);
use Search::Elasticsearch 1.20 ();
use Class::Load qw(load_class);
use Moose::Util qw(does_role);
use MooseX::Types::Moose qw(:all);
use Elastic::Model::UID();
use Elastic::Model::Deleted();
use Scalar::Util qw(blessed refaddr weaken);
use List::MoreUtils qw(uniq);
use JSON();
our $JSON = JSON->new->canonical->utf8;
use namespace::autoclean;
my @wrapped_classes = qw(
domain namespace store view scope
results cached_results scrolled_results result bulk
);
lib/Elastic/Model/Role/Model.pm view on Meta::CPAN
#===================================
my $self = shift;
my $class = shift;
load_class($class);
my $meta
= Moose::Meta::Class->create(
Class::MOP::class_of($self)->wrapped_class_name($class),
superclasses => [$class] );
weaken( my $weak_model = $self );
$meta->add_method( model => sub {$weak_model} );
$meta->add_method( original_class => sub {$class} );
$meta->make_immutable;
return $meta->name;
}
#===================================
sub domain {
#===================================
lib/Elastic/Model/Role/Model.pm view on Meta::CPAN
$scope = $model->new_scope();
Creates a new L<Elastic::Model::Scope> instance (in-memory cache). If there is
an existing scope, then the new scope inherits from the existing scope.
$scope = $model->new_scope(); # scope_1
$scope = $model->new_scope(); # scope_2, inherits from scope_1
undef $scope; # scope_2 and scope_1 are destroyed
Scopes are optional unless you have attributes which are weakened.
See L<Elastic::Model::Scoping> and L<Elastic::Model::Scope> to read more about
how scopes work.
=head1 OTHER METHODS AND ATTRIBUTES
These methods and attributes, while public, are usually used only by internal
modules. They are documented here for completeness.
=head2 CRUD
lib/Elastic/Model/TypeMap/Base.pm view on Meta::CPAN
if ( ref $inflator ) {
$inflator = '$inflators{"' . $name . '"}->($val)';
}
push @src,
(
'$attr = "' . $name . '";',
'if (exists $hash->{"' . $name . '"}) {',
'$val = $hash->{"' . $name . '"};',
'$res = ' . $inflator . ';',
$set_raw . ';',
$attr->_inline_weaken_value( '$self', '$res' ),
'}'
);
}
push @src,
(
'1}',
'or die "Error inflating attribute ($attr) in class ".',
'Scalar::Util::blessed($self).',
'":\n ".($@ || "Unknown error");',
'return $self',
lib/Elastic/Model/TypeMap/Objects.pm view on Meta::CPAN
package Elastic::Model::TypeMap::Objects;
$Elastic::Model::TypeMap::Objects::VERSION = '0.52';
use strict;
use warnings;
use Elastic::Model::TypeMap::Base qw(:all);
use Scalar::Util qw(reftype weaken);
use Moose::Util qw(does_role);
use namespace::autoclean;
#===================================
has_type 'Moose::Meta::TypeConstraint::Class',
#===================================
deflate_via { _deflate_class(@_) },
inflate_via { _inflate_class(@_) },
map_via { _map_class(@_) };
lib/Elastic/Model/TypeMap/Objects.pm view on Meta::CPAN
}
#===================================
sub _inflate_class {
#===================================
my ( $tc, $attr, $map ) = @_;
my $class = $tc->name;
if ( $map->model->knows_class($class) ) {
my $model = $map->model;
weaken $model;
return sub {
my $hash = shift;
die "Missing UID\n" unless $hash->{uid};
my $uid = Elastic::Model::UID->new( %{ $hash->{uid} },
from_store => 1 );
return $model->get_doc( uid => $uid );
};
}
my $attrs = _class_attrs( $map, $class, $attr )
t/50_scope/01_get.t view on Meta::CPAN
#!/usr/bin/perl
use strict;
use warnings;
use Test::More 0.96;
use Test::Exception;
use Scalar::Util qw(refaddr weaken);
use lib 't/lib';
our $es;
do 'es.pl';
use_ok 'MyApp' || print 'Bail out';
use Elastic::Model::Role::Store();
my $store_get = 0;
t/50_scope/02_store.t view on Meta::CPAN
#!/usr/bin/perl
use strict;
use warnings;
use Test::More 0.96;
use Test::Exception;
use Scalar::Util qw(refaddr weaken);
use lib 't/lib';
our $es;
do 'es.pl';
use_ok 'MyApp' || print 'Bail out';
my $model = new_ok( 'MyApp', [ es => $es ], 'Model' );
t/50_scope/02_store.t view on Meta::CPAN
undef $scope_2;
isa_ok $scope_2 = $model->new_scope, 'Elastic::Model::Scope',
'Scope_2 renewed';
isa_ok $u2 = $domain->get( user => 1 ), 'MyApp::User', 'U2 in new scope';
is $u2->name, 'Mary', 'Force U2 inflation';
$u1->name('Felix');
ok $u1->save, 'U1 updated';
compare( "U1-updated and U2-inflate", $u1, $u2, 0, 0 );
# Older version still alive
weaken $u2;
ok $u2, 'Weakened U2 still exists';
# Newer version now current version
isa_ok $u2 = $domain->get( user => 1 ), 'MyApp::User', 'U2 in saved scope';
compare( "U1-saved and U2-same-scope", $u1, $u2, 'same_obj' );
# Done
done_testing;
#===================================
t/50_scope/03_delete.t view on Meta::CPAN
#!/usr/bin/perl
use strict;
use warnings;
use Test::More 0.96;
use Test::Exception;
use Scalar::Util qw(refaddr weaken);
use lib 't/lib';
our $es;
do 'es.pl';
use_ok 'MyApp' || print 'Bail out';
my $model = new_ok( 'MyApp', [ es => $es ], 'Model' );
t/50_scope/04_search.t view on Meta::CPAN
#!/usr/bin/perl
use strict;
use warnings;
use Test::More 0.96;
use Test::Exception;
use Scalar::Util qw(refaddr weaken);
use lib 't/lib';
our $es;
do 'es.pl';
use_ok 'MyApp' || print 'Bail out';
my $model = new_ok( 'MyApp', [ es => $es ], 'Model' );