Skip to content
Browse files

Add some real code

  • Loading branch information...
1 parent f78762f commit ef031a67101ec837143c9a3dc06f4af9df1fb4b0 @benlangfeld benlangfeld committed Aug 17, 2011
Showing with 39 additions and 14 deletions.
  1. +6 −6 future-resource.gemspec
  2. +31 −4 lib/future-resource.rb
  3. +2 −4 lib/future-resource/version.rb
View
12 future-resource.gemspec
@@ -4,12 +4,12 @@ require "future-resource/version"
Gem::Specification.new do |s|
s.name = "future-resource"
- s.version = Future::Resource::VERSION
- s.authors = ["Ben Langfeld"]
- s.email = ["ben@langfeld.me"]
- s.homepage = ""
- s.summary = %q{TODO: Write a gem summary}
- s.description = %q{TODO: Write a gem description}
+ s.version = FutureResource::VERSION
+ s.authors = ["Jay Phillips", "Ben Langfeld"]
+ s.email = ["dev@adhearsion.com"]
+ s.homepage = "https://github.com/adhearsion/future-resource"
+ s.summary = %q{Wait on resources being set in the future}
+ s.description = %q{Sometimes a value is set asynchronously and you need to wait until it appears. Easy!}
s.rubyforge_project = "future-resource"
View
35 lib/future-resource.rb
@@ -1,7 +1,34 @@
-require "future-resource/version"
+require "thread"
-module Future
- module Resource
- # Your code goes here...
+class FutureResource
+ def initialize
+ @resource_lock = Monitor.new
+ @resource_value_blocker = @resource_lock.new_cond
+ end
+
+ def set_yet?
+ @resource_lock.synchronize { defined? @resource }
+ end
+
+ def resource
+ @resource_lock.synchronize do
+ @resource_value_blocker.wait unless defined? @resource
+ @resource
+ end
+ end
+
+ def resource=(resource)
+ @resource_lock.synchronize do
+ raise ResourceAlreadySetException if defined? @resource
+ @resource = resource
+ @resource_value_blocker.broadcast
+ @resource_value_blocker = nil # Don't really need it anymore.
+ end
+ end
+
+ class ResourceAlreadySetException < StandardError
+ def initialize
+ super "Cannot set this resource twice!"
+ end
end
end
View
6 lib/future-resource/version.rb
@@ -1,5 +1,3 @@
-module Future
- module Resource
- VERSION = "0.0.1"
- end
+class FutureResource
+ VERSION = "0.0.1"
end

0 comments on commit ef031a6

Please sign in to comment.
Something went wrong with that request. Please try again.