In Ruby, how to implement global behaviour?
- by Gordon McAllister
Hi all,
I want to implement the concept of a Workspace. This is a global concept - all other code will interact with one instance of this Workspace. The Workspace will be responsible for maintaining the current system state (i.e. interacting with the system model, persisting the system state etc)
So what's the best design strategy for my Workspace, bearing in mind this will have to be testable (using RSpec now, but happy to look at alternatives).
Having read thru some open source projects out there and I've seen 3 strategies. None of which I can identify as "the best practice".
They are:
Include the singleton class. But how testable is this? Will the global state of Workspace change between tests?
Implemented all behaviour as class methods. Again how do you test this?
Implemented all behaviour as module methods. Not sure about this one at all!
Which is best? Or is there another way?
Thanks,
Gordon