File Coverage

blib/lib/ElasticSearchX/Model/Document/Types.pm
Criterion Covered Total %
statement 48 48 100.0
branch n/a
condition n/a
subroutine 16 16 100.0
pod n/a
total 64 64 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) 2019 by Moritz Onken.
5             #
6             # This is free software, licensed under:
7             #
8             # The (three-clause) BSD License
9             #
10             package ElasticSearchX::Model::Document::Types;
11             $ElasticSearchX::Model::Document::Types::VERSION = '2.0.1';
12 14     14   216091 use List::MoreUtils ();
  14         157806  
  14         410  
13 14     14   6380 use DateTime::Format::Epoch::Unix;
  14         6792281  
  14         571  
14 14     14   7441 use DateTime::Format::ISO8601;
  14         1193505  
  14         791  
15 14     14   8608 use MooseX::Attribute::Deflator;
  14         4271157  
  14         98  
16 14     14   22805 use MooseX::Attribute::Deflator::Moose;
  14         219692  
  14         563  
17 14     14   117 use DateTime;
  14         40  
  14         447  
18 14     14   6118 use JSON::MaybeXS qw( decode_json encode_json );
  14         17831  
  14         904  
19 14     14   118 use Scalar::Util qw(blessed);
  14         31  
  14         710  
20 14     14   6010 use MooseX::Types::ElasticSearch qw(:all);
  14         1766340  
  14         95  
21 14     14   41271 use Moose::Util::TypeConstraints qw(duck_type);
  14         46  
  14         99  
22              
23 14         97 use MooseX::Types -declare => [
24             qw(
25             Type
26             Types
27             TimestampField
28             TTLField
29             ESBulk
30             ESScroll
31             )
32 14     14   6956 ];
  14         45  
33              
34 14         171 use Sub::Exporter -setup => {
35             exports => [
36             qw(
37             Location
38             QueryType
39             ES
40             ESBulk
41             ESScroll
42             Type
43             Types
44             TimestampField
45             TTLField
46             )
47             ]
48 14     14   99490 };
  14         66  
49              
50 14     14   7415 use MooseX::Types::Moose qw/Int Str Bool ArrayRef HashRef/;
  14         44  
  14         77  
51 14     14   90410 use MooseX::Types::Structured qw(Dict Tuple Optional slurpy);
  14         5278944  
  14         140  
52              
53             subtype TimestampField,
54             as Dict [
55             enabled => Bool,
56             path => Optional [Str],
57             index => Optional [Str],
58             slurpy HashRef,
59             ];
60             coerce TimestampField, from Int, via {
61             { enabled => 1 };
62             };
63             coerce TimestampField, from Str, via {
64             { enabled => 1, path => $_ };
65             };
66             coerce TimestampField, from HashRef, via {
67             { enabled => 1, %$_ };
68             };
69              
70             subtype ESScroll,
71             as duck_type([qw(
72             next
73             total
74             max_score
75             )]);
76              
77             subtype ESBulk,
78             as duck_type([qw(
79             _buffer_count
80             flush
81             )]);
82              
83             subtype TTLField,
84             as Dict [
85             enabled => Bool,
86             default => Optional [Str],
87             store => Optional [Bool],
88             index => Optional [Str],
89             slurpy HashRef,
90             ];
91             coerce TTLField, from Int, via {
92             { enabled => 1 };
93             };
94             coerce TTLField, from Str, via {
95             { enabled => 1, default => $_ };
96             };
97             coerce TTLField, from HashRef, via {
98             { enabled => 1, %$_ };
99             };
100              
101             class_type 'DateTime';
102             coerce 'DateTime', from Int, via {
103             DateTime->from_epoch( epoch => $_ / 1000 );
104             };
105             coerce 'DateTime', from Str, via {
106             DateTime::Format::ISO8601->parse_datetime($_);
107             };
108              
109             subtype Types, as HashRef ['Object'], where {
110             !grep { $_->isa('Moose::Meta::Class') } keys %$_;
111             }, message {
112             "Types must be either an ArrayRef of class names or a HashRef of name/class name pairs";
113             };
114              
115             coerce Types, from HashRef ['Str'], via {
116             my $hash = $_;
117             return {
118             map { $_ => Class::MOP::Class->initialize( $hash->{$_} ) }
119             keys %$hash
120             };
121             };
122              
123             coerce Types, from ArrayRef ['Str'], via {
124             my $array = $_;
125             return {
126             map {
127             my $meta = Class::MOP::Class->initialize($_);
128             $meta->short_name => $meta
129             } @$array
130             };
131             };
132              
133             my $REGISTRY = Moose::Util::TypeConstraints->get_type_constraint_registry;
134              
135             $REGISTRY->add_type_constraint(
136             Moose::Meta::TypeConstraint::Parameterizable->new(
137             name => Type,
138             package_defined_in => __PACKAGE__,
139             parent => find_type_constraint('Object'),
140             constraint_generator => sub {
141             sub {
142             blessed $_
143             && $_->can('_does_elasticsearchx_model_document_role');
144             }
145             },
146             )
147             );
148              
149             Moose::Util::TypeConstraints::add_parameterizable_type(
150             $REGISTRY->get_type_constraint(Type) );
151              
152 14     14   18035 use MooseX::Attribute::Deflator;
  14         42  
  14         172  
153             my @stat
154             = qw(dev ino mode nlink uid gid rdev size atime mtime ctime blksize blocks);
155             deflate 'File::stat', via { return { List::MoreUtils::mesh( @stat, @$_ ) } },
156             inline_as {
157             join( "\n",
158             'my @stat = qw(dev ino mode nlink uid gid',
159             'rdev size atime mtime ctime blksize blocks);',
160             'List::MoreUtils::mesh( @stat, @$value )',
161             );
162             };
163             deflate [ 'ArrayRef', 'HashRef' ],
164             via { shift->dynamic ? $_ : encode_json($_) }, inline_as {
165             return '$value' if ( $_[0]->dynamic );
166             return 'JSON::encode_json($value)';
167             };
168             inflate [ 'ArrayRef', 'HashRef' ],
169             via { shift->dynamic ? $_ : decode_json($_) }, inline_as {
170             return '$value' if ( $_[0]->dynamic );
171             return 'JSON::decode_json($value)';
172             };
173              
174             deflate 'ArrayRef', via {$_}, inline_as {'$value'};
175             inflate 'ArrayRef', via {$_}, inline_as {'$value'};
176              
177             deflate 'DateTime', via { $_->iso8601 }, inline_as {'$value->iso8601'};
178             inflate 'DateTime', via {
179             $_ =~ /^\d+$/
180             ? DateTime->from_epoch( epoch => $_ / 1000 )
181             : DateTime::Format::ISO8601->parse_datetime($_);
182             }, inline_as {
183             q(
184             $value =~ /^\d+$/
185             ? DateTime->from_epoch(epoch => $value/1000)
186             : DateTime::Format::ISO8601->parse_datetime($value)
187             )
188             };
189             deflate Location, via { [ $_->[0] + 0, $_->[1] + 0 ] },
190             inline_as {'[ $value->[0] + 0, $value->[1] + 0 ]'};
191             deflate Type . '[]', via { ref $_ eq 'HASH' ? $_ : $_->meta->get_data($_) },
192             inline_as {
193             'ref $value eq "HASH" ? $value : $value->meta->get_data($value)';
194             };
195              
196 14     14   20827 no MooseX::Attribute::Deflator;
  14         49  
  14         101  
197              
198             1;
199              
200             __END__
201              
202             =pod
203              
204             =encoding UTF-8
205              
206             =head1 NAME
207              
208             ElasticSearchX::Model::Document::Types
209              
210             =head1 VERSION
211              
212             version 2.0.1
213              
214             =head1 AUTHOR
215              
216             Moritz Onken
217              
218             =head1 COPYRIGHT AND LICENSE
219              
220             This software is Copyright (c) 2019 by Moritz Onken.
221              
222             This is free software, licensed under:
223              
224             The (three-clause) BSD License
225              
226             =cut