| 1 | package Venus::Role::Boxable; | ||||||||||
| 2 | |||||||||||
| 3 | 119 | ▶ | use 5.018; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | |||||||||||
| 5 | 119 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 119 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | # IMPORTS | ||||||||||
| 9 | |||||||||||
| 10 | 119 | ▶ | use Venus::Role 'with'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | # METHODS | ||||||||||
| 13 | |||||||||||
| 14 | 4 | ▶ | sub box { | ||||||||
|
Subroutine
box: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 15 | 4 | my ($self, $method, @args) = @_; | |||||||||
| 16 | |||||||||||
| 17 | 4 | require Venus::Box; | |||||||||
| 18 | |||||||||||
| 19 | 4 | local $_ = $self; | |||||||||
| 20 | |||||||||||
| 21 | 4 | ▶ | my $value = $method ? $self->$method(@args) : $self; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 22 | |||||||||||
| 23 | 4 | return Venus::Box->new(value => $value); | |||||||||
| 24 | } | ||||||||||
| 25 | |||||||||||
| 26 | 2 | ▶ | sub boxed { | ||||||||
|
Subroutine
boxed: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 27 | 2 | my ($self, @args) = @_; | |||||||||
| 28 | |||||||||||
| 29 | 2 | return $self->box(@args)->unbox; | |||||||||
| 30 | } | ||||||||||
| 31 | |||||||||||
| 32 | # EXPORTS | ||||||||||
| 33 | |||||||||||
| 34 | 122 | ▶ | sub EXPORT { | ||||||||
|
Subroutine
EXPORT: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 35 | 122 | ['box', 'boxed'] | |||||||||
| 36 | } | ||||||||||
| 37 | |||||||||||
| 38 | 119 | 1; | |||||||||
| 39 | |||||||||||
| 40 | |||||||||||
| 41 | |||||||||||
| 42 | =head1 NAME | ||||||||||
| 43 | |||||||||||
| 44 | Venus::Role::Boxable - Boxable Role | ||||||||||
| 45 | |||||||||||
| 46 | =cut | ||||||||||
| 47 | |||||||||||
| 48 | =head1 ABSTRACT | ||||||||||
| 49 | |||||||||||
| 50 | Boxable Role for Perl 5 | ||||||||||
| 51 | |||||||||||
| 52 | =cut | ||||||||||
| 53 | |||||||||||
| 54 | =head1 SYNOPSIS | ||||||||||
| 55 | |||||||||||
| 56 | package Example; | ||||||||||
| 57 | |||||||||||
| 58 | use Venus::Class; | ||||||||||
| 59 | |||||||||||
| 60 | with 'Venus::Role::Boxable'; | ||||||||||
| 61 | |||||||||||
| 62 | attr 'text'; | ||||||||||
| 63 | |||||||||||
| 64 | package main; | ||||||||||
| 65 | |||||||||||
| 66 | my $example = Example->new(text => 'hello, world'); | ||||||||||
| 67 | |||||||||||
| 68 | # $example->box('text')->lc->ucfirst->concat('.')->unbox->get; | ||||||||||
| 69 | |||||||||||
| 70 | # "Hello, world." | ||||||||||
| 71 | |||||||||||
| 72 | =cut | ||||||||||
| 73 | |||||||||||
| 74 | =head1 DESCRIPTION | ||||||||||
| 75 | |||||||||||
| 76 | This package modifies the consuming package and provides a method for boxing | ||||||||||
| 77 | itself. This makes it possible to chain method calls across objects and values. | ||||||||||
| 78 | |||||||||||
| 79 | =cut | ||||||||||
| 80 | |||||||||||
| 81 | =head1 METHODS | ||||||||||
| 82 | |||||||||||
| 83 | This package provides the following methods: | ||||||||||
| 84 | |||||||||||
| 85 | =cut | ||||||||||
| 86 | |||||||||||
| 87 | =head2 box | ||||||||||
| 88 | |||||||||||
| 89 | box(string | coderef $method, any @args) (object) | ||||||||||
| 90 | |||||||||||
| 91 | The box method returns the invocant boxed, i.e. encapsulated, using | ||||||||||
| 92 | L<Venus::Box>. This method supports dispatching, i.e. providing a method name | ||||||||||
| 93 | and arguments whose return value will be acted on by this method. | ||||||||||
| 94 | |||||||||||
| 95 | I<Since C<0.01>> | ||||||||||
| 96 | |||||||||||
| 97 | =over 4 | ||||||||||
| 98 | |||||||||||
| 99 | =item box example 1 | ||||||||||
| 100 | |||||||||||
| 101 | package main; | ||||||||||
| 102 | |||||||||||
| 103 | my $example = Example->new(text => 'hello, world'); | ||||||||||
| 104 | |||||||||||
| 105 | my $box = $example->box; | ||||||||||
| 106 | |||||||||||
| 107 | # bless({ value => bless(..., "Example") }, "Venus::Box") | ||||||||||
| 108 | |||||||||||
| 109 | =back | ||||||||||
| 110 | |||||||||||
| 111 | =over 4 | ||||||||||
| 112 | |||||||||||
| 113 | =item box example 2 | ||||||||||
| 114 | |||||||||||
| 115 | package main; | ||||||||||
| 116 | |||||||||||
| 117 | my $example = Example->new(text => 'hello, world'); | ||||||||||
| 118 | |||||||||||
| 119 | my $box = $example->box('text'); | ||||||||||
| 120 | |||||||||||
| 121 | # bless({ value => bless(..., "Venus::String") }, "Venus::Box") | ||||||||||
| 122 | |||||||||||
| 123 | # $example->box('text')->lc->ucfirst->concat('.')->unbox->get; | ||||||||||
| 124 | |||||||||||
| 125 | =back | ||||||||||
| 126 | |||||||||||
| 127 | =cut | ||||||||||
| 128 | |||||||||||
| 129 | =head2 boxed | ||||||||||
| 130 | |||||||||||
| 131 | boxed(string | coderef $method, any @args) (object) | ||||||||||
| 132 | |||||||||||
| 133 | The boxed method dispatches to L</box> and returns the "unboxed" value. This | ||||||||||
| 134 | method supports dispatching, i.e. providing a method name and arguments whose | ||||||||||
| 135 | return value will be acted on by this method. | ||||||||||
| 136 | |||||||||||
| 137 | I<Since C<4.15>> | ||||||||||
| 138 | |||||||||||
| 139 | =over 4 | ||||||||||
| 140 | |||||||||||
| 141 | =item boxed example 1 | ||||||||||
| 142 | |||||||||||
| 143 | # given: synopsis | ||||||||||
| 144 | |||||||||||
| 145 | package main; | ||||||||||
| 146 | |||||||||||
| 147 | my $boxed = $example->boxed; | ||||||||||
| 148 | |||||||||||
| 149 | # bless(..., "Example") | ||||||||||
| 150 | |||||||||||
| 151 | =back | ||||||||||
| 152 | |||||||||||
| 153 | =over 4 | ||||||||||
| 154 | |||||||||||
| 155 | =item boxed example 2 | ||||||||||
| 156 | |||||||||||
| 157 | # given: synopsis | ||||||||||
| 158 | |||||||||||
| 159 | package main; | ||||||||||
| 160 | |||||||||||
| 161 | my $boxed = $example->boxed('text'); | ||||||||||
| 162 | |||||||||||
| 163 | # bless({value => 'hello world'}, "Venus::String") | ||||||||||
| 164 | |||||||||||
| 165 | =back | ||||||||||
| 166 | |||||||||||
| 167 | =cut | ||||||||||
| 168 | |||||||||||
| 169 | =head1 AUTHORS | ||||||||||
| 170 | |||||||||||
| 171 | Awncorp, C<awncorp@cpan.org> | ||||||||||
| 172 | |||||||||||
| 173 | =cut | ||||||||||
| 174 | |||||||||||
| 175 | =head1 LICENSE | ||||||||||
| 176 | |||||||||||
| 177 | Copyright (C) 2022, Awncorp, C<awncorp@cpan.org>. | ||||||||||
| 178 | |||||||||||
| 179 | This program is free software, you can redistribute it and/or modify it under | ||||||||||
| 180 | the terms of the Apache license version 2.0. | ||||||||||
| 181 | |||||||||||
| 182 | =cut | ||||||||||