File Coverage

lib/ElasticSearchX/Model/Generator/DocumentGenerator.pm
Criterion Covered Total %
statement 17 19 89.4
branch n/a
condition n/a
subroutine 7 7 100.0
pod n/a
total 24 26 92.3


line stmt bran cond sub pod time code
1 1     1   1108 use strict;
  1         1  
  1         40  
2 1     1   7 use warnings;
  1         2  
  1         62  
3              
4             package ElasticSearchX::Model::Generator::DocumentGenerator;
5             BEGIN {
6 1     1   32 $ElasticSearchX::Model::Generator::DocumentGenerator::AUTHORITY = 'cpan:KENTNL';
7             }
8             {
9             $ElasticSearchX::Model::Generator::DocumentGenerator::VERSION = '0.1.8';
10             }
11              
12             # ABSTRACT: Moose Class generation back end for Documents/Types.
13              
14 1     1   14 use 5.10.0;
  1         4  
  1         37  
15 1     1   780 use Moo;
  1         18978  
  1         7  
16 1     1   2945 use Data::Dump qw( pp );
  1         5853  
  1         80  
17 1     1   441 use MooseX::Has::Sugar qw( rw required weak_ref );
  0            
  0            
18              
19              
20             has 'generator_base' => rw, required, weak_ref, handles => [qw( attribute_generator typename_translator )];
21              
22              
23             sub generate {
24             my ( $self, %args ) = @_;
25              
26             my $class = $self->typename_translator->translate_to_package( typename => $args{typename} );
27             my $path = $self->typename_translator->translate_to_path( typename => $args{typename} );
28             my @attributes;
29             for my $property_name ( sort { $a cmp $b } $self->generator_base->property_names( $args{index}, $args{typename} ) ) {
30             my $property = $self->generator_base->property( $args{index}, $args{typename}, $property_name );
31             push @attributes,
32             $self->attribute_generator->generate(
33             index => $args{index},
34             typename => $args{typename},
35             propertyname => $property_name,
36             propertydata => $property,
37             );
38             }
39              
40             require ElasticSearchX::Model::Generator::Generated::Document;
41             return ElasticSearchX::Model::Generator::Generated::Document->new(
42             package => $class,
43             path => $path,
44             content => $self->_fill_template(
45             class => $class,
46             typename => $args{typename},
47             attributes => ( join qq{\n}, map { $_->content } @attributes )
48             ),
49             );
50             }
51              
52              
53             sub _template_string {
54             return state $document_template = <<'EOF';
55             use strict;
56             use warnings FATAL => 'all';
57              
58             package %s;
59              
60             # ABSTRACT: Generated model for %s
61              
62             use Moose;
63             use ElasticSearchX::Model::Document v0.1.5;
64              
65             %s
66              
67             no Moose;
68             __PACKAGE__->meta->make_immutable;
69              
70             1;
71              
72             EOF
73              
74             }
75              
76              
77             sub _fill_template {
78             my ( $self, %args ) = @_;
79             return sprintf
80             $self->_template_string,
81             $args{class},
82             $args{typename},
83             $args{attributes};
84             }
85              
86             no Moo;
87              
88             1;
89              
90             __END__
91              
92             =pod
93              
94             =encoding UTF-8
95              
96             =head1 NAME
97              
98             ElasticSearchX::Model::Generator::DocumentGenerator - Moose Class generation back end for Documents/Types.
99              
100             =head1 VERSION
101              
102             version 0.1.8
103              
104             =head1 METHODS
105              
106             =head2 generate
107              
108             $generated_document = $documentgenerator->generate(
109             index => ... Name of current index ...
110             typename => ... Name of the type we're generating ...
111             );
112             $generated_document->isa(ElasticSearchX::Model::Generator::Generated::Document);
113              
114             =head1 ATTRIBUTES
115              
116             =head2 generator_base
117              
118             rw, required, weak_ref
119              
120             =head1 PRIVATE METHODS
121              
122             =head2 _template_string
123              
124             =head2 _fill_template
125              
126             =head1 AUTHOR
127              
128             Kent Fredric <kentfredric@gmail.com>
129              
130             =head1 COPYRIGHT AND LICENSE
131              
132             This software is copyright (c) 2013 by Kent Fredric <kentfredric@gmail.com>.
133              
134             This is free software; you can redistribute it and/or modify it under
135             the same terms as the Perl 5 programming language system itself.
136              
137             =cut