| 1 | package HTML::Widget::Accessor; | ||
| 2 | |||
| 3 | use warnings; | ||
| 4 | use strict; | ||
| 5 | use base 'Class::Accessor::Chained::Fast'; | ||
| 6 | use Carp qw/croak/; | ||
| 7 | |||
| 8 | *attrs = \&attributes; | ||
| 9 | |||
| 10 | =head1 NAME | ||
| 11 | |||
| 12 | HTML::Widget::Accessor - Accessor Class | ||
| 13 | |||
| 14 | =head1 SYNOPSIS | ||
| 15 | |||
| 16 | use base 'HTML::Widget::Accessor'; | ||
| 17 | |||
| 18 | =head1 DESCRIPTION | ||
| 19 | |||
| 20 | Accessor Class. | ||
| 21 | |||
| 22 | =head1 METHODS | ||
| 23 | |||
| 24 | =head2 attributes | ||
| 25 | |||
| 26 | =head2 attrs | ||
| 27 | |||
| 28 | Arguments: %attributes | ||
| 29 | |||
| 30 | Arguments: \%attributes | ||
| 31 | |||
| 32 | Return Value: $self | ||
| 33 | |||
| 34 | Arguments: none | ||
| 35 | |||
| 36 | Return Value: \%attributes | ||
| 37 | |||
| 38 | Accepts either a list of key/value pairs, or a hash-ref. | ||
| 39 | |||
| 40 | $w->attributes( $key => $value ); | ||
| 41 | $w->attributes( { $key => $value } ); | ||
| 42 | |||
| 43 | Returns the object reference, to allow method chaining. | ||
| 44 | |||
| 45 | As of v1.10, passing a hash-ref no longer deletes current | ||
| 46 | attributes, instead the attributes are added to the current attributes | ||
| 47 | hash. | ||
| 48 | |||
| 49 | This means the attributes hash-ref can no longer be emptied using | ||
| 50 | C<$w->attributes( { } );>. Instead, you may use | ||
| 51 | C<%{ $w->attributes } = ();>. | ||
| 52 | |||
| 53 | As a special case, if no arguments are passed, the return value is a | ||
| 54 | hash-ref of attributes instead of the object reference. This provides | ||
| 55 | backwards compatability to support: | ||
| 56 | |||
| 57 | $w->attributes->{key} = $value; | ||
| 58 | |||
| 59 | L</attrs> is an alias for L</attributes>. | ||
| 60 | |||
| 61 | =cut | ||
| 62 | |||
| 63 | sub attributes { | ||
| 64 | my $self = shift; | ||
| 65 | |||
| 66 | $self->{attributes} = {} if not defined $self->{attributes}; | ||
| 67 | |||
| 68 | # special-case to support $w->attrs->{key} = value | ||
| 69 | return $self->{attributes} unless @_; | ||
| 70 | |||
| 71 | my %attrs = | ||
| 72 | ( scalar(@_) == 1 ) | ||
| 73 | ? %{ $_[0] } | ||
| 74 | : @_; | ||
| 75 | |||
| 76 | $self->{attributes}->{$_} = $attrs{$_} for keys %attrs; | ||
| 77 | |||
| 78 | return $self; | ||
| 79 | } | ||
| 80 | |||
| 81 | =head2 mk_attr_accessors | ||
| 82 | |||
| 83 | Arguments: @names | ||
| 84 | |||
| 85 | Return Value: @names | ||
| 86 | |||
| 87 | =cut | ||
| 88 | |||
| 89 | sub mk_attr_accessors { | ||
| 90 | my ( $self, @names ) = @_; | ||
| 91 | my $class = ref $self || $self; | ||
| 92 | for my $name (@names) { | ||
| 93 | no strict 'refs'; | ||
| 94 | *{"$class\::$name"} = sub { | ||
| 95 | return ( $_[0]->{attributes}->{$name} || $_[0] ) unless @_ > 1; | ||
| 96 | my $self = shift; | ||
| 97 | $self->{attributes}->{$name} = ( @_ == 1 ? $_[0] : [@_] ); | ||
| 98 | return $self; | ||
| 99 | } | ||
| 100 | } | ||
| 101 | } | ||
| 102 | |||
| 103 | sub _instantiate { | ||
| 104 | my ( $self, $class, @args ) = @_; | ||
| 105 | my $file = $class . ".pm"; | ||
| 106 | $file =~ s{::}{/}g; | ||
| 107 | eval { require $file }; | ||
| 108 | croak qq/Couldn't load class "$class", "$@"/ if $@; | ||
| 109 | return $class->new(@args); | ||
| 110 | } | ||
| 111 | |||
| 112 | =head1 AUTHOR | ||
| 113 | |||
| 114 | Sebastian Riedel, C<sri@oook.de> | ||
| 115 | |||
| 116 | =head1 LICENSE | ||
| 117 | |||
| 118 | This library is free software, you can redistribute it and/or modify it under | ||
| 119 | the same terms as Perl itself. | ||
| 120 | |||
| 121 | =cut | ||
| 122 | |||
| 123 | 1; |