summaryrefslogtreecommitdiff
path: root/lib/WebService/TFL/Bus/ResponseTypeRole.pm
blob: 7f48419869cf80b6f37bd9d03017713290eced47 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package WebService::TFL::Bus::ResponseTypeRole; 
use Package::Variant
    importing => [ 'Moo::Role'],
    subs => [ 'has' ];
use WebService::TFL::Bus::Fields;
use WebService::TFL::Bus::Types 'DateTimeMillis';
 
sub make_variant {
    my ($class,$target_package,$type) = @_;
 
    my $method = $type . '_return_fields';
    my @fields = WebService::TFL::Bus::Fields->$method;
 
    for my $field (@fields) {
        has $field => (
            is => 'ro',
            $field =~ /Time$/ ? (
                isa => DateTimeMillis,
                coerce => DateTimeMillis->coercion,
            ) : () ),
        );
    }
 
    install new_from_array => sub {
        my ($class,$return_set,$array) = @_;
 
        my %args;
        my $i=1;
        for my $field (@fields) {
            next unless exists $return_set->{$field};
            $args{$field}=$array->[$i];
            ++$i;
        }
        return $class->new(\%args);
    }
}
 
1;