| 1 | package UR::Object::View::Default::Text; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | require UR; | ||
| 6 | our $VERSION = "0.46"; # UR $VERSION; | ||
| 7 | |||
| 8 | class UR::Object::View::Default::Text { | ||
| 9 | is => 'UR::Object::View', | ||
| 10 | has_constant => [ | ||
| 11 | perspective => { value => 'default' }, | ||
| 12 | toolkit => { value => 'text' }, | ||
| 13 | ], | ||
| 14 | has => [ | ||
| 15 | indent_text => { is => 'Text', default_value => ' ', doc => 'indent child views with this text' }, | ||
| 16 | ], | ||
| 17 | }; | ||
| 18 | |||
| 19 | # general view API | ||
| 20 | |||
| 21 | sub _create_widget { | ||
| 22 | # The "widget" for a text view is a pair of items: | ||
| 23 | # - a scalar reference to hold the content | ||
| 24 | # - an I/O handle to which it will display (the "window" it lives in) | ||
| 25 | |||
| 26 | # Note that the former could be something tied to an object, | ||
| 27 | # a file, or other external storage, though it is | ||
| 28 | # simple by default. The later might also be tied. | ||
| 29 | |||
| 30 | # The later is STDOUT unless overridden/changed. | ||
| 31 | my $self = shift; | ||
| 32 | my $scalar_ref = ''; | ||
| 33 | my $fh = 'STDOUT'; | ||
| 34 | return [\$scalar_ref,$fh]; | ||
| 35 | } | ||
| 36 | |||
| 37 | sub show { | ||
| 38 | # Showing a text view typically prints to STDOUT | ||
| 39 | my $self = shift; | ||
| 40 | my $widget = $self->widget(); | ||
| 41 | my ($content_ref,$output_stream) = @$widget; | ||
| 42 | $output_stream->print($$content_ref,"\n"); | ||
| 43 | } | ||
| 44 | |||
| 45 | sub _update_subject_from_view { | ||
| 46 | Carp::confess('currently text views are read-only!'); | ||
| 47 | } | ||
| 48 | |||
| 49 | sub _update_view_from_subject { | ||
| 50 | my $self = shift; | ||
| 51 | my $content = $self->_generate_content(@_); | ||
| 52 | my $widget = $self->widget(); | ||
| 53 | my ($content_ref,$fh) = @$widget; | ||
| 54 | $$content_ref = $content; | ||
| 55 | return 1; | ||
| 56 | } | ||
| 57 | |||
| 58 | # text view API | ||
| 59 | |||
| 60 | sub content { | ||
| 61 | # retuns the current value of the scalar ref containing the text content. | ||
| 62 | my $self = shift; | ||
| 63 | |||
| 64 | my $widget = $self->widget(); | ||
| 65 | if (@_) { | ||
| 66 | die "the widget reference for a view isn't changeable. change its content.."; | ||
| 67 | } | ||
| 68 | my ($content_ref,$output_stream) = @$widget; | ||
| 69 | return $$content_ref; | ||
| 70 | } | ||
| 71 | |||
| 72 | sub output_stream { | ||
| 73 | # retuns the current value of the handle to which we render. | ||
| 74 | my $self = shift; | ||
| 75 | my $widget = $self->widget(); | ||
| 76 | if (@_) { | ||
| 77 | return $widget->[1] = shift; | ||
| 78 | } | ||
| 79 | my ($content_ref,$output_stream) = @$widget; | ||
| 80 | return $output_stream; | ||
| 81 | } | ||
| 82 | |||
| 83 | sub _generate_content { | ||
| 84 | my $self = shift; | ||
| 85 | |||
| 86 | # the header line is the class followed by the id | ||
| 87 | my $text = $self->subject_class_name; | ||
| 88 | $text =~ s/::/ /g; | ||
| 89 | my $subject = $self->subject(); | ||
| 90 | if ($subject) { | ||
| 91 | my $subject_id_txt = $subject->id; | ||
| 92 | $subject_id_txt = "'$subject_id_txt'" if $subject_id_txt =~ /\s/; | ||
| 93 | $text .= " $subject_id_txt"; | ||
| 94 | } | ||
| 95 | |||
| 96 | # Don't recurse back into something we're already in the process of showing | ||
| 97 | if ($self->_subject_is_used_in_an_encompassing_view()) { | ||
| 98 | $text .= " (REUSED ADDR)\n"; | ||
| 99 | } else { | ||
| 100 | $text .= "\n"; | ||
| 101 | # the content for any given aspect is handled separately | ||
| 102 | my @aspects = $self->aspects; | ||
| 103 | my @sorted_aspects = map { $_->[1] } | ||
| 104 | sort { $a->[0] <=> $b->[0] } | ||
| 105 | map { [ $_->number, $_ ] } | ||
| 106 | @aspects; | ||
| 107 | for my $aspect (@sorted_aspects) { | ||
| 108 | next if $aspect->name eq 'id'; | ||
| 109 | my $aspect_text = $self->_generate_content_for_aspect($aspect); | ||
| 110 | $text .= $aspect_text; | ||
| 111 | } | ||
| 112 | } | ||
| 113 | |||
| 114 | return $text; | ||
| 115 | } | ||
| 116 | |||
| 117 | sub _generate_content_for_aspect { | ||
| 118 | # This does two odd things: | ||
| 119 | # 1. It gets the value(s) for an aspect, then expects to just print them | ||
| 120 | # unless there is a delegate view. In which case, it replaces them | ||
| 121 | # with the delegate's content. | ||
| 122 | # 2. In cases where more than one value is returned, it recycles the same | ||
| 123 | # view and keeps the content. | ||
| 124 | # | ||
| 125 | # These shortcuts make it hard to abstract out logic from toolkit-specifics | ||
| 126 | |||
| 127 | my $self = shift; | ||
| 128 | my $aspect = shift; | ||
| 129 | |||
| 130 | my $subject = $self->subject; | ||
| 131 | my $indent_text = $self->indent_text; | ||
| 132 | |||
| 133 | my $aspect_text = $indent_text . $aspect->label . ": "; | ||
| 134 | |||
| 135 | if (!$subject) { | ||
| 136 | $aspect_text .= "-\n"; | ||
| 137 | return $aspect_text; | ||
| 138 | } | ||
| 139 | |||
| 140 | my $aspect_name = $aspect->name; | ||
| 141 | |||
| 142 | my @value; | ||
| 143 | eval { | ||
| 144 | @value = $subject->$aspect_name; | ||
| 145 | }; | ||
| 146 | |||
| 147 | if (@value == 0) { | ||
| 148 | $aspect_text .= "-\n"; | ||
| 149 | return $aspect_text; | ||
| 150 | } | ||
| 151 | |||
| 152 | if (@value == 1 and ref($value[0]) eq 'ARRAY') { | ||
| 153 | @value = @{$value[0]}; | ||
| 154 | } | ||
| 155 | |||
| 156 | unless ($aspect->delegate_view) { | ||
| 157 | $aspect->generate_delegate_view; | ||
| 158 | } | ||
| 159 | |||
| 160 | # Delegate to a subordinate view if needed. | ||
| 161 | # This means we replace the value(s) with their | ||
| 162 | # subordinate widget content. | ||
| 163 | if (my $delegate_view = $aspect->delegate_view) { | ||
| 164 | # TODO: it is bad to recycle a view here?? | ||
| 165 | # Switch to a set view, which is the standard lister. | ||
| 166 | foreach my $value ( @value ) { | ||
| 167 | if (Scalar::Util::blessed($value)) { | ||
| 168 | $delegate_view->subject($value); | ||
| 169 | } | ||
| 170 | else { | ||
| 171 | $delegate_view->subject_id($value); | ||
| 172 | } | ||
| 173 | $delegate_view->_update_view_from_subject(); | ||
| 174 | $value = $delegate_view->content(); | ||
| 175 | } | ||
| 176 | } | ||
| 177 | |||
| 178 | if (@value == 1 and defined($value[0]) and index($value[0],"\n") == -1) { | ||
| 179 | # one item, one row in the value or sub-view of the item: | ||
| 180 | $aspect_text .= $value[0] . "\n"; | ||
| 181 | } | ||
| 182 | else { | ||
| 183 | my $aspect_indent; | ||
| 184 | if (@value == 1) { | ||
| 185 | # one level of indent for this sub-view's sub-aspects | ||
| 186 | # zero added indent for the identity line b/c it's next-to the field label | ||
| 187 | |||
| 188 | # aspect1: class with id ID | ||
| 189 | # sub-aspect1: value1 | ||
| 190 | # sub-aspect2: value2 | ||
| 191 | $aspect_indent = $indent_text; | ||
| 192 | } | ||
| 193 | else { | ||
| 194 | # two levels of indent for this sub-view's sub-aspects | ||
| 195 | # just one level for each identity | ||
| 196 | |||
| 197 | # aspect1: ... | ||
| 198 | # class with id ID | ||
| 199 | # sub-aspect1: value1 | ||
| 200 | # sub-aspect2: value2 | ||
| 201 | # class with id ID | ||
| 202 | # sub-aspect1: value1 | ||
| 203 | # sub-aspect2: value2 | ||
| 204 | $aspect_text .= "\n"; | ||
| 205 | $aspect_indent = $indent_text . $indent_text; | ||
| 206 | } | ||
| 207 | |||
| 208 | for my $value (@value) { | ||
| 209 | my $value_indented = ''; | ||
| 210 | if (defined $value) { | ||
| 211 | my @rows = split(/\n/,$value); | ||
| 212 | $value_indented = join("\n", map { $aspect_indent . $_ } @rows); | ||
| 213 | chomp $value_indented; | ||
| 214 | } | ||
| 215 | $aspect_text .= $value_indented . "\n"; | ||
| 216 | } | ||
| 217 | } | ||
| 218 | return $aspect_text; | ||
| 219 | } | ||
| 220 | |||
| 221 | 1; | ||
| 222 | |||
| 223 | |||
| 224 | =pod | ||
| 225 | |||
| 226 | =head1 NAME | ||
| 227 | |||
| 228 | UR::Object::View::Default::Text - object views in text format | ||
| 229 | |||
| 230 | =head1 SYNOPSIS | ||
| 231 | |||
| 232 | $o = Acme::Product->get(1234); | ||
| 233 | |||
| 234 | # generates a UR::Object::View::Default::Text object: | ||
| 235 | $v = $o->create_view( | ||
| 236 | toolkit => 'text', | ||
| 237 | aspects => [ | ||
| 238 | 'id', | ||
| 239 | 'name', | ||
| 240 | 'qty_on_hand', | ||
| 241 | 'outstanding_orders' => [ | ||
| 242 | 'id', | ||
| 243 | 'status', | ||
| 244 | 'customer' => [ | ||
| 245 | 'id', | ||
| 246 | 'name', | ||
| 247 | ] | ||
| 248 | ], | ||
| 249 | ], | ||
| 250 | ); | ||
| 251 | |||
| 252 | |||
| 253 | $txt1 = $v->content; | ||
| 254 | |||
| 255 | $o->qty_on_hand(200); | ||
| 256 | |||
| 257 | $txt2 = $v->content; | ||
| 258 | |||
| 259 | |||
| 260 | =head1 DESCRIPTION | ||
| 261 | |||
| 262 | This class implements basic text views of objects. It is used for command-line tools, | ||
| 263 | and is the base class for other specific text formats like XML, HTML, JSON, etc. | ||
| 264 | |||
| 265 | =head1 WRITING A SUBCLASS | ||
| 266 | |||
| 267 | # In Acme/Product/View/OutstandingOrders/Text.pm | ||
| 268 | |||
| 269 | package Acme::Product::View::OutstandingOrders::Text; | ||
| 270 | use UR; | ||
| 271 | |||
| 272 | class Acme::Product::View::OutstandingOrders::Text { | ||
| 273 | is => 'UR::Object::View::Default::Text' | ||
| 274 | }; | ||
| 275 | |||
| 276 | sub _initial_aspects { | ||
| 277 | return ( | ||
| 278 | 'id', | ||
| 279 | 'name', | ||
| 280 | 'qty_on_hand', | ||
| 281 | 'outstanding_orders' => [ | ||
| 282 | 'id', | ||
| 283 | 'status', | ||
| 284 | 'customer' => [ | ||
| 285 | 'id', | ||
| 286 | 'name', | ||
| 287 | ] | ||
| 288 | ], | ||
| 289 | ); | ||
| 290 | } | ||
| 291 | |||
| 292 | $v = $o->create_view(perspective => 'outstanding orders', toolkit => 'text'); | ||
| 293 | print $v->content; | ||
| 294 | |||
| 295 | =head1 SEE ALSO | ||
| 296 | |||
| 297 | UR::Object::View, UR::Object::View::Toolkit::Text, UR::Object | ||
| 298 | |||
| 299 | =cut | ||
| 300 |