Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > accessing one objects methods from within another object

Reply
Thread Tools

accessing one objects methods from within another object

 
 
Adam Akhtar
Guest
Posts: n/a
 
      11-29-2008
Ive created a simple todo list app for the console and tried to
implement mvc using my very very very limited mvc knowledge. In the app
users can create single tasks. they can also group tasks with a similiar
theme together into a project. I thought id learn more about mvc and oop
than just reading or using some framework.

So i have two basic class definitions

class Project_controller
class Task_controller

Ive added basic CRUD functionality to them. But now they need to
interact with one anohter in a few situations. For example, when a user
wants to delete a project they might also want to delete the associated
tasks with that project. So in project_controller.delete() I want to be
able to access the task objects delete method. I thought about passing
the object itself as a parameter e.g

class Project_controller
def delete a_task_controller
#blah blah blah
#user says yes delete the associated tasks
a_task_controller.delete
end

but that doesnt seem pratical because if i start adding other
controllers such as say "Category" i could end up with a huge parameter
list

I then thought about using initialize like this

class Project_controller
def initialize a_task_controller
@a_task_controller = a_task_controller
end
end

but then ill also need to do the same with Task_controller and that will
lead to a run error due to the ole what comes first the chicken or the
egg.

task_cntr = Task_controller.new (proj_cntr) #proj_cntr doesnt exist yet
so boom!
proj_cntr = Project_controller.new (task_cntr) #this is ok though

Whats teh best way of doing this, is there a way i make all controller
objects be able to access other controllers methods? Is the observer
pattern useful here? Ive never used it but know a bit about it.












Sorry if the titles vague. Im trying to develop a simple todo list
console app and decided to try and use mvc principles with what limited
knowledge of mvc i have.

in the apps a user can define projects such as "Buy a new PC" and then
assign new tasks to this project such as "Research Anandtech for best
value parts", "find cheapest price of parts on froogle.com".

Hence i have a projects controller class and a tasks controller class.
At the moment they are defined as seperate classes but i can see now
that they could inheirt from an abstract parent "Controller" class but
at the time i didnt know what the common code would be as im a newb.

Alls gone well as i created pretty much each respective items basic CRUD
code and they didnt interact. However when it comes to deleting a
project i realised that the user should be able to decide if they want
to delete the tasks associated with it as well. I dont want to copy and
paste my delete code from my tasks controller over the project
controller as its duplication and contradicts oop.

I want to be able to do just call the task controller objects delete
method from within the project controller objects delete method.

like

class project_controller
def delete
#choose project to delete
project_to_delete = users_choice()
#now display the associated tasks with this project
task_controller_object.display_tasks_for_project(p roject_to_delete)
#find out if user wants to delete tasks as well..
user_wants_to_delete = get_response()
if user_wants_to_delete == true
#use the task cntr object once again to do its own dirty work
task_controller_object.delete(all_tasks_associated _with_this_project)
...
...
blah blah blah
end
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Adam Akhtar
Guest
Posts: n/a
 
      11-29-2008
sorry the bottom part of that mail was a previous version of my message,
forgot to delete it
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
David A. Black
Guest
Posts: n/a
 
      11-29-2008
Hi --

On Sat, 29 Nov 2008, Adam Akhtar wrote:

> Ive created a simple todo list app for the console and tried to
> implement mvc using my very very very limited mvc knowledge. In the app
> users can create single tasks. they can also group tasks with a similiar
> theme together into a project. I thought id learn more about mvc and oop
> than just reading or using some framework.
>
> So i have two basic class definitions
>
> class Project_controller
> class Task_controller
>
> Ive added basic CRUD functionality to them. But now they need to
> interact with one anohter in a few situations. For example, when a user
> wants to delete a project they might also want to delete the associated
> tasks with that project. So in project_controller.delete() I want to be
> able to access the task objects delete method. I thought about passing
> the object itself as a parameter e.g
>
> class Project_controller
> def delete a_task_controller
> #blah blah blah
> #user says yes delete the associated tasks
> a_task_controller.delete
> end
>
> but that doesnt seem pratical because if i start adding other
> controllers such as say "Category" i could end up with a huge parameter
> list
>
> I then thought about using initialize like this
>
> class Project_controller
> def initialize a_task_controller
> @a_task_controller = a_task_controller
> end
> end
>
> but then ill also need to do the same with Task_controller and that will
> lead to a run error due to the ole what comes first the chicken or the
> egg.
>
> task_cntr = Task_controller.new (proj_cntr) #proj_cntr doesnt exist yet
> so boom!
> proj_cntr = Project_controller.new (task_cntr) #this is ok though
>
> Whats teh best way of doing this, is there a way i make all controller
> objects be able to access other controllers methods? Is the observer
> pattern useful here? Ive never used it but know a bit about it.


It looks like you're trying to accomplish the M part of MVC in the C
part. You should think about having not just controller classes, but a
Task class and a Project class. The Task and Project objects would
have knowledge of their relation (a task is part of a project, etc.),
so that when you delete a project, it can delete the tasks that were
part of it.

The controllers should just be firing off the high-level commands,
like telling a project to die. What happens when a project dies is
really in the purview of the Project object itself.


David

--
Rails training from David A. Black and Ruby Power and Light:
INTRO TO RAILS (Jan 12-15) | ADVANCING WITH RAILS (Jan 19-22) *
Both in Fort Lauderdale, FL * Co-taught with Patrick Ewing
See http://www.rubypal.com for details
Coming in 2009: The Well-Grounded Rubyist (http://manning.com/black2)

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there a way to find the class methods of a class, just like'methods' finds the instance methods? Kenneth McDonald Ruby 5 09-26-2008 03:09 PM
Calling internal 'public' methods within js objects bizt Javascript 5 05-22-2008 12:44 AM
Problem with Calling Methods from Objects which are stored in an array from within another Object Andreas Schmitt C++ 6 07-29-2005 03:44 PM
accessing one HTML/Control object from another darrel ASP .Net 1 10-01-2004 10:36 AM
Passing value from one script on one page to another script on another page. Robert Cohen ASP General 3 07-15-2003 01:46 PM



Advertisments