Glue/Object.pm untested

filter: Statementstmt 0.0% 0 / 43 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 9 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 69 SCAR 30.0
CC
4
Cov
0%
_unknown_method
20.0
_init
2.0
set
2.0
CRAP 20.0
1 package Mac::AppleScript::Glue::Object;
2  
3 =head1 NAME
4  
5 Mac::AppleScript::Glue::Object - represents an AppleScript object reference
6  
7  
8 =head1 SYNOPSIS
9  
10 use Mac::AppleScript::Glue::Object;
11  
12 my $obj = new Mac::AppleScript::Glue::Object(
13 ref => 'folder "Applications"',
14 app => $my_app,
15 );
16  
17  
18 =head1 DESCRIPTION
19  
20 Objects of this module store an AppleScript "object reference," which
21 is a way that AppleScript refers to an object.
22  
23 See L<Mac::AppleScript::Glue> for full information on how to use this
24 package.
25  
26 =cut
27  
28 use strict;
29 use warnings;
30  
31 ######################################################################
32  
33 use Carp;
34  
35 use Mac::AppleScript::Glue qw(to_string);
36  
37 use base qw(Mac::AppleScript::Glue);
38  
39 ######################################################################
40 # methods
41  
42 =head1 METHODS
43  
44 =over 4
45  
46 =cut
47  
48 ######################################################################
49  
50 =item new
51  
52 Creates a new Mac::AppleScript::Glue::Object object.
53  
54 =cut
55  
56 sub _init {
57 my ($self, $args) = @_;
58  
59 $self->{_app} = undef;
60 $self->{_ref} = undef;
61  
62 return $self->SUPER::_init($args);
63 }
64  
65 ######################################################################
66  
67 =item app
68  
69 Contains a Mac::AppleScript::Application object to which to send any
70 AppleScripts. If not set, then the script is run without targeting
71 any application.
72  
73 =cut
74  
75 # implemented via $AUTOLOAD
76  
77 ######################################################################
78  
79 =item ref
80  
81 The value of the object reference as a string.
82  
83 =cut
84  
85 # implemented via $AUTOLOAD
86  
87 ######################################################################
88  
89 #
90 # Called by the AUTOLOADer in Mac::AppleScript::Glue when the
91 # specified method isn't known.
92 #
93 # Usage: $obj->_unknown_method('method' [, $arg]
94 # [, param1 => val1, param2 => val2]);
95 #
96  
97 sub _unknown_method {
98 my ($self, $method, @args) = @_;
99  
100 my $expr;
101  
102 #
103 # implicit "get" on any method
104 #
105  
106 ## $expr .= 'get ';
107  
108 #
109 # we start out with the method, with spaces converted to
110 # underscores
111 #
112  
113 $method =~ s/_/ /g;
114  
115 $expr .= $method;
116  
117 #
118 # if there's only one argument, or there's an odd number (meaning
119 # one argument and a hash), then we add on the argument to the
120 # expression
121 #
122  
123 if (@args == 1 || @args % 2 != 0) {
124 $expr .= ' ' . to_string(shift @args);
125 }
126  
127 #
128 # we add the reference (this isn't always the right way to do it)
129 #
130 # an alternate way (which doesn't quite work either) is to
131 # enclosed the entire expression in a "tell" block:
132 #
133 # $expr = 'tell ' . $self->ref . "\n"
134 # . "$expr\n"
135 # . "end tell";
136 #
137  
138 ;;if ($self->ref !~ /^application "/) {
139 $expr .= ' of (' . $self->ref . ')';
140 ;;}
141  
142 #
143 # if there are parameters, we add each parameter
144 #
145  
146 if (@args) {
147 my %args = @args;
148  
149 while (my ($name, $value) = each %args) {
150 $name =~ s/_/ /g;
151  
152 $expr .= " $name " . to_string($value);
153 }
154 }
155  
156 #
157 # finally, we run the expression, either using the application
158 # specified in the object, or the object itself
159 #
160  
161 my $app = $self->app || $self;
162  
163 return $app->run($expr);
164 }
165  
166 ######################################################################
167  
168 =item set(arg1 => val1, arg2 => val2, ...)
169  
170 Sets the value of one or more properties in the object reference.
171 Returns the result, if any, of the last "set" property.
172  
173 =cut
174  
175 sub set {
176 my ($self, %args) = @_;
177  
178 my $app = $self->app || $self;
179  
180 my $r;
181  
182 while (my ($method, $value) = each %args) {
183 $method =~ s/_/ /g;
184  
185 $r = $app->run(
186 "set $method of ("
187 . $self->ref
188 . ') to '
189 . to_string($value)
190 );
191 }
192  
193 return $r;
194 }
195  
196 ######################################################################
197  
198 =back
199  
200 =head1 SEE ALSO
201  
202 L<Mac::AppleScript::Glue>
203  
204 L<Mac::AppleScript::Glue::Application>
205  
206  
207 =head1 AUTHOR
208  
209 John Labovitz E<lt>johnl@johnlabovitz.comE<gt>
210  
211  
212 =head1 COPYRIGHT
213  
214 Copyright (c) 2002 John Labovitz. All rights reserved. This program is
215 free software; you can redistribute it and/or modify it under the same
216 terms as Perl itself.
217  
218 =cut
219  
220 1;