lib/Mac/OSVersion/Lite.pm

filter: Statementstmt 100% 52 / 52 Branchbran 78.5% 11 / 14 Conditioncond 100% 6 / 6 MC/DCmcdc 100% 3 / 3 Subroutinesub 100% 15 / 15 Podpod 80.0% 4 / 5 totaltotal 95.7% 91 / 95 SCAR 24.0
CC
11
Cov
96%
new
4.2
_init_by_version_string
4.0
_init_by_version_numbers
3.0
CRAP 11.0
« blib/lib/Mac/OSVersion/Lite.pm ↑ Summary Build »
1 package Mac::OSVersion::Lite;
2 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 8 our $VERSION = "0.08";
7  
8 8 ▶use constant VERSION_FORMAT => qr/(?<major>[0-9]+)(?:\.(?<minor>[0-9]+))?(?:\.(?<point>[0-9]+))?/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 8 use constant MAC_VERSION_NAMES => {
10 mojave => "10.14",
11 high_sierra => "10.13",
12 sierra => "10.12",
13 el_capitan => "10.11",
14 yosemite => "10.10",
15 mavericks => "10.9",
16 mountain_lion => "10.8",
17 lion => "10.7",
18 snow_leopard => "10.6",
19 leopard => "10.5",
20 tiger => "10.4",
21 8 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 use overload (
24 8 q{""} => \&as_string,
25 q{<=>} => \&_cmp,
26 fallback => 1,
27 8 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 18 ▶sub major { shift->{major} }
Subroutine major: called CC=1 SCAR=0.0
Pod documented
30 18 ▶sub minor { shift->{minor} }
Subroutine minor: called CC=1 SCAR=0.0
Pod documented
31  
32 sub new {
33 10 ▶ my $class = shift;
Subroutine new: called CC=4 SCAR=14.5
Pod documented
34 10 my $self = bless {} => $class;
35  
36 10 ▶ $self->_init_by_current_version if @_ == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 0 0 10 10
37 10 ▶ $self->_init_by_version_string(@_) if @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 10 0 10
38 10 ▶ $self->_init_by_version_numbers(@_) if @_ >= 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ >= 2 0 10 10
39  
40 10 return $self;
41 }
42  
43 sub _init_by_current_version {
44 2 ▶ my $self = shift;
Subroutine _init_by_current_version: called CC=2 SCAR=6.9
45 2 my $command = '/usr/bin/sw_vers -productVersion';
46 2 my $version = `$command`;
47  
48 2 ▶ die "Command \`$command\` failed: $version (exit code: $?)\n" if $? != 0;
Branch: 1 branch
BranchTrueFalseTotal
if $? != 0 1 1 2
49  
50 1 $self->_init_by_version_string($version);
51 }
52  
53 sub _init_by_version_string {
54 26 ▶ my ($self, $string) = @_;
Subroutine _init_by_version_string: called CC=4 SCAR=13.9
55  
56 26 ▶ if (defined MAC_VERSION_NAMES->{$string}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined {"el_capitan" => "10.11","high_sierra" => "10.13","leopard" => "10.5","lion" => "10.7","mavericks" => "10.9","mojave" => "10.14","mountain_lion" => "10.8","sierra" => "10.12","snow_leopard" => "10.6","tiger" => "10.4","yosemite" => "10.10"}->{$string}) 11 15 26
57 11 $string = MAC_VERSION_NAMES->{$string};
58 }
59  
60 26 ▶ die "Invalid format: $string\n" unless $string =~ qr/^@{[VERSION_FORMAT]}$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $string =~ qr"^@{[qr/(?<major>[0-9]+)(?:\.(?<minor>[0-9]+))?(?:\.(?<point>[0-9]+))?/];}$" 1 25 26
61  
62 25 $self->{major} = $+{major};
63 25 ▶ $self->{minor} = $+{minor} // 0;
Condition100%2/2
$+{"minor"} // 0
l!l
232
MC/DC100%1/1
$+{"minor"} // 0
$+{"minor"}
Truth table100%2/2
A
A= $+{"minor"}
Aresult
11
00
64 }
65  
66 sub _init_by_version_numbers {
67 3 ▶ my ($self, $major, $minor) = @_;
Subroutine _init_by_version_numbers: called CC=3 SCAR=11.0
68  
69 3 ▶ $self->{major} = $major // 0;
Condition100%2/2
$major // 0
l!l
21
MC/DC100%1/1
$major // 0
$major
Truth table100%2/2
A
A= $major
Aresult
11
00
70 3 ▶ $self->{minor} = $minor // 0;
Condition100%2/2
$minor // 0
l!l
12
MC/DC100%1/1
$minor // 0
$minor
Truth table100%2/2
A
A= $minor
Aresult
11
00
71 }
72  
73 sub name {
74 2 ▶ my $self = shift;
Subroutine name: called CC=1 SCAR=0.0
Pod undocumented
75 2 my %map = reverse %{ MAC_VERSION_NAMES() };
76 2 return $map{$self->as_string};
77 }
78  
79 sub as_string {
80 4 ▶ my $self = shift;
Subroutine as_string: called CC=1 SCAR=0.0
Pod documented
81 4 return $self->{major}.'.'.$self->{minor};
82 }
83  
84 sub _cmp {
85 4 ▶ my ($self, $other) = @_;
Subroutine _cmp: called CC=2 SCAR=6.9
86  
87 4 ▶ return $self->{major} <=> $other->{major} if $self->{major} != $other->{major};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"major"} != $other->{"major"} 2 2 4
88 2 return $self->{minor} <=> $other->{minor};
89 }
90  
91 8 1;
92 __END__
93  
94 =encoding utf-8
95  
96 =head1 NAME
97  
98 Mac::OSVersion::Lite - It's the lightweight version object for Mac OS X
99  
100 =head1 SYNOPSIS
101  
102 use Mac::OSVersion::Lite;
103 use feature qw/say/;
104  
105 my $version = Mac::OSVersion::Lite->new;
106 say $version->major; # 10
107 say $version->minor; # 11
108 say $version->name; # el_capitan
109  
110 =head1 DESCRIPTION
111  
112 Mac::OSVersion::Lite is the lightweight version object for Mac OS X with auto detection.
113  
114 =head1 METHODS
115  
116 =head2 CLASS METHODS
117  
118 =head3 C<new()>
119  
120 Create new C<Mac::OSVersion::Lite> instance with auto detection.
121  
122 =head3 C<new($version_string)>
123  
124 Create new C<Mac::OSVersion::Lite> instance from a version string.
125 C<Mac::OSVersion::Lite-E<gt>new('10.11')> equals C<Mac::OSVersion::Lite-E<gt>new(10, 11)>.
126  
127 =head3 C<new($major, $minor = 0)>
128  
129 Create new C<Mac::OSVersion::Lite> instance from version numbers.
130 C<Mac::OSVersion::Lite-E<gt>new(10, 11)> equals C<Mac::OSVersion::Lite-E<gt>new('10.11')>.
131  
132 =head2 METHODS
133  
134 =head3 C<major>
135  
136 Get the major version number.
137  
138 =head3 C<minor>
139  
140 Return the minor version number.
141  
142 =head3 C<E<lt>=E<gt>>
143  
144 Compare two C<SemVer::V2::Strict> instances.
145  
146 =head3 C<"">
147  
148 Convert a C<SemVer::V2::Strict> instance to string.
149  
150 =head3 C<as_string()>
151  
152 Convert a C<SemVer::V2::Strict> instance to string.
153  
154 =head1 SEE ALSO
155  
156 =over
157  
158 =item * L<Mac::OSVersion>
159  
160 =back
161  
162 =head1 LICENSE
163  
164 The MIT License (MIT)
165  
166 Copyright (c) 2017 Pine Mizune
167  
168 Permission is hereby granted, free of charge, to any person obtaining a copy
169 of this software and associated documentation files (the "Software"), to deal
170 in the Software without restriction, including without limitation the rights
171 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
172 copies of the Software, and to permit persons to whom the Software is
173 furnished to do so, subject to the following conditions:
174  
175 The above copyright notice and this permission notice shall be included in
176 all copies or substantial portions of the Software.
177  
178 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
179 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
180 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
181 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
182 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
183 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
184 THE SOFTWARE.
185  
186 =head1 AUTHOR
187  
188 Pine Mizune E<lt>pinemz@gmail.comE<gt>
189  
190 =cut
191  
« blib/lib/Mac/OSVersion/Lite.pm ↑ Summary Build »