File Coverage

lib/Mac/OSVersion/Lite.pm
Criterion Covered Total %
statement 53 53 100.0
branch 11 14 78.5
condition 6 6 100.0
subroutine 16 16 100.0
pod 4 5 80.0
total 90 94 95.7


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