1package Config::MVP::Reader::INI;
2# ABSTRACT: an MVP config reader for .ini files
3$Config::MVP::Reader::INI::VERSION = '2.101463';
4use Moose;
5extends 'Config::MVP::Reader';
6with 'Config::MVP::Reader::Findable::ByExtension';
7
8use Config::MVP 2; # Reader is a base class
9
10#pod =head1 DESCRIPTION
11#pod
12#pod Config::MVP::Reader::INI reads F<.ini> files containing MVP-style
13#pod configuration.
14#pod
15#pod =cut
16
17# Clearly this should be an attribute with a builder blah blah blah. -- rjbs,
18# 2009-07-25
19sub default_extension { 'ini' }
20
21sub read_into_assembler {
22  my ($self, $location, $assembler) = @_;
23
24  my $reader = Config::MVP::Reader::INI::INIReader->new($assembler);
25  $reader->read_file($location);
26
27  return $assembler->sequence;
28}
29
30{
31  package
32   Config::MVP::Reader::INI::INIReader;
33  use parent 'Config::INI::Reader';
34
35  sub new {
36    my ($class, $assembler) = @_;
37    my $self = $class->SUPER::new;
38    $self->{assembler} = $assembler;
39    return $self;
40  }
41
42  sub assembler { $_[0]{assembler} }
43
44  sub change_section {
45    my ($self, $section) = @_;
46
47    my ($package, $name) = $section =~ m{\A\s*(?:([^/\s]+)\s*/\s*)?(.+)\z};
48    $package = $name unless defined $package and length $package;
49
50    Carp::croak qq{couldn't understand section header: "$_[1]"}
51      unless $package;
52
53    $self->assembler->change_section($package, $name);
54  }
55
56  sub finalize {
57    my ($self) = @_;
58    $self->assembler->finalize;
59  }
60
61  sub set_value {
62    my ($self, $name, $value) = @_;
63    unless ($self->assembler->current_section) {
64      my $starting_name = $self->starting_section;
65
66      if ($self->assembler->sequence->section_named( $starting_name )) {
67        Carp::croak q{can't set value outside of section once starting }
68                  . q{section exists};
69      }
70
71      $self->assembler->change_section(\undef, $starting_name);
72    }
73
74    $self->assembler->add_value($name, $value);
75  }
76}
77
78no Moose;
79__PACKAGE__->meta->make_immutable;
801;
81
82__END__
83
84=pod
85
86=encoding UTF-8
87
88=head1 NAME
89
90Config::MVP::Reader::INI - an MVP config reader for .ini files
91
92=head1 VERSION
93
94version 2.101463
95
96=head1 DESCRIPTION
97
98Config::MVP::Reader::INI reads F<.ini> files containing MVP-style
99configuration.
100
101=head1 AUTHOR
102
103Ricardo Signes <rjbs@cpan.org>
104
105=head1 COPYRIGHT AND LICENSE
106
107This software is copyright (c) 2008 by Ricardo Signes.
108
109This is free software; you can redistribute it and/or modify it under
110the same terms as the Perl 5 programming language system itself.
111
112=cut
113