| 1 | package Net::Google::WebmasterTools::Row; | ||
| 2 | { | ||
| 3 | $Net::Google::WebmasterTools::Row::VERSION = '0.03'; | ||
| 4 | } | ||
| 5 | use strict; | ||
| 6 | |||
| 7 | # ABSTRACT: Base class for Google WebmasterTools API result rows | ||
| 8 | |||
| 9 | BEGIN { | ||
| 10 | require Class::XSAccessor::Array; | ||
| 11 | } | ||
| 12 | |||
| 13 | my $class_count = 0; | ||
| 14 | |||
| 15 | # Dynamically generate a class with accessors | ||
| 16 | sub _gen_class { | ||
| 17 | my (undef, $column_headers) = @_; | ||
| 18 | |||
| 19 | # Generate unique package name | ||
| 20 | my $class = "Net::Google::WebmasterTools::Row_$class_count"; | ||
| 21 | ++$class_count; | ||
| 22 | |||
| 23 | { | ||
| 24 | # Set globals of new class | ||
| 25 | no strict 'refs'; | ||
| 26 | @{ "${class}::ISA" } = qw(Net::Google::WebmasterTools::Row); | ||
| 27 | ${ "${class}::column_headers" } = $column_headers; | ||
| 28 | } | ||
| 29 | |||
| 30 | # Create accessors | ||
| 31 | my %getters; | ||
| 32 | for (my $i = 0; $i < @$column_headers; ++$i) { | ||
| 33 | my $getter = 'get_' . $column_headers->[$i]->{name}; | ||
| 34 | $getters{$getter} = $i; | ||
| 35 | } | ||
| 36 | |||
| 37 | Class::XSAccessor::Array->import( | ||
| 38 | class => $class, | ||
| 39 | getters => \%getters, | ||
| 40 | ); | ||
| 41 | |||
| 42 | return $class; | ||
| 43 | } | ||
| 44 | |||
| 45 | sub new { | ||
| 46 | my ($class, $row) = @_; | ||
| 47 | return bless($row, $class); | ||
| 48 | } | ||
| 49 | |||
| 50 | sub _column_headers { | ||
| 51 | my $self = shift; | ||
| 52 | my $class = ref($self); | ||
| 53 | no strict 'refs'; | ||
| 54 | return ${ "${class}::column_headers" }; | ||
| 55 | } | ||
| 56 | |||
| 57 | sub get { | ||
| 58 | my ($self, $name) = @_; | ||
| 59 | |||
| 60 | my $column_headers = $self->_column_headers; | ||
| 61 | |||
| 62 | for (my $i = 0; $i < @$column_headers; ++$i) { | ||
| 63 | return $self->[$i] if $column_headers->[$i]->{name} eq $name; | ||
| 64 | } | ||
| 65 | |||
| 66 | return undef; | ||
| 67 | } | ||
| 68 | |||
| 69 | 1; | ||
| 70 | |||
| 71 | __END__ |