File: Literature.pm

package info (click to toggle)
libautobox-transform-perl 1.035-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 296 kB
  • sloc: perl: 717; makefile: 2
file content (141 lines) | stat: -rw-r--r-- 3,103 bytes parent folder | download
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
use strict;
use warnings;

package # hide from PAUSE
    Author;
use Moo;
use autobox::Core;

has name      => ( is => "lazy" );
has publisher => ( is => "lazy" );
has books     => ( is => "lazy" );
sub _build_books { [] }

sub publisher_affiliation {
    my $self = shift;
    my ($of) = @_;
    return $self->name . " $of " . $self->publisher->name;
}

sub is_prolific {
    my $self = shift;
    return $self->books->length > 1;
}

package # hide from PAUSE
    Book;
use Moo;
has title      => ( is => "lazy" );
has genre      => ( is => "lazy" );
has page_count => ( is => "lazy" );
has price      => ( is => "lazy" );
sub _build_price { 10 }
has author => ( is => "rw", weak_ref => 1 );

sub price_with_tax {
    my $self = shift;
    my ($tax_percent) = @_;
    return $self->price + ( $self->price * $tax_percent );
}

sub title_uc {
    my $self = shift;
    return $self->title->uc;
}

package # hide from PAUSE
    Publisher;
use Moo;
has name => ( is => "lazy" );
sub _build_authors { [] }



package # hide from PAUSE
    Literature;
use autobox::Core;

sub literature {

    my $p_orbit    = Publisher->new({ name => "Orbit" });
    my $p_zeus     = Publisher->new({ name => "Head of Zeus" });
    my $p_gollancz = Publisher->new({ name => "Gollanz" });

    # Corey
    my $b_leviathan = Book->new({
        title      => "Leviathan Wakes",
        genre      => "Sci-fi",
        page_count => 342,
        price      => 6,
    });
    my $b_caliban = Book->new({
        title      => "Caliban's War",
        genre      => "Sci-fi",
        page_count => 430,
        price      => 6,
    });

    # Liu
    my $b_three = Book->new({
        title      => "The Tree-Body Problem",
        genre      => "Sci-fi",
        page_count => 400,
        price      => 5,
    });

    # Rothfuss
    my $b_wind = Book->new({
        title      => "The Name of the Wind",
        genre      => "Fantasy",
        page_count => 676,
        price      => 11,
    });

    my $a_corey = Author->new({
        name      => "James A. Corey",
        publisher => $p_orbit,
        books     => [ $b_leviathan, $b_caliban ],
    });
    my $a_liu = Author->new({
        name      => "Cixin Liu",
        publisher => $p_zeus,
        books     => [ $b_three ],
    });
    my $a_rothfuss = Author->new({
        name      => "Patrick Rothfuss",
        publisher => $p_gollancz,
        books     => [ $b_wind ],
    });


    my $reviews = [
        {
            id    => 1,
            score => 7,
        },
        {
            id    => 2,
            score => 6,
        },
        {
            id    => 3,
            score => 9,
        },
    ];

    my $literature = {
        books      => ( my $books = [ $b_leviathan, $b_caliban, $b_three, $b_wind ] ),
        authors    => ( my $authors = [ $a_corey, $a_liu, $a_rothfuss ] ),
        publishers => ( my $publishers = [ $p_orbit, $p_zeus, $p_gollancz ] ),
        reviews    => $reviews,
    };

    for my $author (@$authors) {
        $_->author( $author ) for ( $author->books->elements );
    }

    return $literature;
}

1;