| 1 | package Venus::Kind::Value; | ||||||||||
| 2 | |||||||||||
| 3 | 58 | ▶ | use 5.018; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | |||||||||||
| 5 | 58 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 58 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | # IMPORTS | ||||||||||
| 9 | |||||||||||
| 10 | 58 | ▶ | use Venus::Class 'base', 'with'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | # INHERITS | ||||||||||
| 13 | |||||||||||
| 14 | 58 | base 'Venus::Kind'; | |||||||||
| 15 | |||||||||||
| 16 | # INTEGRATES | ||||||||||
| 17 | |||||||||||
| 18 | 58 | with 'Venus::Role::Valuable'; | |||||||||
| 19 | 58 | with 'Venus::Role::Buildable'; | |||||||||
| 20 | 58 | with 'Venus::Role::Accessible'; | |||||||||
| 21 | 58 | with 'Venus::Role::Explainable'; | |||||||||
| 22 | 58 | with 'Venus::Role::Proxyable'; | |||||||||
| 23 | 58 | with 'Venus::Role::Pluggable'; | |||||||||
| 24 | |||||||||||
| 25 | # OVERLOADS | ||||||||||
| 26 | |||||||||||
| 27 | 58 | ▶ | use overload ( | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 28 | 58 | '""' => 'explain', | |||||||||
| 29 | '~~' => 'explain', | ||||||||||
| 30 | fallback => 1, | ||||||||||
| 31 | 58 | ); | |||||||||
| 32 | |||||||||||
| 33 | # BUILDERS | ||||||||||
| 34 | |||||||||||
| 35 | 3478 | ▶ | sub build_arg { | ||||||||
|
Subroutine
build_arg: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 36 | 3478 | my ($self, $data) = @_; | |||||||||
| 37 | |||||||||||
| 38 | return { | ||||||||||
| 39 | 3478 | value => $data, | |||||||||
| 40 | }; | ||||||||||
| 41 | } | ||||||||||
| 42 | |||||||||||
| 43 | # METHODS | ||||||||||
| 44 | |||||||||||
| 45 | 103 | ▶ | sub cast { | ||||||||
|
Subroutine
cast: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||
| 46 | 103 | my ($self, @args) = @_; | |||||||||
| 47 | |||||||||||
| 48 | 103 | require Venus::What; | |||||||||
| 49 | |||||||||||
| 50 | 103 | ▶ | my $value = $self->can('value') ? $self->value : $self; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 51 | |||||||||||
| 52 | 103 | return Venus::What->new(value => $value)->cast(@args); | |||||||||
| 53 | } | ||||||||||
| 54 | |||||||||||
| 55 | 2 | ▶ | sub defined { | ||||||||
|
Subroutine
defined: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 56 | 2 | my ($self) = @_; | |||||||||
| 57 | |||||||||||
| 58 | 2 | ▶ | return CORE::defined($self->get) ? true : false; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 59 | } | ||||||||||
| 60 | |||||||||||
| 61 | 578 | ▶ | sub explain { | ||||||||
|
Subroutine
explain: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 62 | 578 | my ($self) = @_; | |||||||||
| 63 | |||||||||||
| 64 | 578 | return $self->get; | |||||||||
| 65 | } | ||||||||||
| 66 | |||||||||||
| 67 | 2 | ▶ | sub mutate { | ||||||||
|
Subroutine
mutate: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 68 | 2 | my ($self, $code, @args) = @_; | |||||||||
| 69 | |||||||||||
| 70 | 2 | return $self->set($self->$code(@args)); | |||||||||
| 71 | } | ||||||||||
| 72 | |||||||||||
| 73 | 0 | ▶ | sub TO_JSON { | ||||||||
|
Subroutine
TO_JSON: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 74 | 0 | my ($self) = @_; | |||||||||
| 75 | |||||||||||
| 76 | 0 | return $self->get; | |||||||||
| 77 | } | ||||||||||
| 78 | |||||||||||
| 79 | 58 | 1; | |||||||||
| 80 | |||||||||||
| 81 | |||||||||||
| 82 | |||||||||||
| 83 | =head1 NAME | ||||||||||
| 84 | |||||||||||
| 85 | Venus::Kind::Value - Value Base Class | ||||||||||
| 86 | |||||||||||
| 87 | =cut | ||||||||||
| 88 | |||||||||||
| 89 | =head1 ABSTRACT | ||||||||||
| 90 | |||||||||||
| 91 | Value Base Class for Perl 5 | ||||||||||
| 92 | |||||||||||
| 93 | =cut | ||||||||||
| 94 | |||||||||||
| 95 | =head1 SYNOPSIS | ||||||||||
| 96 | |||||||||||
| 97 | package Example; | ||||||||||
| 98 | |||||||||||
| 99 | use Venus::Class; | ||||||||||
| 100 | |||||||||||
| 101 | base 'Venus::Kind::Value'; | ||||||||||
| 102 | |||||||||||
| 103 | sub test { | ||||||||||
| 104 | $_[0]->get + 1 | ||||||||||
| 105 | } | ||||||||||
| 106 | |||||||||||
| 107 | package main; | ||||||||||
| 108 | |||||||||||
| 109 | my $example = Example->new(1); | ||||||||||
| 110 | |||||||||||
| 111 | # $example->defined; | ||||||||||
| 112 | |||||||||||
| 113 | =cut | ||||||||||
| 114 | |||||||||||
| 115 | =head1 DESCRIPTION | ||||||||||
| 116 | |||||||||||
| 117 | This package provides identity and methods common across all L<Venus> value | ||||||||||
| 118 | classes. | ||||||||||
| 119 | |||||||||||
| 120 | =cut | ||||||||||
| 121 | |||||||||||
| 122 | =head1 INHERITS | ||||||||||
| 123 | |||||||||||
| 124 | This package inherits behaviors from: | ||||||||||
| 125 | |||||||||||
| 126 | L<Venus::Kind> | ||||||||||
| 127 | |||||||||||
| 128 | =cut | ||||||||||
| 129 | |||||||||||
| 130 | =head1 INTEGRATES | ||||||||||
| 131 | |||||||||||
| 132 | This package integrates behaviors from: | ||||||||||
| 133 | |||||||||||
| 134 | L<Venus::Role::Accessible> | ||||||||||
| 135 | |||||||||||
| 136 | L<Venus::Role::Buildable> | ||||||||||
| 137 | |||||||||||
| 138 | L<Venus::Role::Explainable> | ||||||||||
| 139 | |||||||||||
| 140 | L<Venus::Role::Pluggable> | ||||||||||
| 141 | |||||||||||
| 142 | L<Venus::Role::Proxyable> | ||||||||||
| 143 | |||||||||||
| 144 | L<Venus::Role::Valuable> | ||||||||||
| 145 | |||||||||||
| 146 | =cut | ||||||||||
| 147 | |||||||||||
| 148 | =head1 METHODS | ||||||||||
| 149 | |||||||||||
| 150 | This package provides the following methods: | ||||||||||
| 151 | |||||||||||
| 152 | =cut | ||||||||||
| 153 | |||||||||||
| 154 | =head2 cast | ||||||||||
| 155 | |||||||||||
| 156 | cast(string $kind) (object | undef) | ||||||||||
| 157 | |||||||||||
| 158 | The cast method converts L<"value"|Venus::Kind::Value> objects between | ||||||||||
| 159 | different I<"value"> object types, based on the name of the type provided. This | ||||||||||
| 160 | method will return C<undef> if the invocant is not a L<Venus::Kind::Value>. | ||||||||||
| 161 | |||||||||||
| 162 | I<Since C<0.08>> | ||||||||||
| 163 | |||||||||||
| 164 | =over 4 | ||||||||||
| 165 | |||||||||||
| 166 | =item cast example 1 | ||||||||||
| 167 | |||||||||||
| 168 | package main; | ||||||||||
| 169 | |||||||||||
| 170 | my $example = Example->new; | ||||||||||
| 171 | |||||||||||
| 172 | my $cast = $example->cast; | ||||||||||
| 173 | |||||||||||
| 174 | # bless({value => undef}, "Venus::Undef") | ||||||||||
| 175 | |||||||||||
| 176 | =back | ||||||||||
| 177 | |||||||||||
| 178 | =over 4 | ||||||||||
| 179 | |||||||||||
| 180 | =item cast example 2 | ||||||||||
| 181 | |||||||||||
| 182 | package main; | ||||||||||
| 183 | |||||||||||
| 184 | my $example = Example->new( | ||||||||||
| 185 | value => 123.45, | ||||||||||
| 186 | ); | ||||||||||
| 187 | |||||||||||
| 188 | my $cast = $example->cast('array'); | ||||||||||
| 189 | |||||||||||
| 190 | # bless({value => [123.45]}, "Venus::Array") | ||||||||||
| 191 | |||||||||||
| 192 | =back | ||||||||||
| 193 | |||||||||||
| 194 | =over 4 | ||||||||||
| 195 | |||||||||||
| 196 | =item cast example 3 | ||||||||||
| 197 | |||||||||||
| 198 | package main; | ||||||||||
| 199 | |||||||||||
| 200 | my $example = Example->new( | ||||||||||
| 201 | value => 123.45, | ||||||||||
| 202 | ); | ||||||||||
| 203 | |||||||||||
| 204 | my $cast = $example->cast('hash'); | ||||||||||
| 205 | |||||||||||
| 206 | # bless({value => {'123.45' => 123.45}, "Venus::Hash") | ||||||||||
| 207 | |||||||||||
| 208 | =back | ||||||||||
| 209 | |||||||||||
| 210 | =cut | ||||||||||
| 211 | |||||||||||
| 212 | =head2 defined | ||||||||||
| 213 | |||||||||||
| 214 | defined() (number) | ||||||||||
| 215 | |||||||||||
| 216 | The defined method returns truthy or falsy if the underlying value is | ||||||||||
| 217 | L</defined>. | ||||||||||
| 218 | |||||||||||
| 219 | I<Since C<0.01>> | ||||||||||
| 220 | |||||||||||
| 221 | =over 4 | ||||||||||
| 222 | |||||||||||
| 223 | =item defined example 1 | ||||||||||
| 224 | |||||||||||
| 225 | package main; | ||||||||||
| 226 | |||||||||||
| 227 | my $example = Example->new; | ||||||||||
| 228 | |||||||||||
| 229 | my $defined = $example->defined; | ||||||||||
| 230 | |||||||||||
| 231 | # 0 | ||||||||||
| 232 | |||||||||||
| 233 | =back | ||||||||||
| 234 | |||||||||||
| 235 | =over 4 | ||||||||||
| 236 | |||||||||||
| 237 | =item defined example 2 | ||||||||||
| 238 | |||||||||||
| 239 | package main; | ||||||||||
| 240 | |||||||||||
| 241 | my $example = Example->new(time); | ||||||||||
| 242 | |||||||||||
| 243 | my $defined = $example->defined; | ||||||||||
| 244 | |||||||||||
| 245 | # 1 | ||||||||||
| 246 | |||||||||||
| 247 | =back | ||||||||||
| 248 | |||||||||||
| 249 | =cut | ||||||||||
| 250 | |||||||||||
| 251 | =head2 explain | ||||||||||
| 252 | |||||||||||
| 253 | explain() (any) | ||||||||||
| 254 | |||||||||||
| 255 | The explain method returns the value set and is used in stringification | ||||||||||
| 256 | operations. | ||||||||||
| 257 | |||||||||||
| 258 | I<Since C<0.01>> | ||||||||||
| 259 | |||||||||||
| 260 | =over 4 | ||||||||||
| 261 | |||||||||||
| 262 | =item explain example 1 | ||||||||||
| 263 | |||||||||||
| 264 | package main; | ||||||||||
| 265 | |||||||||||
| 266 | my $example = Example->new('hello, there'); | ||||||||||
| 267 | |||||||||||
| 268 | my $explain = $example->explain; | ||||||||||
| 269 | |||||||||||
| 270 | # "hello, there" | ||||||||||
| 271 | |||||||||||
| 272 | =back | ||||||||||
| 273 | |||||||||||
| 274 | =cut | ||||||||||
| 275 | |||||||||||
| 276 | =head2 mutate | ||||||||||
| 277 | |||||||||||
| 278 | mutate(string | coderef $code, any @args) (object) | ||||||||||
| 279 | |||||||||||
| 280 | The mutate method dispatches the method call or executes the callback and | ||||||||||
| 281 | returns the result, which if is of the same type as the invocant's underlying | ||||||||||
| 282 | data type will update the object's internal state or will throw an exception. | ||||||||||
| 283 | |||||||||||
| 284 | I<Since C<1.23>> | ||||||||||
| 285 | |||||||||||
| 286 | =over 4 | ||||||||||
| 287 | |||||||||||
| 288 | =item mutate example 1 | ||||||||||
| 289 | |||||||||||
| 290 | # given: synopsis | ||||||||||
| 291 | |||||||||||
| 292 | package main; | ||||||||||
| 293 | |||||||||||
| 294 | $example->mutate('test'); | ||||||||||
| 295 | |||||||||||
| 296 | $example; | ||||||||||
| 297 | |||||||||||
| 298 | # bless({value => 2}, "Example") | ||||||||||
| 299 | |||||||||||
| 300 | =back | ||||||||||
| 301 | |||||||||||
| 302 | =cut | ||||||||||
| 303 | |||||||||||
| 304 | =head1 AUTHORS | ||||||||||
| 305 | |||||||||||
| 306 | Awncorp, C<awncorp@cpan.org> | ||||||||||
| 307 | |||||||||||
| 308 | =cut | ||||||||||
| 309 | |||||||||||
| 310 | =head1 LICENSE | ||||||||||
| 311 | |||||||||||
| 312 | Copyright (C) 2022, Awncorp, C<awncorp@cpan.org>. | ||||||||||
| 313 | |||||||||||
| 314 | This program is free software, you can redistribute it and/or modify it under | ||||||||||
| 315 | the terms of the Apache license version 2.0. | ||||||||||
| 316 | |||||||||||
| 317 | =cut | ||||||||||