Kudos to my colleague, Kunal (@techthumb) for showing me this today.
I wanted a single instance (singleton) of a 3rd-party client object to be available across my app. Ideally, I should be able to ask this wrapper the same questions as I would ask the client itself (delegation).
Here's an example of how this mix of the singleton and delegator patterns can be achieved in Ruby. The example uses the google_places gem by @marceldegraaf:
class GooglePlacesClient < SimpleDelegator
client = GooglePlaces::Client.new("[my_api_key]")
The two ruby components used here are:
The SimpleDelegator superclass - which delegates all supported method calls to the object passed into the constructor (in this case client). In other words, my GooglePlacesClient.instance now responds to all of the messages that the gem's GooglePlaces::Client responds to.
The Singleton mixin - which ensures that only one instance of my GooglePlacesClient can be created.
For completeness, here's an example of how you'd consume this singleton class:
This delegates the 'spots' message to the instance variable which in turn responds with an array of type GooglePlaces::Spot for the supplied latitude, longitude.