Commit | Line | Data |
---|---|---|
bb70fe3a | 1 | package MooseX::ClassAttribute::Meta::Method::Accessor; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | ||
67e6eaa5 | 6 | use namespace::autoclean; |
bb70fe3a | 7 | use Moose; |
8 | ||
9 | extends 'Moose::Meta::Method::Accessor'; | |
10 | ||
67e6eaa5 | 11 | sub _generate_predicate_method_inline { |
12 | my $attr = (shift)->associated_attribute; | |
bb70fe3a | 13 | |
67e6eaa5 | 14 | my $code |
15 | = eval 'sub {' | |
16 | . $attr->associated_class() | |
17 | ->inline_is_class_slot_initialized( $attr->name() ) . '}'; | |
bb70fe3a | 18 | |
19 | confess "Could not generate inline predicate because : $@" if $@; | |
20 | ||
21 | return $code; | |
22 | } | |
23 | ||
67e6eaa5 | 24 | sub _generate_clearer_method_inline { |
bb70fe3a | 25 | my $attr = (shift)->associated_attribute; |
bb70fe3a | 26 | my $meta_instance = $attr->associated_class->instance_metaclass; |
27 | ||
67e6eaa5 | 28 | my $code |
29 | = eval 'sub {' | |
30 | . $attr->associated_class() | |
31 | ->inline_deinitialize_class_slot( $attr->name() ) . '}'; | |
bb70fe3a | 32 | |
33 | confess "Could not generate inline clearer because : $@" if $@; | |
34 | ||
35 | return $code; | |
36 | } | |
37 | ||
67e6eaa5 | 38 | sub _inline_store { |
39 | my $self = shift; | |
bb70fe3a | 40 | shift; |
41 | my $value = shift; | |
42 | ||
43 | my $attr = $self->associated_attribute(); | |
44 | ||
bb70fe3a | 45 | my $meta = $attr->associated_class(); |
46 | ||
67e6eaa5 | 47 | my $code |
48 | = $meta->inline_set_class_slot_value( $attr->slots(), $value ) . ";"; | |
49 | $code | |
50 | .= $meta->inline_weaken_class_slot_value( $attr->slots(), $value ) | |
51 | . ";" | |
bb70fe3a | 52 | if $attr->is_weak_ref(); |
53 | ||
54 | return $code; | |
55 | } | |
56 | ||
67e6eaa5 | 57 | sub _inline_get { |
58 | my $self = shift; | |
bb70fe3a | 59 | |
60 | my $attr = $self->associated_attribute; | |
61 | my $meta = $attr->associated_class(); | |
62 | ||
7aab7f6c | 63 | return $meta->inline_get_class_slot_value( $attr->slots() ); |
bb70fe3a | 64 | } |
65 | ||
67e6eaa5 | 66 | sub _inline_access { |
67 | my $self = shift; | |
bb70fe3a | 68 | |
69 | my $attr = $self->associated_attribute; | |
70 | my $meta = $attr->associated_class(); | |
71 | ||
7aab7f6c | 72 | return $meta->inline_class_slot_access( $attr->slots() ); |
bb70fe3a | 73 | } |
74 | ||
67e6eaa5 | 75 | sub _inline_has { |
bb70fe3a | 76 | my $self = shift; |
77 | ||
78 | my $attr = $self->associated_attribute; | |
79 | my $meta = $attr->associated_class(); | |
80 | ||
7aab7f6c | 81 | return $meta->inline_is_class_slot_initialized( $attr->slots() ); |
bb70fe3a | 82 | } |
83 | ||
67e6eaa5 | 84 | sub _inline_init_slot { |
bb70fe3a | 85 | my $self = shift; |
86 | ||
87 | return $self->_inline_store( undef, $_[-1] ); | |
88 | } | |
89 | ||
67e6eaa5 | 90 | sub _inline_check_lazy { |
bb70fe3a | 91 | my $self = shift; |
92 | ||
67e6eaa5 | 93 | return $self->SUPER::_inline_check_lazy( q{'} |
94 | . $self->associated_attribute()->associated_class()->name() | |
95 | . q{'} ); | |
bb70fe3a | 96 | } |
97 | ||
67e6eaa5 | 98 | sub _inline_get_old_value_for_trigger { |
8207dfe7 | 99 | my $self = shift; |
100 | ||
101 | my $attr = $self->associated_attribute(); | |
102 | return '' unless $attr->has_trigger(); | |
103 | ||
67e6eaa5 | 104 | my $pred = $attr->associated_class() |
105 | ->inline_is_class_slot_initialized( $attr->name() ); | |
8207dfe7 | 106 | |
107 | return | |
67e6eaa5 | 108 | 'my @old = ' |
8207dfe7 | 109 | . $pred . q{ ? } |
67e6eaa5 | 110 | . $self->_inline_get() |
111 | . q{ : ()} . ";\n"; | |
8207dfe7 | 112 | |
113 | } | |
114 | ||
bb70fe3a | 115 | 1; |
116 | ||
0d0bf8c3 | 117 | # ABSTRACT: Accessor method generation for class attributes |
118 | ||
a1c01f67 | 119 | __END__ |
120 | ||
7a4a3b1e | 121 | =pod |
122 | ||
7a4a3b1e | 123 | =head1 DESCRIPTION |
124 | ||
125 | This class overrides L<Moose::Meta::Method::Accessor> to do code | |
126 | generation properly for class attributes. | |
127 | ||
7a4a3b1e | 128 | =head1 BUGS |
129 | ||
130 | See L<MooseX::ClassAttribute> for details. | |
131 | ||
7a4a3b1e | 132 | =cut |