1package Goo::Thing;
2
3###############################################################################
4# Nigel Hamilton
5#
6# Copyright Nigel Hamilton 2005
7# All Rights Reserved
8#
9# Author:       Nigel Hamilton
10# Filename:     Goo::Thing.pm
11# Description:  A new generic type of "Thing" in The Goo based on global config
12#               files. A Thing is a handle on an underlying Thing.
13#
14# Date          Change
15# -----------------------------------------------------------------------------
16# 15/06/2005    Auto generated file
17# 15/06/2005    Needed a generic thing
18# 01/08/2005    Simplified action handling
19# 11/10/2005    Added method: getLocation
20# 18/10/2005    Added method: getDatabaseRow
21# 19/10/2005    Added method: getColumns
22#
23###############################################################################
24
25use strict;
26
27use Cwd;
28use Goo::Object;
29use Data::Dumper;
30
31# use Smart::Comments;
32use Goo::TrailManager;
33
34use base qw(Goo::Object);
35
36
37###############################################################################
38#
39# new - construct a Thing
40#
41###############################################################################
42
43sub new {
44
45    my ($class, $filename) = @_;
46
47    my $this = $class->SUPER::new();
48
49    unless ($filename) {
50        die("Can't find Thing. No filename found at: " . caller());
51    }
52
53    # extract the prefix and suffix
54    if ($filename =~ /(.*)\.(.*)$/) {
55        $this->{prefix} = $1;
56        $this->{suffix} = $2;
57    } else {
58
59        # it may be all suffix, example: goo -m goo
60        $this->{suffix} = $filename;
61    }
62
63    # remember the filename
64    $this->{filename} = $filename;
65
66    # load the config_file
67    my $config_file = Goo::ConfigFile->new($this->{suffix} . ".goo");
68
69    ### The config file should contain the actions
70    ### $config_file->to_string()
71    unless ($config_file) {
72        die("Can't create Thing. No config file found for $this->{suffix}.");
73    }
74
75    # merge all the config fields with this object
76    %$this = (%$this, %$config_file);
77
78    return $this;
79
80}
81
82
83###############################################################################
84#
85# get_filename - all Things must have a "filename" - even database Things!
86#
87###############################################################################
88
89sub get_filename {
90
91    my ($this) = @_;
92
93    # this is the ID of the handle on the Thing!
94    return $this->{filename};
95
96}
97
98
99###############################################################################
100#
101# get_suffix - return the Thing suffix
102#
103###############################################################################
104
105sub get_suffix {
106
107    my ($this) = @_;
108
109    return $this->{suffix};
110
111}
112
113
114###############################################################################
115#
116# get_prefix - get the full contents of the file
117#
118###############################################################################
119
120sub get_prefix {
121
122    my ($this) = @_;
123
124    return $this->{prefix};
125
126}
127
128
129###############################################################################
130#
131# can_do_action - can this thing do the action?
132#
133###############################################################################
134
135sub can_do_action {
136
137    my ($this, $action) = @_;
138
139    return exists $this->{actions}->{$action};
140
141}
142
143
144###############################################################################
145#
146# get_commands - return a list of commands
147#
148###############################################################################
149
150sub get_commands {
151
152    my ($this) = @_;
153
154    my @commands;
155
156    foreach my $letter (sort { $a cmp $b } keys %{ $this->{actions} }) {
157
158        push(@commands, $this->{actions}->{$letter}->{command});
159
160    }
161
162    return @commands;
163
164}
165
166
167###############################################################################
168#
169# do_action - execute action
170#
171###############################################################################
172
173sub do_action {
174
175    my ($this, $action_letter, @parameters) = @_;
176
177    unless ($this->isa("Goo::Thing")) {
178        print("Invalid Thing.");
179        print Dumper($this);
180    }
181
182
183    #unless ($action_letter eq "B") {
184
185    # this is a new step in the trail - record it
186    Goo::TrailManager::save_goo_action($this, $this->{actions}->{$action_letter}->{command});
187
188    # reset the trail position
189    #Goo::TrailManager::reset_last_action();
190
191    my $module = $this->{actions}->{$action_letter}->{action};
192
193    # strip action handler of .pm suffix
194    $module =~ s/\.pm$//;
195
196    # Goo::Prompter::trace("about to require this $module");
197
198    ### $this->{actions}->{E}->{action} = "ProgramEditor";
199    eval "require $module;";
200
201    if ($@) {
202        die("Evaled failed $@");
203    }
204
205    ### $this->{actions}->{E}->{action} = "ProgramEditor";
206    my $actor = $module->new();
207
208    $actor->run($this, @parameters);
209
210}
211
2121;
213
214
215__END__
216
217=head1 NAME
218
219Goo::Thing - A "Thing" in your working environment that you can do actions to
220
221=head1 SYNOPSIS
222
223use Goo::Thing;
224
225=head1 DESCRIPTION
226
227A "Thing" is something you perform actions on in your working environment. It could be a file, a database entity or
228configuration file.
229
230Everytime you perform an action on a Thing it is recorded in the Goo Trail.
231
232The Goo Trail records all your temporal associations between Things in your environment.
233
234=head1 METHODS
235
236=over
237
238=item new
239
240construct a Thing
241
242=item get_filename
243
244all Things must have a "filename" or "handle" - even database Things!
245
246=item get_suffix
247
248return the Thing suffix
249
250=item get_prefix
251
252get the full contents of the file
253
254=item can_do_action
255
256can this Thing do the action?
257
258=item get_commands
259
260return a list of commands
261
262=item do_action
263
264execute the action
265
266=back
267
268=head1 AUTHOR
269
270Nigel Hamilton <nigel@trexy.com>
271
272=head1 SEE ALSO
273
274Tour    http://thegoo.org/goo-tour.pdf (big)
275
276Web     http://thegoo.org
277
278Blog    http://blog.thegoo.org
279
280