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   621905 use strict;
  8         80  
  8         223  
3 8     8   34 use warnings;
  8         16  
  8         173  
4 8     8   36 use utf8;
  8         11  
  8         52  
5              
6             our $VERSION = "0.09";
7              
8 8     8   576 use constant VERSION_FORMAT => qr/(?<major>[0-9]+)(?:\.(?<minor>[0-9]+))?(?:\.(?<point>[0-9]+))?/;
  8         14  
  8         811  
9 8         710 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     8   51 };
  8         24  
22              
23             use overload (
24 8         63 q{""} => \&as_string,
25             q{<=>} => \&_cmp,
26             fallback => 1,
27 8     8   48 );
  8         11  
28              
29 18     18 1 155 sub major { shift->{major} }
30 18     18 1 65 sub minor { shift->{minor} }
31              
32             sub new {
33 10     10 1 13830 my $class = shift;
34 10         19 my $self = bless {} => $class;
35              
36 10 50       29 $self->_init_by_current_version if @_ == 0;
37 10 50       39 $self->_init_by_version_string(@_) if @_ == 1;
38 10 50       31 $self->_init_by_version_numbers(@_) if @_ >= 2;
39              
40 10         26 return $self;
41             }
42              
43             sub _init_by_current_version {
44 2     2   4053 my $self = shift;
45 2         3 my $command = '/usr/bin/sw_vers -productVersion';
46 2         6 my $version = `$command`;
47              
48 2 100       20 die "Command \`$command\` failed: $version (exit code: $?)\n" if $? != 0;
49              
50 1         6 $self->_init_by_version_string($version);
51             }
52              
53             sub _init_by_version_string {
54 26     26   13433 my ($self, $string) = @_;
55              
56 26 100       76 if (defined MAC_VERSION_NAMES->{$string}) {
57 11         22 $string = MAC_VERSION_NAMES->{$string};
58             }
59              
60 26 100       33 die "Invalid format: $string\n" unless $string =~ qr/^@{[VERSION_FORMAT]}$/;
  26         529  
61              
62 8     8   6619 $self->{major} = $+{major};
  8         2749  
  8         1944  
  25         361  
63 25   100     158 $self->{minor} = $+{minor} // 0;
64             }
65              
66             sub _init_by_version_numbers {
67 3     3   1116 my ($self, $major, $minor) = @_;
68              
69 3   100     32 $self->{major} = $major // 0;
70 3   100     13 $self->{minor} = $minor // 0;
71             }
72              
73             sub name {
74 2     2 0 8 my $self = shift;
75 2         4 my %map = reverse %{ MAC_VERSION_NAMES() };
  2         34  
76 2         11 return $map{$self->as_string};
77             }
78              
79             sub as_string {
80 4     4 1 11 my $self = shift;
81 4         27 return $self->{major}.'.'.$self->{minor};
82             }
83              
84             sub _cmp {
85 4     4   1463 my ($self, $other) = @_;
86              
87 4 100       49 return $self->{major} <=> $other->{major} if $self->{major} != $other->{major};
88 2         36 return $self->{minor} <=> $other->{minor};
89             }
90              
91             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-2019 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