Commits

Shlomi Fish committed dd72f21

Partially implemented ->from_guess.

Comments (0)

Files changed (2)

Module-Format/Module-Format/lib/Module/Format/Module.pm

 
 =cut
 
+my $dash_re = qr{(?:\w+-)*\w+};
+my $colon_re = qr{(?:\w+::)*\w+};
+
 my @formats_by_priority =
 (
     {
         name => 'rpm_dash',
-        input => 
+        regex => qr{\Aperl-$dash_re\z},
+        input =>
         sub {
             my ($class, $value) = @_;
 
     },
     {
         name => 'rpm_colon',
+        regex => qr{\Aperl\($colon_re\)\z},
         input => sub {
             my ($class, $value) = @_;
 
     },
     {
         name => 'colon',
+        regex => qr{\A$colon_re\z},
         input => sub { 
             my ($class, $value) = @_;
             return [split(/::/, $value, -1)]; 
     },
     {
         name => 'dash',
+        regex => qr{\A$dash_re\z},
         input => sub { 
             my ($class, $value) = @_;
             return [split(/-/, $value, -1)]; 
     },
     {
         name => 'unix',
+        regex => qr{\A(?:\w+/)*\.pm\z},
         input => sub {
             my ($class, $value) = @_;
 
 
 =cut
 
+sub from_guess
+{
+    my ($class, $args) = @_;
+
+    my $dummy_format_string;
+
+    my $string = $args->{value};
+    my $out_format_ref = ($args->{format_ref} || (\$dummy_format_string));
+
+    # TODO : After the previous line the indentation in vim is reset to the
+    # first column.
+    
+    foreach my $format_record (@formats_by_priority)
+    {
+        my $regex = $format_record->{regex};
+
+        if ($string =~ $regex)
+        {
+            my $format_id = $format_record->{name};
+            # $out_format_ref = ...
+            return $class->from({value => $string, format => $format_id,});
+        }
+    }
+
+    die "Could not guess the format of the value '$string'!";
+}
+
 =head1 AUTHOR
 
 Shlomi Fish, L<http://www.shlomifish.org/> .

Module-Format/Module-Format/t/module.t

 use strict;
 use warnings;
 
-use Test::More tests => 41;
+use Test::More tests => 43;
 
 use Module::Format::Module;
 
     ok (!scalar($module->is_sane()), "Module is not sane.");
 }
 
+{
+    my $module = Module::Format::Module->from_guess({value => "XML::RSS"});
+    
+    # TEST
+    ok ($module, "from_guess initialises a module.");
 
+    # TEST
+    is_deeply(
+        $module->get_components_list(),
+        [qw(XML RSS)],
+        "from_guess got good components.",
+    );
+}
+