File Coverage

blib/lib/Data/Phrasebook/Loader/Ini.pm
Criterion Covered Total %
statement 45 45 100.0
branch 14 16 87.5
condition 3 3 100.0
subroutine 9 9 100.0
pod 4 4 100.0
total 75 77 97.4


line stmt bran cond sub pod time code
1             package Data::Phrasebook::Loader::Ini;
2 6     6   126350 use strict;
  6         18  
  6         253  
3 6     6   35 use warnings FATAL => 'all';
  6         10  
  6         275  
4 6     6   32 use Carp qw( croak );
  6         22  
  6         552  
5 6     6   32 use base qw( Data::Phrasebook::Loader::Base Data::Phrasebook::Debug );
  6         11  
  6         6513  
6 6     6   22608 use Config::IniFiles;
  6         279257  
  6         3595  
7              
8             our $VERSION = '0.14';
9              
10             =head1 NAME
11              
12             Data::Phrasebook::Loader::Ini - Absract your phrases with ini files.
13              
14             =head1 SYNOPSIS
15              
16             use Data::Phrasebook;
17              
18             my $q = Data::Phrasebook->new(
19             class => 'Fnerk',
20             loader => 'Ini',
21             file => 'phrases.ini',
22             );
23              
24             # simple keyword to phrase mapping
25             my $phrase = $q->fetch($keyword);
26              
27             # keyword to phrase mapping with parameters
28             $q->delimiters( qr{ \[% \s* (\w+) \s* %\] }x );
29             my $phrase = $q->fetch($keyword,{this => 'that'});
30              
31             =head1 ABSTRACT
32              
33             This module provides a loader class for phrasebook implementations using INI
34             files.
35              
36             =head1 DESCRIPTION
37              
38             This module provides a base class for phrasebook implementations.
39              
40             Phrases can be contained within one or more dictionaries, with each phrase
41             accessible via a unique key. Phrases may contain placeholders, please see
42             L for an explanation of how to use these. Groups of phrases
43             are kept in a dictionary. In this implementation a dictionary is considered to
44             be equivilent to a section in an ini file.
45              
46             An example ini file:
47              
48             [BASE]
49             foo=\
50             Welcome to :my world. \
51             It is a nice :place.
52              
53             Within the phrase text placeholders can be used, which are then replaced with
54             the appropriate values once the get() method is called. The default style of
55             placeholders can be altered using the delimiters() method.
56              
57             =head1 INHERITANCE
58              
59             L inherits from the base class
60             L.
61             See that module for other available methods and documentation.
62              
63             =head1 METHODS
64              
65             =head2 load
66              
67             Given a C, load it. C must contain a INI style layout.
68              
69             $loader->load( $file, $dict );
70              
71             This method is used internally by L's
72             C method, to initialise the data store.
73              
74             It must take a C (be it a scalar, or something more complex)
75             and return a handle.
76              
77             =cut
78              
79             sub load
80             {
81 7     7 1 10516 my ($class, $file, $dict) = @_;
82 7 100       241 croak "No file given as argument!" unless defined $file;
83 6 100       298 croak "Cannot read configuration file [$file]\n"
84             unless(-r $file);
85              
86 5         50 my $cfg = Config::IniFiles->new(
87             -file => $file,
88             -allowcontinue => 1, # allows continuation lines
89             );
90 5 50       6887 croak "Cannot access configuration file [$file]".
91             " - [@Config::IniFiles::errors]\n" unless($cfg);
92 5         20 $class->{cfg} = $cfg;
93              
94             # what sections are we using?
95 5         40 ($class->{default}) = $cfg->Sections;
96 5         62 $class->{dict} = $class->{default};
97 5 100 100     29 $class->{dict} = $dict
98             if($dict && $class->{cfg}->SectionExists( $dict ));
99             };
100              
101             =head2 get
102              
103             Returns the phrase stored in the phrasebook, for a given keyword.
104              
105             my $value = $loader->get( $key );
106              
107             =cut
108              
109             sub get {
110 5     5 1 1477 my ($class, $key) = @_;
111              
112 5         21 my $data = $class->{cfg}->val( $class->{dict}, $key );
113 5 100       271 $data = $class->{cfg}->val( $class->{default}, $key ) unless($data);
114 5 100       39 return unless($data);
115              
116 3         10 $data =~ s!^\s+!!s;
117 3         14 $data =~ s!\s+$!!s;
118 3         20 $data =~ s!\s+! !sg;
119              
120 3         14 return $data;
121             }
122              
123             =head2 dicts
124              
125             Returns the list of dictionaries available.
126              
127             my @dicts = $loader->dicts();
128              
129             =cut
130              
131             sub dicts {
132 1     1 1 510 my $class = shift;
133 1         5 $class->{cfg}->Sections
134             }
135              
136             =head2 keywords
137              
138             Returns the list of keywords available. List is lexically sorted.
139              
140             my @dicts = $loader->keywords();
141              
142             =cut
143              
144             sub keywords {
145 2     2 1 1027 my $class = shift;
146 2         3 my $dict = shift;
147              
148 2 100       6 if($dict) {
149 1         6 my @dicts = sort $class->{cfg}->Parameters($dict);
150 1         23 return @dicts;
151             }
152              
153 1         6 my @keywords = $class->{cfg}->Parameters($class->{dict});
154 1 50       27 push @keywords, $class->{cfg}->Parameters($class->{default})
155             unless($class->{dict} eq $class->{default});
156              
157 1         18 my %keywords = map {$_=>1} @keywords;
  4         13  
158 1         8 @keywords = sort keys %keywords;
159 1         6 return @keywords;
160             }
161              
162             1;
163              
164             __END__