« blib/lib/Class/MakeMethods/Standard/Array.pm
↑ Summary
blib/lib/Class/MakeMethods/Template/Array.pm »
| 1 | package Class::MakeMethods::Template::ClassName; | ||
| 2 | |||
| 3 | use Class::MakeMethods::Template '-isasubclass'; | ||
| 4 | $VERSION = 1.008; | ||
| 5 | |||
| 6 | sub _diagnostic { &Class::MakeMethods::_diagnostic } | ||
| 7 | |||
| 8 | ######################################################################## | ||
| 9 | ###### CLASS NAME MANIPULATIONS | ||
| 10 | ######################################################################## | ||
| 11 | |||
| 12 | =head1 NAME | ||
| 13 | |||
| 14 | Class::MakeMethods::Template::ClassName - Access object's class | ||
| 15 | |||
| 16 | =head1 SYNOPSIS | ||
| 17 | |||
| 18 | package MyObject; | ||
| 19 | use Class::MakeMethods::Template::ClassName ( | ||
| 20 | subclass_name => [ 'type' ] | ||
| 21 | ); | ||
| 22 | ... | ||
| 23 | package main; | ||
| 24 | my $object = MyObject->new; | ||
| 25 | |||
| 26 | $object->type('Foo') | ||
| 27 | # reblesses object to MyObject::Foo subclass | ||
| 28 | |||
| 29 | print $object->type(); | ||
| 30 | # prints "Foo". | ||
| 31 | |||
| 32 | =head1 DESCRIPTION | ||
| 33 | |||
| 34 | These method types access or change information about the class an object is associated with. | ||
| 35 | |||
| 36 | =head2 class_name | ||
| 37 | |||
| 38 | Called without arguments, returns the class name. | ||
| 39 | |||
| 40 | If called with an argument, reblesses object into that class. | ||
| 41 | If the class doesn't already exist, it will be created. | ||
| 42 | |||
| 43 | =head2 subclass_name | ||
| 44 | |||
| 45 | Called without arguments, returns the subclass name. | ||
| 46 | |||
| 47 | If called with an argument, reblesses object into that subclass. | ||
| 48 | If the subclass doesn't already exist, it will be created. | ||
| 49 | |||
| 50 | The subclass name is written as follows: | ||
| 51 | |||
| 52 | =over 4 | ||
| 53 | |||
| 54 | =item * | ||
| 55 | |||
| 56 | if it's the original, defining class: empty | ||
| 57 | |||
| 58 | =item * | ||
| 59 | |||
| 60 | if its a a package within the namespace of the original: the distingushing name within that namespace, without leading C<::> | ||
| 61 | |||
| 62 | =item * | ||
| 63 | |||
| 64 | if it's a package elsewhere: the full name with leading C<::> | ||
| 65 | |||
| 66 | =back | ||
| 67 | |||
| 68 | =cut | ||
| 69 | |||
| 70 | # $subclass = _pack_subclass( $base, $pckg ); | ||
| 71 | sub _pack_subclass { | ||
| 72 | my $base = shift; | ||
| 73 | my $pckg = shift; | ||
| 74 | |||
| 75 | ( $pckg eq $base ) ? '' : | ||
| 76 | ( $pckg =~ s/^\Q$base\E\:\:// ) ? $pckg : | ||
| 77 | "::$pckg"; | ||
| 78 | } | ||
| 79 | |||
| 80 | # $pckg = _unpack_subclass( $base, $subclass ); | ||
| 81 | sub _unpack_subclass { | ||
| 82 | my $base = shift; | ||
| 83 | my $subclass = shift; | ||
| 84 | |||
| 85 | ! $subclass ? $base : | ||
| 86 | ( $subclass =~ s/^::// ) ? $subclass : | ||
| 87 | "$base\::$subclass"; | ||
| 88 | } | ||
| 89 | |||
| 90 | # $pckg = _require_class( $package ); | ||
| 91 | sub _require_class { | ||
| 92 | my $package = shift; | ||
| 93 | |||
| 94 | no strict 'refs'; | ||
| 95 | unless ( @{$package . '::ISA'} ) { | ||
| 96 | (my $file = $package . '.pm' ) =~ s|::|/|go; | ||
| 97 | local $SIG{__DIE__} = sub { die @_ }; | ||
| 98 | # warn "Auto-requiring package $package \n"; | ||
| 99 | eval { require $file }; | ||
| 100 | if ( $@ ) { _diagnostic('mm_package_fail', $package, $@) } | ||
| 101 | } | ||
| 102 | |||
| 103 | return $package; | ||
| 104 | } | ||
| 105 | |||
| 106 | # $pckg = _provide_class( $base, $package ); | ||
| 107 | sub _provide_class { | ||
| 108 | my $base = shift; | ||
| 109 | my $package = shift; | ||
| 110 | |||
| 111 | # If the subclass hasn't been created yet, do so. | ||
| 112 | no strict 'refs'; | ||
| 113 | unless ( scalar @{$package . '::ISA'} ) { | ||
| 114 | # warn "Auto-vivifying $base subclass $package\n"; | ||
| 115 | @{$package . '::ISA'} = ( $base ); | ||
| 116 | } | ||
| 117 | |||
| 118 | return $package; | ||
| 119 | } | ||
| 120 | |||
| 121 | sub class_name { | ||
| 122 | { | ||
| 123 | 'interface' => { | ||
| 124 | default => 'autocreate', | ||
| 125 | autocreate => { '*'=>'autocreate' }, | ||
| 126 | require => { '*'=>'require' }, | ||
| 127 | }, | ||
| 128 | 'behavior' => { | ||
| 129 | 'autocreate' => q{ | ||
| 130 | if ( ! scalar @_ ) { | ||
| 131 | _CLASS_GET_ | ||
| 132 | } else { | ||
| 133 | _CLASS_PROVIDE_ | ||
| 134 | } | ||
| 135 | }, | ||
| 136 | 'require' => q{ | ||
| 137 | if ( ! scalar @_ ) { | ||
| 138 | _CLASS_GET_ | ||
| 139 | } else { | ||
| 140 | _CLASS_REQUIRE_ | ||
| 141 | } | ||
| 142 | }, | ||
| 143 | }, | ||
| 144 | 'code_expr' => { | ||
| 145 | _CLASS_GET_ => q{ | ||
| 146 | my $class = ref $self || $self; | ||
| 147 | }, | ||
| 148 | _CLASS_REQUIRE_ => q{ | ||
| 149 | my $class = Class::MakeMethods::Template::ClassName::_require_class( shift() ); | ||
| 150 | _BLESS_AND_RETURN_ | ||
| 151 | }, | ||
| 152 | _CLASS_PROVIDE_ => q{ | ||
| 153 | my $class = Class::MakeMethods::Template::ClassName::_provide_class( | ||
| 154 | $m_info->{'target_class'}, shift() ); | ||
| 155 | _BLESS_AND_RETURN_ | ||
| 156 | }, | ||
| 157 | _BLESS_AND_RETURN_ => q{ | ||
| 158 | bless $self, $class if ( ref $self ); | ||
| 159 | return $class; | ||
| 160 | }, | ||
| 161 | }, | ||
| 162 | } | ||
| 163 | } | ||
| 164 | |||
| 165 | sub subclass_name { | ||
| 166 | { | ||
| 167 | '-import' => { | ||
| 168 | 'Template::ClassName:class_name' => '*', | ||
| 169 | }, | ||
| 170 | 'code_expr' => { | ||
| 171 | _CLASS_GET_ => q{ | ||
| 172 | my $class = ref $self || $self; | ||
| 173 | Class::MakeMethods::Template::ClassName::_pack_subclass( $m_info->{'target_class'}, $class ) | ||
| 174 | }, | ||
| 175 | _CLASS_REQUIRE_ => q{ | ||
| 176 | my $subclass = Class::MakeMethods::Template::ClassName::_unpack_subclass( | ||
| 177 | $m_info->{'target_class'}, shift() ); | ||
| 178 | my $class = Class::MakeMethods::Template::ClassName::_require_class($subclass); | ||
| 179 | _BLESS_AND_RETURN_ | ||
| 180 | }, | ||
| 181 | _CLASS_PROVIDE_ => q{ | ||
| 182 | my $subclass = Class::MakeMethods::Template::ClassName::_unpack_subclass( | ||
| 183 | $m_info->{'target_class'}, shift() ); | ||
| 184 | my $class = Class::MakeMethods::Template::ClassName::_provide_class( | ||
| 185 | $m_info->{'target_class'}, $subclass ); | ||
| 186 | _BLESS_AND_RETURN_ | ||
| 187 | }, | ||
| 188 | }, | ||
| 189 | } | ||
| 190 | } | ||
| 191 | |||
| 192 | |||
| 193 | ######################################################################## | ||
| 194 | ### CLASS_REGISTRY | ||
| 195 | |||
| 196 | =head2 static_hash_classname | ||
| 197 | |||
| 198 | Provides a shared hash mapping keys to class names. | ||
| 199 | |||
| 200 | class_registry => [ qw/ foo / ] | ||
| 201 | |||
| 202 | Takes a single string or a reference to an array of strings as its argument. | ||
| 203 | For each string, creates a new anonymous hash and associated accessor methods | ||
| 204 | that will map scalar values to classes in the calling package's subclass | ||
| 205 | hiearchy. | ||
| 206 | |||
| 207 | The accessor methods provide an interface to the hash as illustrated below. | ||
| 208 | Note that several of these functions operate quite differently depending on the | ||
| 209 | number of arguments passed, or the context in which they are called. | ||
| 210 | |||
| 211 | =over 4 | ||
| 212 | |||
| 213 | =item @indexes = $class_or_ref->x; | ||
| 214 | |||
| 215 | Returns the scalar values that are indexes associated with this class, or the class of this object. | ||
| 216 | |||
| 217 | =item $class = $class_or_ref->x( $index ); | ||
| 218 | |||
| 219 | Returns the class name associated with the provided index value. | ||
| 220 | |||
| 221 | =item @classes = $class_or_ref->x( @indexes ); | ||
| 222 | |||
| 223 | Returns the associated classes for each index in order. | ||
| 224 | |||
| 225 | =item @all_indexes = $class_or_ref->x_keys; | ||
| 226 | |||
| 227 | Returns a list of the indexes defined for this registry. | ||
| 228 | |||
| 229 | =item @all_classes = $class_or_ref->x_values; | ||
| 230 | |||
| 231 | Returns a list of the classes associated with this registry. | ||
| 232 | |||
| 233 | =item @all_classes = $class_or_ref->unique_x_values; | ||
| 234 | |||
| 235 | Returns a list of the classes associated with this registry, with no more than one occurance of any value. | ||
| 236 | |||
| 237 | =item %mapping = $class_or_ref->x_hash; | ||
| 238 | |||
| 239 | Return the key-value pairs used to store this attribute | ||
| 240 | |||
| 241 | =item $mapping_ref = $class_or_ref->x_hash; | ||
| 242 | |||
| 243 | Returns a reference to the hash used for the mapping. | ||
| 244 | |||
| 245 | =item $class_or_ref->add_x( @indexes ); | ||
| 246 | |||
| 247 | Adds an entry in the hash for each of the provided indexes, mapping it to this class, or the class of this object. | ||
| 248 | |||
| 249 | =item $class_or_ref->clear_x; | ||
| 250 | |||
| 251 | Removes those entries from the hash whose values are this class, or the class of this object. | ||
| 252 | |||
| 253 | =item $class_or_ref->clear_xs( @indexes ); | ||
| 254 | |||
| 255 | Remove all entries from the hash. | ||
| 256 | |||
| 257 | =back | ||
| 258 | |||
| 259 | =cut | ||
| 260 | |||
| 261 | sub static_hash_classname { | ||
| 262 | { | ||
| 263 | '-import' => { | ||
| 264 | 'Template::Static:hash' => '*', | ||
| 265 | }, | ||
| 266 | 'params' => { 'instance' => {} }, | ||
| 267 | 'interface' => { | ||
| 268 | default => { | ||
| 269 | '*'=>'get_classname', | ||
| 270 | 'add_*'=>'add_classname', | ||
| 271 | 'clear_*'=>'drop_classname', | ||
| 272 | '*_keys'=>'keys', | ||
| 273 | '*_hash'=>'get', | ||
| 274 | '*_values'=>'values', | ||
| 275 | 'clear_*s'=>'clear', | ||
| 276 | 'unique_*_values'=>'unique_values', | ||
| 277 | }, | ||
| 278 | }, | ||
| 279 | 'behavior' => { | ||
| 280 | 'get_classname' => sub { my $m_info = $_[0]; sub { | ||
| 281 | my $self = shift; | ||
| 282 | my $class = ( ref($self) || $self ); | ||
| 283 | |||
| 284 | defined $m_info->{'instance'} or $m_info->{'instance'} = {}; | ||
| 285 | my $hash = $m_info->{'instance'}; | ||
| 286 | |||
| 287 | if ( ! scalar @_ ) { | ||
| 288 | my @keys = grep { $hash->{$_} eq $class } keys %$hash; | ||
| 289 | return wantarray ? @keys : $keys[0]; | ||
| 290 | } elsif (scalar @_ == 1) { | ||
| 291 | return $hash->{ shift() }; | ||
| 292 | } else { | ||
| 293 | return @{$hash}{ @_ }; | ||
| 294 | } | ||
| 295 | }}, | ||
| 296 | 'add_classname' => sub { my $m_info = $_[0]; sub { | ||
| 297 | my $self = shift; | ||
| 298 | my $class = ( ref($self) || $self ); | ||
| 299 | |||
| 300 | defined $m_info->{'instance'} or $m_info->{'instance'} = {}; | ||
| 301 | my $hash = $m_info->{'instance'}; | ||
| 302 | |||
| 303 | foreach ( @_ ) { $hash->{$_} = $class } | ||
| 304 | }}, | ||
| 305 | 'drop_classname' => sub { my $m_info = $_[0]; sub { | ||
| 306 | my $self = shift; | ||
| 307 | my $class = ( ref($self) || $self ); | ||
| 308 | |||
| 309 | defined $m_info->{'instance'} or $m_info->{'instance'} = {}; | ||
| 310 | my $hash = $m_info->{'instance'}; | ||
| 311 | |||
| 312 | foreach ( grep { $hash->{$_} eq $class } keys %$hash ){ | ||
| 313 | delete $hash{$_} | ||
| 314 | } | ||
| 315 | }}, | ||
| 316 | }, | ||
| 317 | } | ||
| 318 | } | ||
| 319 | |||
| 320 | ######################################################################## | ||
| 321 | |||
| 322 | =head1 SEE ALSO | ||
| 323 | |||
| 324 | See L<Class::MakeMethods> for general information about this distribution. | ||
| 325 | |||
| 326 | See L<Class::MakeMethods::Template> for information about this family of subclasses. | ||
| 327 | |||
| 328 | =cut | ||
| 329 | |||
| 330 | 1; |