| 1 | package File::Assets::Kind; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use Object::Tiny qw/kind type head tail/; | ||
| 7 | use Carp; | ||
| 8 | |||
| 9 | sub new { | ||
| 10 | my $self = bless {}, shift; | ||
| 11 | confess "Uhh, whut?" unless $self->{kind} = my $kind = shift; | ||
| 12 | my @kind = split m/-/, $kind, 2; | ||
| 13 | my $type = shift; | ||
| 14 | unless ($type) { | ||
| 15 | $type = $kind[0]; | ||
| 16 | $type = File::Assets::Util->parse_type($type); | ||
| 17 | } | ||
| 18 | $self->{type} = $type; | ||
| 19 | $kind[1] = "" unless defined $kind[1]; | ||
| 20 | $self->{tail} = my $tail = $kind[1]; | ||
| 21 | $self->{head} = ($type->extensions)[0]; | ||
| 22 | |||
| 23 | return $self; | ||
| 24 | } | ||
| 25 | |||
| 26 | sub extension { | ||
| 27 | my $self = shift; | ||
| 28 | return ($self->type->extensions)[0]; | ||
| 29 | } | ||
| 30 | |||
| 31 | sub is_better_than_or_equal { | ||
| 32 | my $self = shift; | ||
| 33 | my $other = shift; | ||
| 34 | |||
| 35 | return 1 if $self->kind eq $other->kind; | ||
| 36 | |||
| 37 | return $self->is_better_than($other); | ||
| 38 | } | ||
| 39 | |||
| 40 | sub is_better_than { | ||
| 41 | my $self = shift; | ||
| 42 | my $other = shift; | ||
| 43 | |||
| 44 | return 0 unless File::Assets::Util->same_type($self->type, $other->type); | ||
| 45 | my $self_tail = $self->tail; | ||
| 46 | my $other_tail = $other->tail; | ||
| 47 | if (length $self_tail && length $other_tail) { | ||
| 48 | return 0 unless 0 == index($self->tail, $other->tail) || 0 == index($other->tail, $self->tail); | ||
| 49 | } | ||
| 50 | return length $self->tail > length $other->tail; | ||
| 51 | } | ||
| 52 | |||
| 53 | 1; |