File Coverage

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