File Coverage

blib/lib/ElasticSearchX/Model/Trait/Class.pm
Criterion Covered Total %
statement 11 11 100.0
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 15 15 100.0


line stmt bran cond sub pod time code
1             #
2             # This file is part of ElasticSearchX-Model
3             #
4             # This software is Copyright (c) 2016 by Moritz Onken.
5             #
6             # This is free software, licensed under:
7             #
8             # The (three-clause) BSD License
9             #
10             package ElasticSearchX::Model::Trait::Class;
11             $ElasticSearchX::Model::Trait::Class::VERSION = '1.0.2';
12 11     11   24489 use Moose::Role;
  11         17  
  11         78  
13 11     11   37320 use List::Util ();
  11         16  
  11         161  
14 11     11   37 use Carp;
  11         13  
  11         2820  
15              
16             sub _handles {
17 44     44   47 my ( $s, $p ) = @_;
18             return {
19 44         258 "get_$s" => 'get',
20             "get_$p" => 'values',
21             "get_${s}_list" => 'keys',
22             "remove_$s" => 'delete',
23             "add_$s" => 'set',
24             };
25             }
26             my %foo = (
27             analyzer => 'analyzers',
28             tokenizer => 'tokenizers',
29             filter => 'filters'
30             );
31             while ( my ( $s, $p ) = each %foo ) {
32             has $p => (
33             traits => ['Hash'],
34             isa => 'HashRef',
35             default => sub { {} },
36             handles => _handles( $s, $p )
37             );
38             }
39              
40             has indices => (
41             traits => ['Hash'],
42             isa => 'HashRef',
43             default => sub { { default => {} } },
44             handles => _handles( 'index', 'indices' )
45             );
46              
47             before add_index => sub {
48             my ( $self, $name, $index ) = @_;
49             $self->remove_index('default');
50             if ( $index->{alias_for} && $name ne $index->{alias_for} ) {
51             return $self->add_index( $index->{alias_for}, $index );
52             }
53             };
54              
55             1;
56              
57             __END__
58              
59             =pod
60              
61             =encoding UTF-8
62              
63             =head1 NAME
64              
65             ElasticSearchX::Model::Trait::Class
66              
67             =head1 VERSION
68              
69             version 1.0.2
70              
71             =head1 AUTHOR
72              
73             Moritz Onken
74              
75             =head1 COPYRIGHT AND LICENSE
76              
77             This software is Copyright (c) 2016 by Moritz Onken.
78              
79             This is free software, licensed under:
80              
81             The (three-clause) BSD License
82              
83             =cut