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