/[pub]/suikawiki/script/lib/SuikaWiki/Name.pm
Suika

Contents of /suikawiki/script/lib/SuikaWiki/Name.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.2 - (hide annotations) (download)
Sat Feb 14 10:59:18 2004 UTC (21 years, 5 months ago) by wakaba
Branch: MAIN
Changes since 1.1: +25 -2 lines
(clone): New

1 wakaba 1.1
2     =head1 NAME
3    
4     SuikaWiki::Name - SuikaWiki WikiName Implementation
5    
6     =head1 DESCRIPTION
7    
8     C<SuikaWiki::Name> implements WikiName related functions used in SuikaWiki.
9     SuikaWiki cope with two styles of WikiName: internal (array reference) form
10     and external (string) form. This module provides "serializer" and "parser"
11     converting to each form, as well as "resolver" getting absolute WikiName
12     from relative WikiName and base WikiName.
13    
14     This module is part of SuikaWiki.
15    
16     =cut
17    
18     package SuikaWiki::Name;
19     use strict;
20 wakaba 1.2 our $VERSION = do{my @r=(q$Revision: 1.1 $=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};
21    
22     use overload
23     'eq' => sub {
24     my ($n1, $n2) = @_;
25     return 0 if not (ref $n1) or not (ref $n2);
26     return 0 if @$n1 != @$n2;
27     for (0..$#$n1) {
28     return 0 if $n1->[$_] ne $n2->[$_];
29     }
30     return 1;
31     },
32     fallback => 1;
33 wakaba 1.1
34     =head1 METHODS
35    
36     =over 4
37    
38     =item $name = SuikaWiki::Name->new ($name, %option)
39    
40     Constructs a new instance of WikiName. C<$name> can be either in external
41     or internal form and either in relative or absolute form.
42     C<wiki> or C<delimiter> option required when C<$name> is in external form.
43    
44     Ensure <$name> is interpretable as an array reference when it is a reference
45     to something.
46    
47     =cut
48    
49     sub new ($;%) {
50     my $class = shift;
51     my ($name, %opt) = @_;
52     my $self;
53     if (ref $name) {
54     $self = bless [@$name], $class;
55     } else {
56     my $delim = $opt{delimiter_reg} || $opt{delimiter}
57     || $opt{wiki}->{config}->{name}->{space}->{separator_reg};
58     $self = bless [split $delim, $name], $class;
59     }
60     $self;
61     }
62    
63     =item $string = $name->stringify (%option)
64    
65     Returns WikiName in external (string) form.
66     C<delimiter> or C<wiki> option is required.
67    
68     =cut
69    
70     sub stringify ($%) {
71     my ($self, %opt) = @_;
72     my $delim = $opt{delimiter}
73     || $opt{wiki}->{config}->{name}->{space}->{separator};
74     join $delim, @$self;
75     }
76    
77     =item $name->append_component ($component)
78    
79     Appends WikiName component. C<$component> is either a reference
80     interpretable as an array reference or a string.
81    
82     =cut
83    
84     sub append_component ($$%) {
85     my ($self, $component) = @_;
86     push @$self, ref $component ? @$component : $component;
87     }
88    
89     =item $name->prepend_component ($component)
90    
91     Prepends WikiName component. C<$component> is either a reference
92     interpretable as an array reference or a string.
93    
94     =cut
95    
96     sub prepend_component ($$%) {
97     my ($self, $component) = @_;
98     unshift @$self, ref $component ? @$component : $component;
99     }
100    
101     =item $abs = $name->absolute (base => $base, %option)
102    
103     Resolves WikiName to absolute form. New instance is constructed.
104     Either C<self> and C<parent> options or C<wiki> option required
105     to specify "self" and "parent" indicator.
106    
107     =cut
108    
109     sub absolute ($%) {
110     my ($self, %opt) = @_;
111     my @abs = @{$opt{base} || []};
112     my @rel = @$self;
113     my $Self = $opt{self} || $opt{wiki}->{config}->{name}->{space}->{self};
114     my $Parent = $opt{parent} || $opt{wiki}->{config}->{name}->{space}->{parent};
115     while ($rel[0] eq $Self or $rel[0] eq $Parent) {
116     if ($rel[0] eq $Parent) {
117     $#abs-- if @abs;
118     }
119     shift @rel;
120     }
121     if (@rel == @$self) {
122     @abs = @rel;
123     } else {
124     push @abs, @rel;
125     }
126     ref ($self)->new (\@abs);
127     }
128    
129 wakaba 1.2 =item $clone = $name->clone
130    
131     Generates a clone of C<$name> object.
132    
133     =cut
134    
135     sub clone ($;%) {
136     my $self = shift;
137     bless [@$self], ref $self;
138     }
139    
140 wakaba 1.1 =head1 TO DO
141    
142     More study needed to enable to include delimiter string in WikiName
143     (string form) as part of data.
144    
145     =head1 LICENCE
146    
147     Copyright 2004 Wakaba <w@suika.fam.cx>. All rights reserved.
148    
149     This program is free software; you can redistribute it and/or
150     modify it under the same terms as Perl itself.
151    
152     =cut
153    
154 wakaba 1.2 1; # $Date: 2004/02/08 08:57:04 $

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24