Glue/Object.pm untested

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