CatalystX/Moose.pm untested

filter: Statementstmt 0.0% 0 / 128 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 192 CC 9 SCAR 27.8
CC
9
Cov
0%
_process_shared
42.0
_process_context
20.0
register_implementation
2.0
CRAP 16.2
1 package MooseX::Attribute::Catalyst::Shared;
2
3 use Moose::Role;
4  
5 has 'shared' => (is=>'ro', predicate=>'has_shared');
6  
7 around '_process_options' => sub {
8 my ($orig, $self, $name, $options) = (@_);
9 $options = $self->_process_shared($name, $options)
10 if exists($options->{shared});
11 return $self->$orig($name, $options);
12 };
13  
14 # When an attibute is shared, set its default to the value in the shared
15 # context key, if it exists UNLESS the programmer is setting a default or
16 # builder manually.
17  
18 sub _process_shared {
19 my ($self, $name, $options) = @_;
20 unless($options->{default} || $options->{builder}) {
21 $options->{lazy} = 1;
22 $options->{default} = sub {
23 my $self = shift;
24 my $ctx = $self->ctx; # This only works for per-request controllers
25 return $ctx->stash->{__MXACShared}{$name}
26 if exists($ctx->stash->{__MXACShared}{$name});
27 };
28 return $options;
29 }
30 if(my $default = $options->{default}) {
31 $options->{lazy} = 1;
32 $options->{default} = sub {
33 my $self = shift;
34 my $ctx = $self->ctx; # This only works for per-request controllers
35 my $default_value = ref($default) ? $default->($self) : $default;
36
37 $ctx->stash->{__MXACShared}{$name} = $default_value;
38  
39 return $default_value;
40 };
41 }
42 # if there's a builder, around it and store in the stash
43 if(my $builder = delete $options->{builder}) {
44 $options->{lazy} = 1;
45 $options->{default} = sub {
46 my $self = shift;
47 my $ctx = $self->ctx; # This only works for per-request controllers
48 my $default_value = $self->$builder;
49
50 $ctx->stash->{__MXACShared}{$name} = $default_value;
51  
52 return $default_value;
53 };
54  
55 }
56 return $options;
57 }
58  
59 # If someone tries to update a shared attribute, sync that with the stash
60  
61 around 'install_accessors' => sub {
62 my $orig = shift;
63 my $attr = shift;
64 my $class = $attr->associated_class;
65  
66 $attr->$orig(@_);
67  
68 if($attr->has_shared) {
69 if(my $writer_name = $attr->get_write_method) {
70 $class->add_before_method_modifier($writer_name, sub {
71 my ($self, $value) = @_;
72 return unless defined $value;
73  
74 my $name = $attr->name;
75 my $ctx = $self->ctx;
76 $ctx->stash->{__MXACShared}{$name} = $value;
77 });
78 }
79  
80 if(my $reader_name = $attr->get_read_method) {
81 my $sub = sub {
82 my ($self) = @_;
83 $self->$reader_name; # Trigger the lazy builder if necessary
84 };
85 $class->can('BUILD') ?
86 $class->add_after_method_modifier('BUILD', $sub) :
87 $class->add_method('BUILD', $sub);
88 }
89 }
90  
91 };
92  
93 package Moose::Meta::Attribute::Custom::Trait::Catalyst::Shared;
94 sub register_implementation { 'MooseX::Attribute::Catalyst::Shared' }
95  
96 package MooseX::Attribute::Catalyst::Scoped;
97
98 use Moose::Role;
99  
100 has 'context' => (is=>'ro', predicate=>'has_context');
101 has 'from_key' => (is=>'ro');
102 has 'to_key' => (is=>'ro');
103  
104 around '_process_options' => sub {
105 my ($orig, $self, $name, $options) = (@_);
106 $options = $self->_process_context($name, $options) if exists $options->{context};
107 return $self->$orig($name, $options);
108 };
109  
110 sub _process_context {
111 my ($self, $name, $options) = @_;
112 my $from_key = $options->{context};
113 $from_key = $name if $from_key eq '1';
114  
115 $options->{to_key} = $name;
116 $options->{from_key} = $from_key;
117 $options->{lazy} = 1;
118 $options->{default} = sub {
119 my $self = shift;
120 my $value = sub {
121 my $ctx = $self->ctx;
122 return $ctx->stash->{$from_key} if exists $ctx->stash->{$from_key};
123 return $ctx->$from_key if $ctx->can($from_key);
124 die "Could not find value for $from_key";
125 }->();
126 return $self->ctx->stash->{$name} = $value;
127 };
128 return $options;
129 }
130  
131 around 'install_accessors' => sub {
132 my $orig = shift;
133 my $attr = shift;
134 my $class = $attr->associated_class;
135  
136 $attr->$orig(@_);
137  
138 if($attr->has_context) {
139 if(my $writer_name = $attr->get_write_method) {
140 $class->add_before_method_modifier($writer_name, sub {
141 my ($self, $value) = @_;
142 return unless defined $value;
143 $self->ctx->stash->{$attr->to_key} = $value;
144 });
145 }
146 }
147  
148  
149 # This seems like a fragile hack, if anyone has a better way to
150 # trigger the lazy builder, please let me know.
151  
152 if(my $reader_name = $attr->get_read_method) {
153 my $sub = sub {
154 my ($self) = @_;
155 $self->$reader_name;
156 };
157 $class->can('BUILD') ?
158 $class->add_after_method_modifier('BUILD', $sub) :
159 $class->add_method('BUILD', $sub);
160 }
161 };
162  
163  
164 package Moose::Meta::Attribute::Custom::Trait::Catalyst::Scoped;
165 sub register_implementation { 'MooseX::Attribute::Catalyst::Scoped' }
166  
167 package CatalystX::Moose::Object;
168  
169 use Moose;
170 use MooseX::MethodAttributes;
171  
172 package CatalystX::Moose;
173  
174 our $VERSION = '0.007';
175
176 use Moose ();
177 use Moose::Exporter;
178 use Moose::Util::MetaRole;
179 use MooseX::Attribute::Catalyst::Scoped;
180 use MooseX::Attribute::Catalyst::Shared;
181  
182 Moose::Exporter->setup_import_methods( also => 'Moose' );
183  
184 sub init_meta {
185 my ($class, %args) = (@_);
186  
187 Moose->init_meta(%args, base_class => 'CatalystX::Moose::Object');
188 Moose::Util::MetaRole::apply_metaroles(
189 for => $args{for_class},
190 class_metaroles => {
191 attribute => ['MooseX::Attribute::Catalyst::Scoped',
192 'MooseX::Attribute::Catalyst::Shared'],
193 },
194 );
195
196 return $args{for_class}->meta();
197 }
198  
199 1;