| 1 | package UR::Value::HASH; | ||
| 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::HASH', | ||
| 11 | is => ['UR::Value::PerlReference'], | ||
| 12 | ); | ||
| 13 | |||
| 14 | sub __display_name__ { | ||
| 15 | my $self = shift; | ||
| 16 | my $hash = $self->id; | ||
| 17 | my @values; | ||
| 18 | for my $key (sort keys %$hash) { | ||
| 19 | next unless defined $hash->{$key}; | ||
| 20 | push @values, "$key => '".( defined $hash->{$key} ? $hash->{$key} : '' ). "'"; | ||
| 21 | } | ||
| 22 | my $join = ( defined $_[0] ) ? $_[0] : ','; # Default join is a comma | ||
| 23 | return join($join, @values); | ||
| 24 | } | ||
| 25 | |||
| 26 | sub to_text { | ||
| 27 | my $self = shift; | ||
| 28 | my $hash = $self->id; | ||
| 29 | my @tokens; | ||
| 30 | for my $key (sort keys %$hash) { | ||
| 31 | push @tokens, '-'.$key; | ||
| 32 | next if not defined $hash->{$key} or $hash->{$key} eq ''; | ||
| 33 | if ( my $ref = ref $hash->{$key} ) { | ||
| 34 | if ( $ref ne 'ARRAY' ) { | ||
| 35 | $self->warning_message("Can not convert hash to text. Cannot handle $ref for $key"); | ||
| 36 | return; | ||
| 37 | } | ||
| 38 | push @tokens, @{$hash->{$key}}; | ||
| 39 | } | ||
| 40 | else { | ||
| 41 | push @tokens, $hash->{$key}; | ||
| 42 | } | ||
| 43 | } | ||
| 44 | my $join = ( defined $_[0] ) ? $_[0] : ' '; # Default join is a space | ||
| 45 | return UR::Value::Text->get( join($join, @tokens)); | ||
| 46 | } | ||
| 47 | |||
| 48 | 1; | ||
| 49 |