Path::is and Path::at
authorIan Beckwith <ianb@erislabs.net>
Mon, 18 Oct 2010 06:02:02 +0000 (07:02 +0100)
committerIan Beckwith <ianb@erislabs.net>
Mon, 18 Oct 2010 06:02:02 +0000 (07:02 +0100)
lib/ID3FS/Path.pm

index 07a9b0b..96fd323 100644 (file)
@@ -87,11 +87,8 @@ sub dirents
     }
     elsif($state==$STATE_TAG || $state==$STATE_TAGVAL)
     {
-       my $tag=$self->tail();
-       if($state==$STATE_TAG &&
-          defined($tag) &&
-          ref($tag) eq "ID3FS::PathElement::Tag" &&
-          $self->{db}->tag_has_values($tag->{id}))
+       if($state==$STATE_TAG && $self->at("tag") &&
+          $self->{db}->tag_has_values($self->tail()->{id}))
        {
            @dents=$self->tags();
        }
@@ -108,8 +105,7 @@ sub dirents
     elsif($state==$STATE_BOOLEAN)
     {
        my $parent=$self->tail();
-       unless(defined($parent) &&
-              ref($parent) eq "ID3FS::PathElement::Boolean" &&
+       unless($self->is("boolean", $parent) &&
               $parent->{name} eq "NOT")
        {
            @dents=("NOT");
@@ -190,11 +186,9 @@ sub parse
        }
        elsif($state==$STATE_TAG || $state==$STATE_TAGVAL)
        {
-#          print "SM: TAG/TAGVAL($state): $name\n";
            my $tag=$self->tail();
-           if($state==$STATE_TAG &&
-              defined($tag) &&
-              ref($tag) eq "ID3FS::PathElement::Tag" &&
+#          print "SM: TAG/TAGVAL($state): $name\n";
+           if($state==$STATE_TAG && $self->is("tag", $tag) &&
               $self->{db}->tag_has_values($tag->{id}))
            {
 #              print "Parsing: parent: $tag->{id}\n";
@@ -247,8 +241,7 @@ sub parse
 #          print "SM: BOOLEAN: $name\n";
            my $parent=$self->tail();
            my $allownot=1;
-           if(defined($parent) &&
-              ref($parent) eq "ID3FS::PathElement::Boolean" &&
+           if($self->is("boolean", $parent) &&
               $parent->{name} eq "NOT")
            {
                $allownot=0;
@@ -356,7 +349,7 @@ sub parse
 
     # remove trailing boolean
     my @elements=@{$self->{elements}};
-    while(@elements && ref($elements[$#elements]) eq "ID3FS::PathElement::Boolean")
+    while(@elements && $self->is("boolean", $elements[$#elements]))
     {
        pop @elements;
     }
@@ -392,7 +385,7 @@ sub elements_to_tree
     return undef unless(@$elements);
     my ($left, $right, $op)=(undef, undef, undef);
     my $thing=pop @$elements;
-    if(ref($thing) eq "ID3FS::PathElement::Boolean")
+    if($self->is("boolean", $thing))
     {
        my $op=$thing;
        $right=$self->elements_to_tree($elements);
@@ -417,16 +410,16 @@ sub sort_elements
 #    print "INPUT: ", join(', ', map { $_->{name}; } @input), "\n";
     while(my $thing = shift @input)
     {
-       if(ref($thing) eq "ID3FS::PathElement::Tag")
+       if($self->is("tag", $thing))
        {
            # Handle tag values by dropping parent
-           if(@input && ref($input[0]) eq "ID3FS::PathElement::Tag")
+           if(@input && $self->is("tag", $input[0])
            {
                $thing=shift @input;
            }
            push(@output, $thing);
        }
-       elsif(ref($thing) eq "ID3FS::PathElement::Boolean")
+       elsif($self->is("boolean", $thing))
        {
            # bool
            while(@opstack &&
@@ -456,7 +449,7 @@ sub expecting_values
 {
     my($self)=@_;
     my $tail=$self->tail();
-    if($tail && ref($tail) eq "ID3FS::PathElement::Tag")
+    if($self->is("tag", $tail))
     {
        return($self->{db}->tag_has_values($tail->{id}));
     }
@@ -466,7 +459,7 @@ sub trailing_tag_id
 {
     my($self)=@_;
     my $tail=$self->tail();
-    if($tail && ref($tail) eq "ID3FS::PathElement::Tag")
+    if($self->is("tag", $tail))
     {
        return($tail->{id});
     }
@@ -477,7 +470,7 @@ sub trailing_tag_parent
 {
     my($self)=@_;
     my $tail=$self->tail();
-    if($tail && ref($tail) eq "ID3FS::PathElement::Tag")
+    if($self->is("tag", $tail))
     {
        return($tail->{parents_id});
     }
@@ -490,6 +483,22 @@ sub tail
     return($self->{elements}->[$#{$self->{elements}}]);
 }
 
+sub at
+{
+    my($self, $type)=@_;
+    return($self->is($type, $self->tail()));
+}
+
+sub is
+{
+    my($self, $type, $thing)=@_;
+    return 0 unless($thing);
+    my $ref=ref($thing);
+    my $typestr="ID3FS::PathElement::" . ucfirst($type);
+    return 1 if($ref eq $typestr);
+    return 0;
+}
+
 # the one before last
 sub tail_parent
 {
@@ -602,7 +611,7 @@ sub albums
     my @ids=();
     my $tail=$self->tail();
     # FIXME: rework PathElements
-    if(ref($tail) eq "ID3FS::PathElement::Artist")
+    if($self->is("artist", $tail))
     {
        return $self->artist_albums($tail->{id});
     }
@@ -663,15 +672,15 @@ sub tracks
     my($self)=@_;
     # FIXME: rework PathElements
     my $tail=$self->tail();
-    if(ref($tail) eq "ID3FS::PathElement::Artist")
+    if($self->is("artist", $tail))
     {
        return $self->artist_tracks($tail->{id});
     }
-    elsif(ref($tail) eq "ID3FS::PathElement::Album")
+    elsif($self->is("album", $tail))
     {
        my $artist_id=0;
        my $artist=$self->tail_parent();
-       if(defined($artist) && (ref($artist) eq "ID3FS::PathElement::Artist"))
+       if($self->is("artist", $artist))
        {
            # should always happen
            $artist_id=$artist->{id};
@@ -695,7 +704,7 @@ sub filename
 {
     my($self, $mountpoint)=@_;
     my $tail=$self->tail();
-    if(ref($tail) eq "ID3FS::PathElement::File")
+    if($self->is("file", $tail))
     {
        my $id=$tail->{id};
        my $sql=("SELECT paths.name, files.name FROM files\n" .
@@ -770,7 +779,7 @@ sub tags_subselect
 sub bare_not_subselect
 {
     my($self)=@_;
-    my @tags=grep { ref($_) eq "ID3FS::PathElement::Tag"; } @{$self->{elements}};
+    my @tags=grep { $self->is("tag", $_); } @{$self->{elements}};
     my $sql=("\tSELECT f1.id AS files_id FROM files f1 WHERE f1.id NOT IN (\n" .
             "\t\tSELECT fxt1.files_id FROM tags t1\n" .
             "\t\tINNER JOIN files_x_tags fxt1 ON t1.id=fxt1.tags_id\n" .
@@ -816,7 +825,7 @@ sub constraints_tag_list
     for my $constraint (@constraints)
     {
 #      print ref($constraint), ": ", $constraint->{name}, "\n";
-       if(ref($constraint) eq "ID3FS::PathElement::Tag")
+       if($self->is("tag", $constraint))
        {
            if(defined($lasttag))
            {