File Coverage

blib/lib/Dancer/Plugin/DictionaryCheck.pm
Criterion Covered Total %
statement 18 35 51.4
branch 2 12 16.6
condition 2 9 22.2
subroutine 6 8 75.0
pod n/a
total 28 64 43.7


line stmt bran cond sub pod time code
1             package Dancer::Plugin::DictionaryCheck;
2              
3 1     1   47230 use 5.006;
  1         4  
  1         54  
4 1     1   6 use strict;
  1         2  
  1         45  
5 1     1   1354 use Dancer ':syntax';
  1         348274  
  1         6  
6 1     1   1494 use Dancer::Plugin;
  1         1405  
  1         439  
7              
8             =head1 NAME
9              
10             Dancer::Plugin::DictionaryCheck
11              
12             =head1 VERSION
13              
14             Version 0.03
15              
16             =cut
17              
18             our $VERSION = '0.03';
19              
20             =head1 SYNOPSIS
21              
22             Allows a case insensive search of a given string against a given dictionary.
23              
24             use Dancer::Plugin::DictionaryCheck;
25              
26             get '/ok_for_scrabble' => sub {
27             return dictionary_check( params->{word} ) ? "yes" : "no";
28             };
29              
30             get '/change_dictionary' => sub {
31             return dictionary_load( params->{file} ) ? "Loaded" : "Error";
32             };
33              
34             By default makes use /usr/share/dict/words which is present in standard debian
35             images.
36              
37             =cut
38              
39             my %DICT;
40              
41             # Load a default dictionary when Dancer starts.
42             sub INIT {
43 1     1   4 my $default_dict = '/usr/share/dict/words';
44            
45 1 50       5 Dancer::Plugin::DictionaryCheck::_load_dict($default_dict)
46             or warning <
47             The Default dictionary (/usr/share/dict/words) could not be loaded,
48             Please use dictionary_load to specify a dictionary to use before attempting to
49             use dictionary_check.
50             NODEFAULTDICT
51              
52             };
53              
54             =head1 Dancer Keywords
55              
56             =head2 dictionary_load
57              
58             Reloads the stored dictionary with the words from the supplied filename.
59             Returns false if said file doesn't exists and is not a regular file.
60              
61             =cut
62              
63             register 'dictionary_load' => sub {
64              
65             # Dancer 2 keywords receive a reference to the DSL object as a first param,
66             # So if we're running under D2, we need to make sure we don't pass that on
67             # to the route gathering code.
68 0 0 0 0   0 shift if Scalar::Util::blessed($_[0]) && $_[0]->isa('Dancer::Core::DSL');
69 0         0 my $dict_file = shift;
70              
71 0         0 return _load_dict($dict_file);
72             };
73              
74             =head2 dictionary_check
75              
76             Checks the supplied string against the words in the loaded dictionary.
77              
78             Returns 1 if present, 0 if it's not! Simples!!
79              
80             =cut
81              
82             register 'dictionary_check' => sub {
83             # Check we have a dictionary with some content
84 0 0   0   0 if (!%Dancer::Plugin::DictionaryCheck::DICT) {
85 0         0 warning( "Dictionary is empty" );
86 0         0 return 0;
87             }
88 0 0       0 return 0 if (!$_[0]);
89 0         0 return exists $Dancer::Plugin::DictionaryCheck::DICT{ lc $_[0] };
90             };
91              
92              
93             # OK, now we need to register this plugin with Dancer.
94             register_plugin( for_versions => [ qw( 1 2 ) ] );
95              
96              
97             # Loads specified dictionary file into the Plugin memspace.
98              
99             sub _load_dict {
100 1     1   3 my $file = shift;
101              
102 1 50 33     39 if ( !$file || !-e $file || !-f _ ) {
      33        
103 1         7 warning( "Given file does not exist or is not a regular file." );
104 1         17 return 0;
105             }
106              
107 0 0         open my $dict_fh, '<', $file or do {
108 0           warning( "Unable to open dictionary file for reading." );
109 0           return 0;
110             };
111            
112             # Bin the old dictionary
113 0           %Dancer::Plugin::DictionaryCheck::DICT = ();
114              
115             # Load the dictionary and bin the newlines.
116 0           while (<$dict_fh>) {
117 0           chomp;
118 0           $Dancer::Plugin::DictionaryCheck::DICT{ lc $_ } = 1;
119             }
120 0           close $dict_fh;
121              
122 0           return 1;
123             }
124              
125             =head1 AUTHOR
126              
127             Ross Hayes, C<< >>
128              
129             =head1 BUGS
130              
131             Please report any bugs or feature requests to C, or through
132             the web interface at L. I will be notified, and then you'll
133             automatically be notified of progress on your bug as I make changes.
134              
135             =head1 SUPPORT
136              
137             You can find documentation for this module with the perldoc command.
138              
139             perldoc Dancer::Plugin::DictionaryCheck
140              
141              
142             You can also look for information at:
143              
144             =over 4
145              
146             =item * RT: CPAN's request tracker (report bugs here)
147              
148             L
149              
150             =item * AnnoCPAN: Annotated CPAN documentation
151              
152             L
153              
154             =item * CPAN Ratings
155              
156             L
157              
158             =item * Search CPAN
159              
160             L
161              
162             =back
163              
164             =head1 LICENSE AND COPYRIGHT
165              
166             Copyright 2012 Ross Hayes.
167              
168             This program is free software; you can redistribute it and/or modify it
169             under the terms of the the Artistic License (2.0). You may obtain a
170             copy of the full license at:
171              
172             L
173              
174             Any use, modification, and distribution of the Standard or Modified
175             Versions is governed by this Artistic License. By using, modifying or
176             distributing the Package, you accept this license. Do not use, modify,
177             or distribute the Package, if you do not accept this license.
178              
179             If your Modified Version has been derived from a Modified Version made
180             by someone other than you, you are nevertheless required to ensure that
181             your Modified Version complies with the requirements of this license.
182              
183             This license does not grant you the right to use any trademark, service
184             mark, tradename, or logo of the Copyright Holder.
185              
186             This license includes the non-exclusive, worldwide, free-of-charge
187             patent license to make, have made, use, offer to sell, sell, import and
188             otherwise transfer the Package with respect to any patent claims
189             licensable by the Copyright Holder that are necessarily infringed by the
190             Package. If you institute patent litigation (including a cross-claim or
191             counterclaim) against any party alleging that the Package constitutes
192             direct or contributory patent infringement, then this Artistic License
193             to you shall terminate on the date that such litigation is filed.
194              
195             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
196             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
197             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
198             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
199             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
200             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
201             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
202             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
203              
204              
205             =cut
206              
207             1; # End of Dancer::Plugin::DictionaryCheck