| 1 | package UR::Value::Text; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | require UR; | ||
| 7 | our $VERSION = "0.46"; # UR $VERSION; | ||
| 8 | |||
| 9 | UR::Object::Type->define( | ||
| 10 | class_name => 'UR::Value::Text', | ||
| 11 | is => ['UR::Value'], | ||
| 12 | ); | ||
| 13 | |||
| 14 | use overload ( | ||
| 15 | '.' => \&concat, | ||
| 16 | '""' => \&stringify, | ||
| 17 | fallback => 1, | ||
| 18 | ); | ||
| 19 | |||
| 20 | sub swap { | ||
| 21 | my ($a, $b) = @_; | ||
| 22 | return ($b, $a); | ||
| 23 | } | ||
| 24 | |||
| 25 | sub concat { | ||
| 26 | my ($self, $other, $swap) = @_; | ||
| 27 | my $class = ref $self; | ||
| 28 | $self = $self->id; | ||
| 29 | ($self, $other) = swap($self, $other) if $swap; | ||
| 30 | return $class->get($self . $other); | ||
| 31 | } | ||
| 32 | |||
| 33 | sub stringify { | ||
| 34 | my $self = shift; | ||
| 35 | return $self->id; | ||
| 36 | } | ||
| 37 | |||
| 38 | sub capitalize { | ||
| 39 | my $self = shift; | ||
| 40 | my $seps = join('', ' ', @_); # allow other separators | ||
| 41 | my $regexp = qr/[$seps]+/; | ||
| 42 | my $capitalized_string = join(' ', map { ucfirst } split($regexp, $self->id)); | ||
| 43 | return $self->class->get($capitalized_string); | ||
| 44 | } | ||
| 45 | |||
| 46 | sub to_camel { | ||
| 47 | my $self = shift; | ||
| 48 | my $seps = join('', ( @_ ? @_ : ( ' ', '_' ))); | ||
| 49 | my $regexp = qr/[$seps]+/; | ||
| 50 | my $camel_case = join('', map { ucfirst } split($regexp, $self->id)); | ||
| 51 | return $self->class->get($camel_case); | ||
| 52 | } | ||
| 53 | |||
| 54 | sub to_lemac { # camel backwards = undo camel case. This was nutters idea. Ignore 'git blame' | ||
| 55 | my $self = shift; | ||
| 56 | # Split on the first capital or the start of a number | ||
| 57 | my @words = split( /(?=(?<![A-Z])[A-Z])|(?=(?<!\d)\d)/, $self->id); | ||
| 58 | # Default join is a space | ||
| 59 | my $join = ( defined $_[0] ) ? $_[0] : ' '; | ||
| 60 | return $self->class->get( join($join, map { lc } @words) ); | ||
| 61 | } | ||
| 62 | |||
| 63 | sub to_hash { | ||
| 64 | my ($self, $split) = @_; # split splits to value of a key into many values | ||
| 65 | |||
| 66 | my $text = $self->id; | ||
| 67 | if ( $text !~ m#^-# ) { | ||
| 68 | $self->warning_message('Can not convert text object with id "' . $self->id . '" to hash. Text must start with a dash (-)'); | ||
| 69 | return; | ||
| 70 | } | ||
| 71 | |||
| 72 | my %hash; | ||
| 73 | my @values = split(/\s?(\-{1,2}\D[\w\d\-]*)\s?/, $text); | ||
| 74 | shift @values; | ||
| 75 | for ( my $i = 0; $i < @values; $i += 2 ) { | ||
| 76 | my $key = $values[$i]; | ||
| 77 | $key =~ s/^\-{1,2}//; | ||
| 78 | if ( $key eq '' ) { | ||
| 79 | $self->warning_message("Can not convert text ($text) to hash. Found empty dash (-)."); | ||
| 80 | return; | ||
| 81 | } | ||
| 82 | my $value = $values[$i + 1]; | ||
| 83 | if ( defined $value ){ | ||
| 84 | $value =~ s/\s*$//; | ||
| 85 | } | ||
| 86 | else { | ||
| 87 | $value = ''; | ||
| 88 | } | ||
| 89 | # FIXME What if the key exists? | ||
| 90 | if ( defined $split ) { | ||
| 91 | $hash{$key} = [ split($split, $value) ]; | ||
| 92 | } | ||
| 93 | else { | ||
| 94 | $hash{$key} = $value; | ||
| 95 | } | ||
| 96 | } | ||
| 97 | |||
| 98 | #print Data::Dumper::Dumper(\@values, \%hash); | ||
| 99 | return UR::Value::HASH->get(\%hash); | ||
| 100 | } | ||
| 101 | |||
| 102 | 1; | ||
| 103 |