The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
ContextualService 0.1.0

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Francis Hwang

Posts: 130
Nickname: francis
Registered: Jul, 2004

Francis Hwang is the Director of Technology at Rhizome.org.
ContextualService 0.1.0 Posted: Aug 29, 2005 1:06 AM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by Francis Hwang.
Original Post: ContextualService 0.1.0
Feed Title: Francis Hwang's site: ruby
Feed URL: http://fhwang.net/syndicate/ruby.atom
Feed Description: Author & artist Francis Hwang's personal site.
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Francis Hwang
Latest Posts From Francis Hwang's site: ruby

Advertisement

Here’s another library that comes from Lafcadio: ContextualService.

The ContextualService library makes it easy to manage and set services to a single, global resource such as a database or file system. By defining a service to be a child of ContextualService::Service, you

  • lock off access to instantiation, so all access have to go through the service’s get method,
  • and provide a simple way to set the instance, most likely for testing.

For example, Lafcadio uses ContextualService to manage access to the ObjectStore, which controls access to a database.

class Lafcadio::ObjectStore < ContextualService::Service
 def initialize
 ...
 end
end

ObjectStore.new will raise an error. To get an instance of ObjectStore, you call ObjectStore.get_object_store. If no instance has been previously set, this will instantiate the ObjectStore and save the instance for future accesses. If a mock instance has been previously set with set_object_store, it will simply return that mock instance.

Since access to the global service is attached to the class, there’s no need to pass in a mock service as an argument to any method that needs it. This can simplify testing considerably if you’ve got highly decomposed code that needs to access a global service from many places. Also, since the Context object is hidden, clients of the service don’t have to be conscious of it.

def some_method
 os = Lafcadio::ObjectStore.get_object_store
 # do something with the object store here
 some_other_method
end
def some_other_method
 os = Lafcadio::ObjectStore.get_object_store
 # do something with the object store here, too
end
class TestSomeMethod < Test::Unit::TestCase
 def test1
 mock_object_store = Lafcadio::MockObjectStore.new
 Lafcadio::ObjectStore.set_object_store mock_object_store
 some_method
 # assert something happened 
 end
end

Read: ContextualService 0.1.0

Topic: Movie night Previous Topic   Next Topic Topic: Nearing Greasemonkey for Browser-of-Choice with the Hoodlum Proxy!

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use