| 1 | package Sash::Plugin::VerticalResponse::Cursor; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use base qw( Sash::Cursor ); | ||
| 7 | use Carp; | ||
| 8 | |||
| 9 | # Apparently I will sell you the rope you are going to use to hang me with... | ||
| 10 | sub AUTOLOAD { | ||
| 11 | our $AUTOLOAD; | ||
| 12 | |||
| 13 | # No really what this does is allow sash to grow as the api grows and uses | ||
| 14 | # the same type of data structures. | ||
| 15 | if ( $AUTOLOAD =~ /::(\w*)\_meth$/ ) { | ||
| 16 | my $self = shift; | ||
| 17 | my $method = $1; | ||
| 18 | |||
| 19 | # Just in case you didn't grok it, $1 is the name of the method from | ||
| 20 | # the match above. | ||
| 21 | my $result = $self->{client}->$method( $self->query ); | ||
| 22 | |||
| 23 | # Account for the fact that create methods just return the | ||
| 24 | # id of the record. | ||
| 25 | $result = { id => $result } if $method =~ /^create/; | ||
| 26 | |||
| 27 | # In the delete methods case the query is a hash so we want the | ||
| 28 | # return value to be the record id that was passed to us. | ||
| 29 | $result = { id => ( %{$self->query} )[1] } if $method =~ /^delete/; | ||
| 30 | |||
| 31 | $self->_define_header_data( $result ); | ||
| 32 | |||
| 33 | return; | ||
| 34 | } | ||
| 35 | } | ||
| 36 | |||
| 37 | sub open { | ||
| 38 | my $class = shift; | ||
| 39 | my $args = shift; | ||
| 40 | |||
| 41 | # We have a usage relationship with this singleton. | ||
| 42 | $args->{client} = Sash::Plugin::VerticalResponse->client; | ||
| 43 | |||
| 44 | my $self = $class->SUPER::open( $args ); | ||
| 45 | |||
| 46 | return $self; | ||
| 47 | } | ||
| 48 | |||
| 49 | sub _define_header_data { | ||
| 50 | my $self = shift; | ||
| 51 | my $result = shift; | ||
| 52 | my $attributes = shift; | ||
| 53 | |||
| 54 | my $header; | ||
| 55 | my $data; | ||
| 56 | |||
| 57 | # Help format VR Special Datatypes for simple output. | ||
| 58 | my $format_data_attribute = sub { | ||
| 59 | my $attribute = shift; | ||
| 60 | |||
| 61 | my $joinby = ( Sash::Properties->output eq Sash::Properties->vertical ) ? "\n" : ', '; | ||
| 62 | |||
| 63 | if ( ref $attribute eq 'ArrayOfString' ) { | ||
| 64 | return undef unless scalar( @$attribute ) > 0; | ||
| 65 | return join $joinby, @$attribute; | ||
| 66 | } | ||
| 67 | |||
| 68 | return $attribute; | ||
| 69 | }; | ||
| 70 | |||
| 71 | # The real purpose of this method is to convert what otherwise would be nested | ||
| 72 | # data into data that is at the same level so to speak for each attribute in | ||
| 73 | # the nested structure. | ||
| 74 | my $expand_record = sub { | ||
| 75 | my $record = shift; | ||
| 76 | |||
| 77 | my $expanded_record = { }; | ||
| 78 | my $header; | ||
| 79 | |||
| 80 | foreach my $attribute ( sort keys %$record ) { | ||
| 81 | if ( ref $record->{$attribute} eq 'NVDictionary' ) { | ||
| 82 | $expanded_record = { %$expanded_record, map { $_->{name} => $_->{value} } @{$record->{$attribute}} }; | ||
| 83 | } elsif ( ref $record->{$attribute} eq 'StreetAddress' ) { | ||
| 84 | $expanded_record = { %$expanded_record, map { $_ => $record->{$attribute}->{$_} } sort keys %{$record->{$attribute}} }; | ||
| 85 | } else { | ||
| 86 | $expanded_record->{$attribute} = $record->{$attribute}; | ||
| 87 | } | ||
| 88 | } | ||
| 89 | |||
| 90 | return $expanded_record; | ||
| 91 | }; | ||
| 92 | |||
| 93 | my $use_result = $result; | ||
| 94 | $use_result = [ $use_result ] unless uc( ref $use_result ) =~ /^ARRAY/; | ||
| 95 | |||
| 96 | $data = [ | ||
| 97 | map { | ||
| 98 | my $record = $expand_record->( $_ ); | ||
| 99 | $header = [ sort keys %$record ] unless defined $header; | ||
| 100 | [ map { $format_data_attribute->( $record->{$_} ) } sort keys %$record ]; | ||
| 101 | } @$use_result | ||
| 102 | ]; | ||
| 103 | |||
| 104 | $self->result( $result ); | ||
| 105 | $self->header( $header ); | ||
| 106 | $self->data( $data ); | ||
| 107 | |||
| 108 | return; | ||
| 109 | } | ||
| 110 | |||
| 111 | sub getCompany_meth { | ||
| 112 | my $self = shift; | ||
| 113 | |||
| 114 | my $company = [ $self->{client}->getCompany( $self->query ) ]; | ||
| 115 | |||
| 116 | my @users; | ||
| 117 | |||
| 118 | if ( defined $company->[0]->{users} ) { | ||
| 119 | @users = $company->{users}; | ||
| 120 | delete $company->{users}; | ||
| 121 | my @user_ids = map { $_->{id} } @users; | ||
| 122 | $company->{user_ids} = @user_ids; | ||
| 123 | } | ||
| 124 | |||
| 125 | $self->_define_header_data( $company ); | ||
| 126 | |||
| 127 | return; | ||
| 128 | } | ||
| 129 | |||
| 130 | sub show_proc { | ||
| 131 | my $self = shift; | ||
| 132 | |||
| 133 | my $header = [ 'Methods' ]; | ||
| 134 | my $data = [ map { [ $_ ] } sort $self->{client}->_methods ]; | ||
| 135 | |||
| 136 | $self->header( $header ); | ||
| 137 | $self->data( $data ); | ||
| 138 | |||
| 139 | return; | ||
| 140 | } | ||
| 141 | |||
| 142 | 1; |