| 1 | package Venus::Role::Fromable; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 1 | ▶ | use 5.018; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 1 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | # IMPORTS | ||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | 1 | ▶ | use Venus::Role 'fault'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | # METHODS | ||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | 11 | ▶ | sub from { | ||||||||||||||||||
|
Subroutine
from: called CC=5 SCAR=16.2
Pod
documented
| |||||||||||||||||||||
| 15 | 11 | my ($self, @args) = @_; | |||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | 11 | require Venus::What; | |||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | 11 | my $type = lc scalar Venus::What->new(value => $args[0])->identify; | |||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | 11 | ▶ | (my $name, @args) = @args < 2 ? ($type, @args) : (@args); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | 11 | ▶ | my $class = ref $self || $self; | ||||||||||||||||||
|
Condition33%1/3
ref $self || $self
MC/DC0%0/2
ref $self || $self
ref $self
$self
Truth table33%1/3
A || B
A= ref $selfB= $self
| |||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | 11 | ▶ | fault "No name provided to \"from\" via package \"$class\"" if !$name; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | 9 | my $method = "from_$name"; | |||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | 9 | ▶ | return $class->new($class->$method(@args)) if $class->can($method); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | 2 | fault "Unable to locate class method \"$method\" via package \"$class\""; | |||||||||||||||||||
| 32 | } | ||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | # EXPORTS | ||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | 6 | ▶ | sub EXPORT { | ||||||||||||||||||
|
Subroutine
EXPORT: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 37 | 6 | ['from'] | |||||||||||||||||||
| 38 | } | ||||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | 1 | 1; | |||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | =head1 NAME | ||||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | Venus::Role::Fromable - Fromable Role | ||||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | =cut | ||||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | =head1 ABSTRACT | ||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | Fromable Role for Perl 5 | ||||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | =cut | ||||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | =head1 SYNOPSIS | ||||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | package Person; | ||||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | use Venus::Class 'attr', 'with'; | ||||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | with 'Venus::Role::Fromable'; | ||||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | attr 'fname'; | ||||||||||||||||||||
| 65 | attr 'lname'; | ||||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | sub from_name { | ||||||||||||||||||||
| 68 | my ($self, $name) = @_; | ||||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | my ($fname, $lname) = split / /, $name; | ||||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | return { | ||||||||||||||||||||
| 73 | fname => $fname, | ||||||||||||||||||||
| 74 | lname => $lname, | ||||||||||||||||||||
| 75 | }; | ||||||||||||||||||||
| 76 | } | ||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | package main; | ||||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | my $person = Person->from(name => 'Elliot Alderson'); | ||||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | # bless({fname => 'Elliot', lname => 'Alderson'}, 'Person') | ||||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | =cut | ||||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | =head1 DESCRIPTION | ||||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | This package modifies the consuming package and provides methods for | ||||||||||||||||||||
| 89 | dispatching to constructor argument builders. | ||||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | =cut | ||||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | =head1 METHODS | ||||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | This package provides the following methods: | ||||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | =cut | ||||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | =head2 from | ||||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | from(any @values) (object) | ||||||||||||||||||||
| 102 | |||||||||||||||||||||
| 103 | The from method takes a key and value(s) and dispatches to the corresponding | ||||||||||||||||||||
| 104 | argument builder named in the form of C<from_${name}> which should return | ||||||||||||||||||||
| 105 | arguments required by the constructor. The constructor will be called with the | ||||||||||||||||||||
| 106 | arguments returned from the argument builder and a class instance will be | ||||||||||||||||||||
| 107 | returned. If the key is omitted, the data type of the first value will be used | ||||||||||||||||||||
| 108 | as the key (or name), i.e. if the daya type of the first value is a string this | ||||||||||||||||||||
| 109 | method will attempt to dispatch to a builder named C<from_string>. | ||||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | I<Since C<4.15>> | ||||||||||||||||||||
| 112 | |||||||||||||||||||||
| 113 | =over 4 | ||||||||||||||||||||
| 114 | |||||||||||||||||||||
| 115 | =item from example 1 | ||||||||||||||||||||
| 116 | |||||||||||||||||||||
| 117 | # given: synopsis; | ||||||||||||||||||||
| 118 | |||||||||||||||||||||
| 119 | $person = Person->from(name => 'Elliot Alderson'); | ||||||||||||||||||||
| 120 | |||||||||||||||||||||
| 121 | # bless({fname => 'Elliot', lname => 'Alderson'}, 'Person') | ||||||||||||||||||||
| 122 | |||||||||||||||||||||
| 123 | # $person->fname; | ||||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | # "Elliot" | ||||||||||||||||||||
| 126 | |||||||||||||||||||||
| 127 | # $person->lname; | ||||||||||||||||||||
| 128 | |||||||||||||||||||||
| 129 | # "Alderson" | ||||||||||||||||||||
| 130 | |||||||||||||||||||||
| 131 | =back | ||||||||||||||||||||
| 132 | |||||||||||||||||||||
| 133 | =over 4 | ||||||||||||||||||||
| 134 | |||||||||||||||||||||
| 135 | =item from example 2 | ||||||||||||||||||||
| 136 | |||||||||||||||||||||
| 137 | # given: synopsis; | ||||||||||||||||||||
| 138 | |||||||||||||||||||||
| 139 | $person = Person->from('', 'Elliot Alderson'); | ||||||||||||||||||||
| 140 | |||||||||||||||||||||
| 141 | # Exception! "No name provided to \"from\" via package \"Person\"" | ||||||||||||||||||||
| 142 | |||||||||||||||||||||
| 143 | =back | ||||||||||||||||||||
| 144 | |||||||||||||||||||||
| 145 | =over 4 | ||||||||||||||||||||
| 146 | |||||||||||||||||||||
| 147 | =item from example 3 | ||||||||||||||||||||
| 148 | |||||||||||||||||||||
| 149 | # given: synopsis; | ||||||||||||||||||||
| 150 | |||||||||||||||||||||
| 151 | $person = Person->from(undef, 'Elliot Alderson'); | ||||||||||||||||||||
| 152 | |||||||||||||||||||||
| 153 | # Exception! "No name provided to \"from\" via package \"Person\"" | ||||||||||||||||||||
| 154 | |||||||||||||||||||||
| 155 | =back | ||||||||||||||||||||
| 156 | |||||||||||||||||||||
| 157 | =over 4 | ||||||||||||||||||||
| 158 | |||||||||||||||||||||
| 159 | =item from example 4 | ||||||||||||||||||||
| 160 | |||||||||||||||||||||
| 161 | # given: synopsis; | ||||||||||||||||||||
| 162 | |||||||||||||||||||||
| 163 | $person = Person->from('fullname', 'Elliot Alderson'); | ||||||||||||||||||||
| 164 | |||||||||||||||||||||
| 165 | # Exception! "Unable to locate class method \"from_fullname\" via package \"Person\"" | ||||||||||||||||||||
| 166 | |||||||||||||||||||||
| 167 | =back | ||||||||||||||||||||
| 168 | |||||||||||||||||||||
| 169 | =over 4 | ||||||||||||||||||||
| 170 | |||||||||||||||||||||
| 171 | =item from example 5 | ||||||||||||||||||||
| 172 | |||||||||||||||||||||
| 173 | # given: synopsis; | ||||||||||||||||||||
| 174 | |||||||||||||||||||||
| 175 | $person = Person->from('Elliot Alderson'); | ||||||||||||||||||||
| 176 | |||||||||||||||||||||
| 177 | # Exception! "Unable to locate class method \"from_string\" via package \"Person\"" | ||||||||||||||||||||
| 178 | |||||||||||||||||||||
| 179 | =back | ||||||||||||||||||||
| 180 | |||||||||||||||||||||
| 181 | =cut | ||||||||||||||||||||
| 182 | |||||||||||||||||||||
| 183 | =head1 AUTHORS | ||||||||||||||||||||
| 184 | |||||||||||||||||||||
| 185 | Awncorp, C<awncorp@cpan.org> | ||||||||||||||||||||
| 186 | |||||||||||||||||||||
| 187 | =cut | ||||||||||||||||||||
| 188 | |||||||||||||||||||||
| 189 | =head1 LICENSE | ||||||||||||||||||||
| 190 | |||||||||||||||||||||
| 191 | Copyright (C) 2022, Awncorp, C<awncorp@cpan.org>. | ||||||||||||||||||||
| 192 | |||||||||||||||||||||
| 193 | This program is free software, you can redistribute it and/or modify it under | ||||||||||||||||||||
| 194 | the terms of the Apache license version 2.0. | ||||||||||||||||||||
| 195 | |||||||||||||||||||||
| 196 | =cut | ||||||||||||||||||||