You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucy.apache.org by ma...@apache.org on 2009/08/13 02:11:35 UTC
svn commit: r803746 - in /lucene/lucy/trunk/boilerplater:
lib/Boilerplater/Parcel.pm lib/Boilerplater/Symbol.pm t/402-parcel.t
Author: marvin
Date: Thu Aug 13 00:11:34 2009
New Revision: 803746
URL: http://svn.apache.org/viewvc?rev=803746&view=rev
Log:
Add Boilerplater::Symbol and Boilerplater::Parcel.
Added:
lucene/lucy/trunk/boilerplater/lib/Boilerplater/Parcel.pm (with props)
lucene/lucy/trunk/boilerplater/lib/Boilerplater/Symbol.pm (with props)
lucene/lucy/trunk/boilerplater/t/402-parcel.t (with props)
Added: lucene/lucy/trunk/boilerplater/lib/Boilerplater/Parcel.pm
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/boilerplater/lib/Boilerplater/Parcel.pm?rev=803746&view=auto
==============================================================================
--- lucene/lucy/trunk/boilerplater/lib/Boilerplater/Parcel.pm (added)
+++ lucene/lucy/trunk/boilerplater/lib/Boilerplater/Parcel.pm Thu Aug 13 00:11:34 2009
@@ -0,0 +1,148 @@
+use strict;
+use warnings;
+
+package Boilerplater::Parcel;
+use base qw( Exporter );
+use Boilerplater::Util qw( verify_args );
+use Carp;
+
+our %parcels;
+
+our %singleton_PARAMS = (
+ name => undef,
+ cnick => undef,
+);
+
+# Create the default parcel.
+our $default_parcel = __PACKAGE__->singleton(
+ name => 'DEFAULT',
+ cnick => '',
+);
+
+sub default_parcel {$default_parcel}
+
+sub singleton {
+ my ( $either, %args ) = @_;
+ verify_args( \%singleton_PARAMS, %args ) or confess $@;
+ my ( $name, $cnick ) = @args{qw( name cnick )};
+
+ # Return the default parcel for either a blank name or an undefined name.
+ return $default_parcel unless $name;
+
+ # Return an existing singleton if the parcel has already been registered.
+ my $existing = $parcels{$name};
+ if ($existing) {
+ if ( $cnick and $cnick ne $existing->{cnick} ) {
+ confess( "cnick '$cnick' for parcel '$name' conflicts with "
+ . "'$existing->{cnick}'" );
+ }
+ return $existing;
+ }
+
+ # Register new parcel. Default cnick to name.
+ my $self = bless { %singleton_PARAMS, %args, }, ref($either) || $either;
+ defined $self->{cnick} or $self->{cnick} = $self->{name};
+ $parcels{$name} = $self;
+
+ # Pre-generate prefixes.
+ $self->{Prefix} = length $self->{cnick} ? "$self->{cnick}_" : "";
+ $self->{prefix} = lc( $self->{Prefix} );
+ $self->{PREFIX} = uc( $self->{Prefix} );
+
+ return $self;
+}
+
+# Accessors.
+sub get_prefix { shift->{prefix} }
+sub get_Prefix { shift->{Prefix} }
+sub get_PREFIX { shift->{PREFIX} }
+
+sub equals {
+ my ( $self, $other ) = @_;
+ return 0 unless $self->{name} eq $other->{name};
+ return 0 unless $self->{cnick} eq $other->{cnick};
+ return 1;
+}
+
+1;
+
+__END__
+
+__POD__
+
+=head1 NAME
+
+Boilerplater::Parcel - Collection of code.
+
+=head1 DESCRIPTION
+
+A Parcel is a cohesive collection of code, which could, in theory, be
+published as as a single entity.
+
+Boilerplater supports two-tier manual namespacing, using a prefix, an optional
+class nickname, and the local symbol:
+
+ prefix_ClassNick_local_symbol
+
+Boilerplater::Parcel supports the first tier, specifying initial prefixes.
+These prefixes come in three capitalization variants: prefix_, Prefix_, and
+PREFIX_.
+
+=head1 CLASS METHODS
+
+=head2 singleton
+
+ Boilerplater::Parcel->singleton(
+ name => 'Crustacean',
+ cnick => 'Crust',
+ );
+
+Add a Parcel singleton to a global registry. May be called multiple times,
+but only with compatible arguments.
+
+=over
+
+=item *
+
+B<name> - The name of the parcel.
+
+=item *
+
+B<cnick> - The C nickname for the parcel, which will be used as a prefix for
+generated global symbols. Must be mixed case and start with a capital letter.
+Defaults to C<name>.
+
+=back
+
+=head2 default_parcel
+
+ $parcel ||= Boilerplater::Parcel->default_parcel;
+
+Return the singleton for default parcel, which has no prefix.
+
+=head1 OBJECT METHODS
+
+=head2 get_prefix get_Prefix get_PREFIX
+
+Return one of the three capitalization variants for the parcel's prefix.
+
+=head1 COPYRIGHT AND LICENSE
+
+ /**
+ * Copyright 2009 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ * implied. See the License for the specific language governing
+ * permissions and limitations under the License.
+ */
+
+=cut
+
Propchange: lucene/lucy/trunk/boilerplater/lib/Boilerplater/Parcel.pm
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: lucene/lucy/trunk/boilerplater/lib/Boilerplater/Parcel.pm
------------------------------------------------------------------------------
svn:executable = *
Added: lucene/lucy/trunk/boilerplater/lib/Boilerplater/Symbol.pm
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/boilerplater/lib/Boilerplater/Symbol.pm?rev=803746&view=auto
==============================================================================
--- lucene/lucy/trunk/boilerplater/lib/Boilerplater/Symbol.pm (added)
+++ lucene/lucy/trunk/boilerplater/lib/Boilerplater/Symbol.pm Thu Aug 13 00:11:34 2009
@@ -0,0 +1,157 @@
+use strict;
+use warnings;
+
+package Boilerplater::Symbol;
+use Boilerplater::Parcel;
+use Scalar::Util qw( blessed );
+use Carp;
+
+my %new_PARAMS = (
+ parcel => undef,
+ exposure => undef,
+);
+
+sub new {
+ my ( $either, %args ) = @_;
+
+ # Acquire a Parcel.
+ my $parcel = $args{parcel};
+ if ( !defined $parcel ) {
+ $parcel = Boilerplater::Parcel->default_parcel;
+ }
+ elsif ( blessed($parcel) ) {
+ confess("Not a Boilerplater::Parcel")
+ unless $parcel->isa('Boilerplater::Parcel');
+ }
+ else {
+ $parcel = Boilerplater::Parcel->singleton( name => $args{parcel} );
+ }
+
+ # Create the object.
+ my $self = bless { %new_PARAMS, %args, parcel => $parcel },
+ ref($either) || $either;
+
+ # Validate the symbol's exposure, then cache accessor values.
+ confess("Invalid value for 'exposure': $self->{exposure}")
+ unless $self->{exposure} =~ /^(?:public|parcel|private|local)$/;
+ $self->{_public} = $self->{exposure} eq 'public' ? 1 : 0;
+ $self->{_private} = $self->{exposure} eq 'private' ? 1 : 0;
+ $self->{_parcel} = $self->{exposure} eq 'parcel' ? 1 : 0;
+ $self->{_local} = $self->{exposure} eq 'local' ? 1 : 0;
+
+ return $self;
+}
+
+sub set_parcel {
+ my ( $self, $parcel ) = @_;
+ if ( blessed($parcel) and $parcel->isa('Boilerplater::Parcel') ) {
+ $self->{parcel} = $parcel;
+ }
+ else {
+ $self->{parcel} = Boilerplater::Parcel->singleton( name => $parcel );
+ }
+}
+sub get_parcel { shift->{parcel} }
+
+sub get_prefix { shift->{parcel}->get_prefix; }
+sub get_Prefix { shift->{parcel}->get_Prefix; }
+sub get_PREFIX { shift->{parcel}->get_PREFIX; }
+
+sub public { shift->{_public} }
+sub private { shift->{_private} }
+sub parcel { shift->{_parcel} }
+sub local { shift->{_local} }
+
+sub equals {
+ my ( $self, $other ) = @_;
+ return 0 unless $self->{parcel}->equals( $other->{parcel} );
+ if ( defined $self->{exposure} ) {
+ return 0 unless defined $other->{exposure};
+ return 0 unless $self->{exposure} eq $other->{exposure};
+ }
+ else {
+ return 0 if defined $other->{exposure};
+ }
+ return 1;
+}
+
+1;
+
+__END__
+
+__POD__
+
+=head1 NAME
+
+Boilerplater::Symbol - Abstract base class for Boilerplater symbols.
+
+=head1 DESCRIPTION
+
+Boilerplater::Symbol serves as an abstract parent class for entities which may
+live in the global namespace, such as classes, functions, methods, and
+variables.
+
+=head1 CONSTRUCTOR
+
+ my $symbol = MySymbol->new(
+ parcel => $parcel, # required
+ exposure => $exposure, # required
+ );
+
+=over
+
+=item
+
+B<parcel> - A Boilerplater::Parcel, or a string that can be used to
+create/retrieve one.
+
+=item
+
+B<exposure> - The scope in which the symbol is exposed. Must be 'public',
+'parcel', 'private', or 'local'.
+
+=back
+
+=head1 OBJECT METHODS
+
+=head2 get_parcel set_parcel
+
+Accessors for C<parcel> member var.
+
+=head2 get_prefix get_Prefix get_PREFIX
+
+Get a string prefix, delegating to C<parcel> member var.
+
+=head2 public parcel private local
+
+ if ( $sym->public ) { do_x() }
+ elsif ( $sym->parcel ) { do_y() }
+
+Indicate whether the symbol matches a given access level.
+
+=head2 equals
+
+ do_stuff() if $sym->equals($other_sym);
+
+Returns true if the symbols are "equal", false otherwise.
+
+=head1 COPYRIGHT AND LICENSE
+
+ /**
+ * Copyright 2009 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ * implied. See the License for the specific language governing
+ * permissions and limitations under the License.
+ */
+
+=cut
+
Propchange: lucene/lucy/trunk/boilerplater/lib/Boilerplater/Symbol.pm
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: lucene/lucy/trunk/boilerplater/lib/Boilerplater/Symbol.pm
------------------------------------------------------------------------------
svn:executable = *
Added: lucene/lucy/trunk/boilerplater/t/402-parcel.t
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/boilerplater/t/402-parcel.t?rev=803746&view=auto
==============================================================================
--- lucene/lucy/trunk/boilerplater/t/402-parcel.t (added)
+++ lucene/lucy/trunk/boilerplater/t/402-parcel.t Thu Aug 13 00:11:34 2009
@@ -0,0 +1,27 @@
+use strict;
+use warnings;
+
+use Test::More tests => 7;
+
+BEGIN { use_ok('Boilerplater::Parcel') }
+
+package BoilingThing;
+use base qw( Boilerplater::Symbol );
+
+sub new { return shift->SUPER::new( exposure => 'parcel', @_ ) }
+
+package main;
+
+# Register singleton.
+Boilerplater::Parcel->singleton( name => 'Boil' );
+
+my $thing = BoilingThing->new;
+is( $thing->get_prefix, '', 'get_prefix with no parcel' );
+is( $thing->get_Prefix, '', 'get_Prefix with no parcel' );
+is( $thing->get_PREFIX, '', 'get_PREFIx with no parcel' );
+
+$thing = BoilingThing->new( parcel => 'Boil' );
+is( $thing->get_prefix, 'boil_', 'get_prefix with parcel' );
+is( $thing->get_Prefix, 'Boil_', 'get_Prefix with parcel' );
+is( $thing->get_PREFIX, 'BOIL_', 'get_PREFIx with parcel' );
+
Propchange: lucene/lucy/trunk/boilerplater/t/402-parcel.t
------------------------------------------------------------------------------
svn:eol-style = native