File: Location.pm

package info (click to toggle)
libgeoip2-perl 2.006002-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, trixie
  • size: 2,156 kB
  • sloc: perl: 2,275; makefile: 10
file content (125 lines) | stat: -rw-r--r-- 2,771 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package GeoIP2::Role::Model::Location;

use strict;
use warnings;

our $VERSION = '2.006002';

use Moo::Role;

use B;
use GeoIP2::Record::City;
use GeoIP2::Record::Continent;
use GeoIP2::Record::Country;
use GeoIP2::Record::Location;
use GeoIP2::Record::MaxMind;
use GeoIP2::Record::Postal;
use GeoIP2::Record::RepresentedCountry;
use GeoIP2::Record::Traits;
use GeoIP2::Types qw( ArrayRef HashRef );
use Sub::Quote qw( quote_sub );

use namespace::clean;

with 'GeoIP2::Role::Model', 'GeoIP2::Role::HasLocales';

## no critic (ProhibitUnusedPrivateSubroutines)
sub _define_attributes_for_keys {
    my $class = shift;
    my @keys  = @_;

    my $has = $class->can('_has');

    for my $key (@keys) {
        my $record_class = __PACKAGE__->_record_class_for_key($key);

        my $raw_attr = '_raw_' . $key;

        $has->(
            $raw_attr => (
                is       => 'ro',
                isa      => HashRef,
                init_arg => $key,
                default  => quote_sub(q{ {} }),
            ),
        );

        ## no critic (Subroutines::ProhibitCallsToUnexportedSubs)
        $has->(
            $key => (
                is  => 'ro',
                isa => quote_sub(
                    sprintf(
                        q{ GeoIP2::Types::object_isa_type( $_[0], %s ) },
                        B::perlstring($record_class)
                    )
                ),
                init_arg => undef,
                lazy     => 1,
                default  => quote_sub(
                    sprintf(
                        q{ $_[0]->_build_record( %s, %s ) },
                        map { B::perlstring($_) } $key, $raw_attr
                    )
                ),
            ),
        );
        ## use critic
    }
}

sub _all_record_names {
    return qw(
        city
        continent
        country
        location
        maxmind
        postal
        registered_country
        represented_country
        traits
    );
}

around BUILDARGS => sub {
    my $orig = shift;
    my $self = shift;

    my $p = $self->$orig(@_);

    delete $p->{raw};

    # We make a copy to avoid a circular reference
    $p->{raw} = { %{$p} };

    return $p;
};

sub _build_record {
    my $self   = shift;
    my $key    = shift;
    my $method = shift;

    my $raw = $self->$method();

    return $self->_record_class_for_key($key)
        ->new( %{$raw}, locales => $self->locales() );
}

{
    my %key_to_class = (
        maxmind             => 'MaxMind',
        registered_country  => 'Country',
        represented_country => 'RepresentedCountry',
    );

    sub _record_class_for_key {
        my $self = shift;
        my $key  = shift;

        return 'GeoIP2::Record::' . ( $key_to_class{$key} || ucfirst $key );
    }
}

1;