File Coverage

lib/UR/Value/Text.pm
Criterion Covered Total %
statement 54 57 94.7
branch 10 14 71.4
condition n/a
subroutine 10 10 100.0
pod 0 7 0.0
total 74 88 84.0


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