| 1 | package Template::Lace::Factory::InferInitArgsRole; | ||
| 2 | |||
| 3 | use Moo::Role; | ||
| 4 | use Scalar::Util; | ||
| 5 | |||
| 6 | has 'fields' => ( | ||
| 7 | is=>'ro', | ||
| 8 | required=>1, | ||
| 9 | default=>sub {[]}); | ||
| 10 | |||
| 11 | around BUILDARGS => sub { | ||
| 12 | my ($orig, $class, @args) = @_; | ||
| 13 | my $args = $class->$orig(@args); | ||
| 14 | $args->{fields} = $class->find_fields($args->{model_class}); | ||
| 15 | return $args; | ||
| 16 | }; | ||
| 17 | |||
| 18 | # Might need something better here eventually. Not sure | ||
| 19 | # we want to allow to infer any init argument, just the | ||
| 20 | # ones aimed to be in the render of the template. might | ||
| 21 | # need a 'has_content', 'has_node', etc? | ||
| 22 | |||
| 23 | # Please Note obviously this requires Moose | ||
| 24 | |||
| 25 | sub find_fields { | ||
| 26 | my ($class, $model_class) = @_; | ||
| 27 | return map { $_->init_arg } | ||
| 28 | grep { $_->has_init_arg } | ||
| 29 | grep { $_->name ne 'ctx' } | ||
| 30 | grep { $_->name ne 'catalyst_component_name' } | ||
| 31 | ($model_class->meta->get_all_attributes); | ||
| 32 | } | ||
| 33 | |||
| 34 | around 'prepare_args', sub { | ||
| 35 | my ($orig, $self, @args) = @_; | ||
| 36 | my %args = $self->infer_args_from(@args); | ||
| 37 | return $self->$orig(%args); | ||
| 38 | }; | ||
| 39 | |||
| 40 | sub infer_args_from { | ||
| 41 | my $self = shift; | ||
| 42 | my %args = (); | ||
| 43 | my @fields = @{$self->fields}; | ||
| 44 | |||
| 45 | # If the first argment is an object or a hashref then | ||
| 46 | # we inspect it and unroll any matching fields. This | ||
| 47 | # is to allow a more ease of use for the view call (and | ||
| 48 | # it enables a few other things ). | ||
| 49 | |||
| 50 | if(Scalar::Util::blessed($_[0])) { | ||
| 51 | my $init_object = shift @_; | ||
| 52 | %args = map { $_ => $init_object->$_ } | ||
| 53 | grep { $init_object->can($_) } | ||
| 54 | @fields; | ||
| 55 | } | ||
| 56 | |||
| 57 | return (%args, @_); | ||
| 58 | } | ||
| 59 | |||
| 60 | 1; | ||
| 61 | |||
| 62 | =head1 NAME | ||
| 63 | |||
| 64 | Template::Lace::Factory::InferInitArgsRole - fill init args by inspecting an object | ||
| 65 | |||
| 66 | =head1 SYNOPSIS | ||
| 67 | |||
| 68 | Create a template class: | ||
| 69 | |||
| 70 | package MyApp::Template::User; | ||
| 71 | |||
| 72 | use Moo; | ||
| 73 | with 'Template::Lace::ModelRole', | ||
| 74 | 'Template::Lace::Factory::InferInitArgsRole', | ||
| 75 | |||
| 76 | has [qw/age name motto/] => (is=>'ro', required=>1); | ||
| 77 | |||
| 78 | sub template {q[ | ||
| 79 | <html> | ||
| 80 | <head> | ||
| 81 | <title>User Info</title> | ||
| 82 | </head> | ||
| 83 | <body> | ||
| 84 | <dl id='user'> | ||
| 85 | <dt>Name</dt> | ||
| 86 | <dd id='name'>NAME</dd> | ||
| 87 | <dt>Age</dt> | ||
| 88 | <dd id='age'>AGE</dd> | ||
| 89 | <dt>Motto</dt> | ||
| 90 | <dd id='motto'>MOTTO</dd> | ||
| 91 | </dl> | ||
| 92 | </body> | ||
| 93 | </html> | ||
| 94 | ]} | ||
| 95 | |||
| 96 | sub process_dom { | ||
| 97 | my ($self, $dom) = @_; | ||
| 98 | $dom->dl('#user', +{ | ||
| 99 | age=>$self->age, | ||
| 100 | name=>$self->name, | ||
| 101 | motto=>$self->motto | ||
| 102 | }); | ||
| 103 | } | ||
| 104 | |||
| 105 | 1; | ||
| 106 | |||
| 107 | Create an object; | ||
| 108 | |||
| 109 | package MyApp::User; | ||
| 110 | |||
| 111 | has [qw/age name motto/] => (is=>'ro', required=>1); | ||
| 112 | |||
| 113 | 1; | ||
| 114 | |||
| 115 | my $user = MyApp::User->new(age=>42,name=>'Joe', motto=>'Why?'); | ||
| 116 | |||
| 117 | Use the object to create a render instance for your template: | ||
| 118 | |||
| 119 | my $factory = Template::Lace::Factory->new( | ||
| 120 | model_class=>'MyApp::Template::User'); | ||
| 121 | |||
| 122 | my $renderer = $factory->create($user); | ||
| 123 | |||
| 124 | print $renderer->render; | ||
| 125 | |||
| 126 | Outputs: | ||
| 127 | |||
| 128 | <html> | ||
| 129 | <head> | ||
| 130 | <title> | ||
| 131 | User Info | ||
| 132 | </title> | ||
| 133 | </head> | ||
| 134 | <body id="body"> | ||
| 135 | <dl id="user"> | ||
| 136 | <dt> | ||
| 137 | Name | ||
| 138 | </dt> | ||
| 139 | <dd id="name"> | ||
| 140 | Joe | ||
| 141 | </dd> | ||
| 142 | <dt> | ||
| 143 | Age | ||
| 144 | </dt> | ||
| 145 | <dd id="age"> | ||
| 146 | 42 | ||
| 147 | </dd> | ||
| 148 | <dt> | ||
| 149 | Motto | ||
| 150 | </dt> | ||
| 151 | <dd id="motto"> | ||
| 152 | Why? | ||
| 153 | </dd> | ||
| 154 | </dl> | ||
| 155 | </body> | ||
| 156 | </html> | ||
| 157 | |||
| 158 | =head1 DESCRIPTION | ||
| 159 | |||
| 160 | Allows you to fill your template arguments from an object, possibily saving you some | ||
| 161 | tedious typing (at the possible expense of understanding). | ||
| 162 | |||
| 163 | In the (often likely) case that there is great interface compatibility between your | ||
| 164 | business objects and template models, this role can save you the effort of writing a lot | ||
| 165 | of tendious mapping code. Can save a lot when the interfaces contain a ton of fields. | ||
| 166 | However you are reducing possible comprehension as well as introducing a possible evil | ||
| 167 | interface binding into your code. The choice is yours :) | ||
| 168 | |||
| 169 | B<NOTE> This works by using L<Moose> and assumes your classes are written with L<Moo> | ||
| 170 | or L<Moose>. This means you are adding a dependency on L<Moose> into your project | ||
| 171 | that you may not want. You will also inflate a meta object on your L<Moo> class which | ||
| 172 | may have some performance and memory usage implications. | ||
| 173 | |||
| 174 | =head1 SEE ALSO | ||
| 175 | |||
| 176 | L<Template::Lace>. | ||
| 177 | |||
| 178 | =head1 AUTHOR | ||
| 179 | |||
| 180 | Please See L<Template::Lace> for authorship and contributor information. | ||
| 181 | |||
| 182 | =head1 COPYRIGHT & LICENSE | ||
| 183 | |||
| 184 | Please see L<Template::Lace> for copyright and license information. | ||
| 185 | |||
| 186 | =cut |