| 1 | package Scrappy::Project::Document; | ||
| 2 | |||
| 3 | BEGIN { | ||
| 4 | $Scrappy::Project::Document::VERSION = '0.94112090'; | ||
| 5 | } | ||
| 6 | |||
| 7 | use Moose::Role; | ||
| 8 | |||
| 9 | has fields => ( | ||
| 10 | is => 'ro', | ||
| 11 | isa => 'ArrayRef', | ||
| 12 | default => sub { | ||
| 13 | my $self = shift; | ||
| 14 | |||
| 15 | #die Data::Dumper::Dumper((shift->meta->get_all_methods)[1]); | ||
| 16 | |||
| 17 | my @fields = (); | ||
| 18 | for ($self->meta->get_all_methods) { | ||
| 19 | push @fields, $_->name | ||
| 20 | if $_->package_name eq ref $self | ||
| 21 | && $_->name ne 'meta' | ||
| 22 | && $_->name ne 'scraper' | ||
| 23 | && $_->name ne 'fields' | ||
| 24 | && $_->name ne 'parse' | ||
| 25 | && $_->name ne 'records' | ||
| 26 | && $_->name ne 'url'; | ||
| 27 | |||
| 28 | ##### NOTE !!!!!! The list above must always contain a list | ||
| 29 | ##### of all attributes and function in this role | ||
| 30 | } | ||
| 31 | |||
| 32 | return [@fields]; | ||
| 33 | } | ||
| 34 | ); | ||
| 35 | |||
| 36 | has records => ( | ||
| 37 | is => 'rw', | ||
| 38 | isa => 'ArrayRef', | ||
| 39 | default => sub { [] } | ||
| 40 | ); | ||
| 41 | |||
| 42 | has scraper => ( | ||
| 43 | is => 'rw', | ||
| 44 | isa => 'Scrappy' | ||
| 45 | ); | ||
| 46 | |||
| 47 | sub parse { | ||
| 48 | my ($self, $vars) = @_; | ||
| 49 | |||
| 50 | my $record = {}; | ||
| 51 | map { $record->{$_} = $self->$_($self->scraper, $vars) } @{$self->fields}; | ||
| 52 | |||
| 53 | # $record->{url} = $self->scraper->url->as_string; | ||
| 54 | push @{$self->records}, $record; | ||
| 55 | |||
| 56 | return $record; | ||
| 57 | } | ||
| 58 | |||
| 59 | 1; |