File Coverage

blib/lib/WebService/Toggl/Role/Item.pm
Criterion Covered Total %
statement 17 34 50.0
branch n/a
condition n/a
subroutine 6 8 75.0
pod 0 1 0.0
total 23 43 53.4


line stmt bran cond sub pod time code
1             package WebService::Toggl::Role::Item;
2              
3 1     1   5 use strict;
  1         2  
  1         32  
4 1     1   4 use warnings;
  1         1  
  1         37  
5              
6             use Package::Variant
7 1         6 importing => ['Moo::Role'],
8 1     1   486 subs => [qw(has with around)];
  1         6831  
9              
10 1     1   1050 use Sub::Quote qw(quote_sub);
  1         8811  
  1         93  
11 1     1   710 use Types::Standard qw(Bool Int Str);
  1         63146  
  1         13  
12              
13             sub make_variant {
14 9     9 0 7137 my ($class, $target_pkg, %arguments) = @_;
15              
16 9         27 with 'WebService::Toggl::Role::Base';
17              
18             around '_build_my_url' => sub {
19 0     0     my $orig = shift;
20 0           my $self = shift;
21 0           my $url = $self->$orig() . '/' . $self->api_id;
22 0           $url =~ s{/$}{};
23 0           return $url;
24 0           };
25              
26 0           has raw => (is => 'ro', lazy => 1, builder => 1);
27             install '_build_raw' => sub {
28 0     0     my ($self) = @_;
29 0           my $response = $self->api_get( $self->my_url, {with_related_data => 1} );
30 0           return $response->data->{data};
31 0           };
32              
33 0           has $_ => (is => 'ro', isa => Bool, lazy => 1, builder => quote_sub(qq| \$_[0]->raw->{$_} |))
34 0           for (@{ $arguments{bools} } );
35 0           has $_ => (is => 'ro', isa => Str, lazy => 1, builder => quote_sub(qq| \$_[0]->raw->{$_} |))
36 0           for (@{ $arguments{strings} } );
37 0           has $_ => (is => 'ro', isa => Int, lazy => 1, builder => quote_sub(qq| \$_[0]->raw->{$_} |))
38 0           for (@{ $arguments{integers} });
39             }
40              
41              
42             1;
43             __END__
44              
45             =encoding utf-8
46              
47             =head1 NAME
48              
49             WebService::Toggl::Role::Item - Create roles for all WebService::Toggl::API Items
50              
51             =head1 SYNOPSIS
52              
53             package WebService::Toggl::API::Tag;
54              
55             use WebService::Toggl::Role::Item as => 'JsonItem';
56              
57             use Moo;
58             with 'WebService::Toggl::API';
59             use namespace::clean;
60              
61             with JsonItem(
62             bools => [ qw() ],
63             strings => [ qw(name) ],
64             integers => [ qw(id wid) ],
65             );
66              
67             sub api_path { 'tags' }
68             sub api_id { shift->id }
69              
70              
71             =head1 DESCRIPTION
72              
73             This package constructs dynamic roles for WebService::Toggl::API
74             objects representing individual Items. The calling class gives it a
75             list of boolean fields, a list of string fields, and a list of integer
76             fields. This package will then construct type-checked accessors for
77             all the provided attributes to fetch them from the raw response.
78             Calling the constructed attributes will cause an API request to be
79             made, unless the raw data already exists in the object.
80              
81             =head2 Provided Attributes
82              
83             =head3 raw
84              
85             The raw data returned from an API request.
86              
87             =head2 Wrapped Methods
88              
89             =head3 my_url
90              
91             Returns the API URL for the object.
92              
93             =head1 LICENSE
94              
95             Copyright (C) Fitz Elliott.
96              
97             This library is free software; you can redistribute it and/or modify
98             it under the same terms as Perl itself.
99              
100             =head1 AUTHOR
101              
102             Fitz Elliott E<lt>felliott@fiskur.orgE<gt>
103              
104             =cut