File Coverage

blib/lib/WE/DB/Glossary.pm
Criterion Covered Total %
statement 34 91 37.3
branch 9 44 20.4
condition 0 3 0.0
subroutine 8 15 53.3
pod 6 6 100.0
total 57 159 35.8


line stmt bran cond sub pod time code
1             # -*- perl -*-
2              
3             #
4             # $Id: Glossary.pm,v 1.5 2004/02/23 07:27:49 eserte Exp $
5             # Author: Slaven Rezic
6             #
7             # Copyright (C) 2002 Slaven Rezic. All rights reserved.
8             #
9             # Mail: slaven@rezic.de
10             # WWW: http://rezic.de/eserte
11             #
12              
13             package WE::DB::Glossary;
14              
15 2     2   31989 use base qw(Class::Accessor);
  2         6  
  2         1138  
16             __PACKAGE__->mk_accessors(qw(DB Root));
17              
18 2     2   2374 use strict;
  2         5  
  2         53  
19 2     2   9 use vars qw($VERSION);
  2         11  
  2         137  
20             $VERSION = sprintf("%d.%02d", q$Revision: 1.5 $ =~ /(\d+)\.(\d+)/);
21              
22 2     2   6472 use MLDBM;
  2         4209  
  2         13  
23 2     2   53 use Fcntl;
  2         3  
  2         666  
24              
25             {
26             package WE::GlossaryObj;
27 2     2   11 use base qw(Class::Accessor);
  2         4  
  2         248  
28             __PACKAGE__->mk_accessors
29             (qw(Keyword Description));
30 0     0   0 sub new { bless {}, $_[0] }
31             }
32              
33             {
34             # this will be written to the database and should not be used otherwise
35             package WE::DB::Glossary::DBInfo;
36 2     2   10 use base qw(Class::Accessor);
  2         4  
  2         1925  
37             __PACKAGE__->mk_accessors(qw());
38 0     0   0 sub new { bless {}, $_[0] }
39             }
40              
41             sub new {
42 1     1 1 830 my($class, $root, $file, %args) = @_;
43 1         2 my $self = {};
44              
45 1 50       10 $args{-db} = "DB_File" unless defined $args{-db};
46 1 50       7 $args{-serializer} = "Data::Dumper" unless defined $args{-serializer};
47 1 50       8 $args{-locking} = 0 unless defined $args{-locking};
48 1 50       6 $args{-readonly} = 0 unless defined $args{-readonly};
49 1 50       6 $args{-writeonly} = 0 unless defined $args{-writeonly};
50              
51 1         2 my @tie_args;
52 1 50       21 if ($args{-readonly}) {
    50          
53 0         0 push @tie_args, O_RDONLY;
54             } elsif ($args{-writeonly}) {
55 0         0 push @tie_args, O_RDWR;
56             } else {
57 1         3 push @tie_args, O_RDWR|O_CREAT;
58             }
59              
60 1 50       5 push @tie_args, $args{-db} eq 'Tie::TextDir' ? 0770 : 0660;
61              
62 1 50       5 if ($args{-db} eq 'DB_File') {
63 1         622 require DB_File;
64 0           push @tie_args, $DB_File::DB_BTREE;
65 0 0         if ($args{-locking}) {
66 0           $MLDBM::UseDB = 'DB_File::Lock';
67 0 0         push @tie_args, $args{-readonly} ? "read" : "write";
68             } else {
69 0           $MLDBM::UseDB = 'DB_File';
70             }
71             } else {
72 0           $MLDBM::UseDB = $args{-db};
73             }
74              
75 0           $MLDBM::Serializer = $args{-serializer};
76              
77 0 0         tie %{ $self->{DB} }, 'MLDBM', $file, @tie_args
  0            
78             or require Carp, Carp::confess("Can't tie MLDBM database $file with args <@tie_args>, db <$MLDBM::UseDB> and serializer <$MLDBM::Serializer>: $!");
79              
80 0           bless $self, $class;
81 0           $self->Root($root);
82              
83             # read database information
84 0           my $db_info = $self->DB->{__DBINFO__};
85 0 0         if (!defined $db_info) {
86 0           $db_info = $self->DB->{__DBINFO__} = new WE::DB::Glossary::DBInfo;
87             }
88             # sync members with DBINFO
89 0 0         if ($db_info) {
90             # none yet
91             }
92             # set %args
93             # none yet
94             # write back database information
95 0 0         if (!$args{-readonly}) {
96 0           $self->DB->{__DBINFO__} = $db_info;
97             }
98              
99 0           $self;
100             }
101              
102             sub add_entry {
103 0     0 1   my($self, @args) = @_;
104 0           my %args;
105 0           for(my $i=0; $i<=$#args; $i++) {
106 0 0         if ($args[$i] =~ /^-/) {
107 0           $args{$args[$i]} = $args[$i+1];
108 0           splice @args, $i, 2;
109 0           $i--;
110             }
111             }
112              
113 0           my $obj;
114 0 0         if (UNIVERSAL::isa($args[0], "WE::GlossaryObj")) {
115 0           $obj = $args[0];
116             } else {
117 0           my %obj_args = @args;
118 0           $obj = WE::GlossaryObj->new;
119 0           $obj->$_($obj_args{$_}) for (qw(Keyword Description));
120             }
121              
122 0 0         if (exists $self->{DB}->{$obj->Keyword}) {
123 0 0         if (!$args{-force}) {
124 0           require Carp, Carp::confess("There is already a glossary entry for keyword " . $obj->Keyword);
125             }
126             }
127              
128 0           $self->{DB}->{$obj->Keyword} = $obj;
129 0           $obj;
130             }
131              
132             sub delete_entry {
133 0     0 1   my($self, $keyword) = @_;
134 0           delete $self->{DB}->{$keyword};
135             }
136              
137             sub get_entry {
138 0     0 1   my($self, $keyword) = @_;
139 0           $self->{DB}->{$keyword};
140             }
141              
142             sub get_descr {
143 0     0 1   my($self, $keyword) = @_;
144 0           my $obj = $self->get_entry($keyword);
145 0 0         return undef if !$obj;
146 0           $obj->Description;
147             }
148              
149             sub all_keywords_regex {
150 0     0 1   my($self, $filter) = @_;
151 0           my @keywords;
152 0           while(my($k,$v) = each %{ $self->{DB} }) {
  0            
153 0 0         next if $k =~ /^__/; # skip special keys
154 0 0 0       next if ($filter && !$filter->($k));
155 0           push @keywords, $k;
156             }
157 0           "(" . join("|", map { "\\b" . quotemeta($_) . "\\b" } @keywords) . ")";
  0            
158             }
159              
160             1;
161              
162             __END__