| 1 | package Image::ButtonMaker::ButtonClass; | ||
| 2 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 0 | ▶ | use Image::ButtonMaker::Button; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 4 | |||
| 5 | #### Default values for classes: a class is just a hash | ||
| 6 | my @defaults = ( | ||
| 7 | classname => '', | ||
| 8 | parent => '', | ||
| 9 | container => undef, | ||
| 10 | properties => {}, | ||
| 11 | ); | ||
| 12 | |||
| 13 | our $error; | ||
| 14 | our $errorstr; | ||
| 15 | |||
| 16 | sub new { | ||
| 17 | my $invocant = shift; | ||
| 18 | my $class = ref($invocant) || $invocant; | ||
| 19 | |||
| 20 | my %prototype = @_; | ||
| 21 | return | ||
| 22 | set_error(1000, "No class name specified") | ||
| 23 | unless($prototype{classname}); | ||
| 24 | |||
| 25 | #### Check properites validity #### | ||
| 26 | my $prop = $prototype{properties} || {}; | ||
| 27 | return | ||
| 28 | set_error(1001, "properties argument must be a HASH ref") | ||
| 29 | if(ref($prop) ne 'HASH'); | ||
| 30 | |||
| 31 | foreach my $p (keys(%$prop)) { | ||
| 32 | return | ||
| 33 | set_error(1002, "Illegal property: $p") | ||
| 34 | unless(Image::ButtonMaker::Button->is_property_legal($p)); | ||
| 35 | |||
| 36 | } | ||
| 37 | |||
| 38 | my $object = { @defaults, %prototype}; | ||
| 39 | bless $object, $class; | ||
| 40 | return $object; | ||
| 41 | } | ||
| 42 | |||
| 43 | |||
| 44 | #### Instance Methods ####################################################### | ||
| 45 | sub set_container { | ||
| 46 | my $self = shift; | ||
| 47 | my $container = shift; | ||
| 48 | reset_error(); | ||
| 49 | |||
| 50 | $self->{container} = $container; | ||
| 51 | return; | ||
| 52 | } | ||
| 53 | |||
| 54 | |||
| 55 | sub lookup_name { | ||
| 56 | my $self = shift; | ||
| 57 | reset_error(); | ||
| 58 | |||
| 59 | return $self->{classname}; | ||
| 60 | } | ||
| 61 | |||
| 62 | |||
| 63 | sub lookup_parent { | ||
| 64 | my $self = shift; | ||
| 65 | reset_error(); | ||
| 66 | return $self->{parent}; | ||
| 67 | } | ||
| 68 | |||
| 69 | |||
| 70 | sub lookup_property { | ||
| 71 | my $self = shift; | ||
| 72 | my $propname = shift; | ||
| 73 | |||
| 74 | reset_error(); | ||
| 75 | |||
| 76 | my $prop = $self->{properties}; | ||
| 77 | |||
| 78 | return $prop->{$propname} if(defined($prop->{$propname})); | ||
| 79 | |||
| 80 | if($self->{parent}) { | ||
| 81 | |||
| 82 | return set_error(2000, "Parent attribute is set, but no container is given") | ||
| 83 | unless($self->{container}); | ||
| 84 | my $container = $self->{container}; | ||
| 85 | |||
| 86 | my $parent_obj = $container->lookup_class($self->{parent}); | ||
| 87 | return set_error(2000, "Parent class not found") | ||
| 88 | unless($parent_obj); | ||
| 89 | |||
| 90 | return $parent_obj->lookup_property($propname); | ||
| 91 | } | ||
| 92 | |||
| 93 | return undef; | ||
| 94 | } | ||
| 95 | |||
| 96 | |||
| 97 | #### Set property. Return undef if property is illegal | ||
| 98 | sub set_property { | ||
| 99 | reset_error(); | ||
| 100 | |||
| 101 | my $self = shift; | ||
| 102 | my ($propname, $propvalue) = (@_); | ||
| 103 | |||
| 104 | return set_error(2000, "No property name given") | ||
| 105 | unless(defined($propname)); | ||
| 106 | return set_error(2000, "Illegal property name $propname") | ||
| 107 | unless(Image::ButtonMaker::Button->is_property_legal($propname)); | ||
| 108 | |||
| 109 | $self->{properties}{$propname} = $propvalue; | ||
| 110 | return $propvalue; | ||
| 111 | } | ||
| 112 | |||
| 113 | |||
| 114 | #### Remove property from properties HASH and return the property value | ||
| 115 | ## which _can_ be inherited from some parent class | ||
| 116 | sub delete_property { | ||
| 117 | reset_error(); | ||
| 118 | my $self = shift; | ||
| 119 | my ($propname) = (@_); | ||
| 120 | |||
| 121 | return set_error(2000, "No property name given") | ||
| 122 | unless(defined($propname)); | ||
| 123 | return set_error(2000, "Illegal property name $propname") | ||
| 124 | unless(Image::ButtonMaker::Button->is_property_legal($propname)); | ||
| 125 | } | ||
| 126 | |||
| 127 | |||
| 128 | #### Package methods ############################################################ | ||
| 129 | #### Set and reset package-wide error codes | ||
| 130 | sub reset_error { | ||
| 131 | $error = 0; | ||
| 132 | $errorstr = 0; | ||
| 133 | return; | ||
| 134 | } | ||
| 135 | |||
| 136 | sub set_error { | ||
| 137 | $error = shift; | ||
| 138 | $errorstr = shift; | ||
| 139 | return undef; | ||
| 140 | } | ||
| 141 | |||
| 142 | 1; | ||