summaryrefslogtreecommitdiffabout
path: root/lib/Config/HAProxy/Node.pm
blob: be095381e729607372aa22e3ce5465d379955700 (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
93
94
95
96
97
98
99
package Config::HAProxy::Node;
use strict;
use warnings;
use Carp;
use Config::HAProxy::Iterator;

sub new {
    my $class = shift;
    local %_ = @_;
    bless {
	_kw => $_{kw},
	_argv => $_{argv} // [],
	_orig => $_{orig},
	_locus => $_{locus},
	_parent => $_{parent},
	_index => -1
    }, $class;
}

my @ATTRIBUTES = qw(kw orig locus parent index);

{
    no strict 'refs';
    foreach my $attribute (@ATTRIBUTES) {
	*{ __PACKAGE__ . '::' . $attribute } = sub {
	    my $self = shift;
	    if (defined(my $val = shift)) {
		croak "too many arguments" if @_;
		$self->{'_'.$attribute} = $val;
	    }
	    return $self->{'_'.$attribute};
	}
    }
}

sub argv {
    my $self = shift;
    if (my $val = shift) {
	croak "too many arguments" if @_;
	$self->{_argv} = $val;
    }
    return @{$self->{_argv}};
}

sub arg {
    my $self = shift;
    my $n = shift;
    if (my $val = shift) {
	croak "too many arguments" if @_;
	$self->{_argv}[$n] = $val;
    }
    return $self->{_argv}[$n];
}

sub drop {
    my $self = shift;
    $self->parent->delete_node($self->index);
}

sub iterator {
    return new Config::HAProxy::Iterator(@_);
}

sub depth {
    my $self = shift;
    my $n = 0;
    while ($self = $self->parent) {
	$n++;
    }
    return $n - 1;
}

sub root {
    my $self = shift;
    while ($self->parent()) {
	$self = $self->parent();
    }
    return $self;
}

sub as_string {
    my $self = shift;
    if (defined(my $v = $self->orig)) {
	return $v;
    }
    return '' unless $self->kw;
    return $self->orig(join(' ', ($self->kw, $self->argv())));
}

# use overload
#     '""' => sub { shift->as_string };

sub is_root { 0 }
sub is_section { 0 }
sub is_statement { 0 }
sub is_empty { 0 }
sub is_comment { 0 }

1;

Return to:

Send suggestions and report system problems to the System administrator.