summaryrefslogtreecommitdiff
path: root/lib/Enigmatic/Rotor.pm
blob: 9118beed07ea23f944ef23b16e956eea7cd4996a (plain)
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
package Enigmatic::Rotor; 
use DAKKAR::p 'class';
use Enigmatic::Types qw(WiringMap Letter RotorPos);
use MooseX::Types::Set::Object;
 
with 'Enigmatic::Role::Rotate';
 
has wiring => (
    is => 'ro',
    isa => WiringMap,
    coerce => 1,
);
 
has inverse_wiring => (
    is => 'ro',
    isa => WiringMap,
    init_arg => undef,
    lazy_build => 1,
);
 
sub _build_inverse_wiring {
    my ($self) = @_;
    my $w = $self->wiring;
    my $ret = { reverse %$w };
    return $ret;
}
 
has ring_setting => (
    is => 'rw',
    isa => RotorPos,
    default => 0,
    coerce => 1,
);
 
has notches => (
    isa => 'Set::Object',
    coerce => 1,
    handles => {
        has_notch_at => 'member',
    },
    default => sub { [] },
);
 
with 'Enigmatic::Role::WithWiring';
 
sub map {
    my $self = shift;
    my ($letter) = pos_validated_list(
        \@_,
        isa => Letter },
    );
 
    return $self->_real_map($letter,'wiring');
}
 
sub inverse_map {
    my $self = shift;
    my ($letter) = pos_validated_list(
        \@_,
        isa => Letter },
    );
 
    return $self->_real_map($letter,'inverse_wiring');
}
 
sub _real_map {
    my ($self,$letter,$wiring) = @_;
 
    my $setting = $self->ring_setting;
 
    $letter = _rotate_by($letter,-$setting);
    $letter = $self->$wiring->at($letter);
    $letter = _rotate_by($letter,$setting);
 
    return $letter;
}
 
__END__
 
=head1 AUTHOR
 
Gianni Ceccarelli <dakkar@thenautilus.net>
 
=head1 COPYRIGHT AND LICENSE
 
This software is copyright (c) 2011 by Gianni Ceccarelli.
 
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, version 3.
 
=cut