future-resource 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/CHANGELOG.md +2 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +20 -0
- data/README.md +42 -0
- data/Rakefile +1 -0
- data/future-resource.gemspec +20 -0
- data/lib/future-resource/version.rb +3 -0
- data/lib/future-resource.rb +35 -0
- metadata +58 -0
data/.gitignore
ADDED
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Jay Phillips
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
# FutureResource
|
2
|
+
|
3
|
+
future-resource allows you to wait on a final value being set for a placeholder, which may occur asynchronously.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
gem install future-resource
|
7
|
+
|
8
|
+
## Usage
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
require 'future-resource'
|
12
|
+
|
13
|
+
fr = FutureResource.new
|
14
|
+
|
15
|
+
Thread.new do
|
16
|
+
sleep 10
|
17
|
+
fr.resource = :foo
|
18
|
+
end
|
19
|
+
|
20
|
+
p fr.set_yet?
|
21
|
+
p fr.resource
|
22
|
+
```
|
23
|
+
|
24
|
+
You will see `false` printed first, followed by a delay before `:foo` is printed
|
25
|
+
|
26
|
+
## Links:
|
27
|
+
* [Source](https://github.com/adhearsion/future-resource)
|
28
|
+
* [Documentation](http://rdoc.info/github/adhearsion/future-resource/master/frames)
|
29
|
+
* [Bug Tracker](https://github.com/adhearsion/future-resource/issues)
|
30
|
+
|
31
|
+
## Note on Patches/Pull Requests
|
32
|
+
|
33
|
+
* Fork the project.
|
34
|
+
* Make your feature addition or bug fix.
|
35
|
+
* Add tests for it. This is important so I don't break it in a future version unintentionally.
|
36
|
+
* Commit, do not mess with rakefile, version, or history.
|
37
|
+
* If you want to have your own version, that is fine but bump version in a commit by itself so I can ignore when I pull
|
38
|
+
* Send me a pull request. Bonus points for topic branches.
|
39
|
+
|
40
|
+
## Copyright
|
41
|
+
|
42
|
+
Copyright (c) 2011 Jay Phillips. MIT licence (see LICENSE for details).
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "future-resource/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "future-resource"
|
7
|
+
s.version = FutureResource::VERSION
|
8
|
+
s.authors = ["Jay Phillips", "Ben Langfeld"]
|
9
|
+
s.email = ["dev@adhearsion.com"]
|
10
|
+
s.homepage = "https://github.com/adhearsion/future-resource"
|
11
|
+
s.summary = %q{Wait on resources being set in the future}
|
12
|
+
s.description = %q{Sometimes a value is set asynchronously and you need to wait until it appears. Easy!}
|
13
|
+
|
14
|
+
s.rubyforge_project = "future-resource"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require "thread"
|
2
|
+
require "monitor"
|
3
|
+
|
4
|
+
class FutureResource
|
5
|
+
def initialize
|
6
|
+
@resource_lock = Monitor.new
|
7
|
+
@resource_value_blocker = @resource_lock.new_cond
|
8
|
+
end
|
9
|
+
|
10
|
+
def set_yet?
|
11
|
+
!!@resource_lock.synchronize { defined? @resource }
|
12
|
+
end
|
13
|
+
|
14
|
+
def resource
|
15
|
+
@resource_lock.synchronize do
|
16
|
+
@resource_value_blocker.wait unless defined? @resource
|
17
|
+
@resource
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def resource=(resource)
|
22
|
+
@resource_lock.synchronize do
|
23
|
+
raise ResourceAlreadySetException if defined? @resource
|
24
|
+
@resource = resource
|
25
|
+
@resource_value_blocker.broadcast
|
26
|
+
@resource_value_blocker = nil # Don't really need it anymore.
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
class ResourceAlreadySetException < StandardError
|
31
|
+
def initialize
|
32
|
+
super "Cannot set this resource twice!"
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
metadata
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: future-resource
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Jay Phillips
|
9
|
+
- Ben Langfeld
|
10
|
+
autorequire:
|
11
|
+
bindir: bin
|
12
|
+
cert_chain: []
|
13
|
+
date: 2011-08-17 00:00:00.000000000 +01:00
|
14
|
+
default_executable:
|
15
|
+
dependencies: []
|
16
|
+
description: Sometimes a value is set asynchronously and you need to wait until it
|
17
|
+
appears. Easy!
|
18
|
+
email:
|
19
|
+
- dev@adhearsion.com
|
20
|
+
executables: []
|
21
|
+
extensions: []
|
22
|
+
extra_rdoc_files: []
|
23
|
+
files:
|
24
|
+
- .gitignore
|
25
|
+
- CHANGELOG.md
|
26
|
+
- Gemfile
|
27
|
+
- LICENSE.txt
|
28
|
+
- README.md
|
29
|
+
- Rakefile
|
30
|
+
- future-resource.gemspec
|
31
|
+
- lib/future-resource.rb
|
32
|
+
- lib/future-resource/version.rb
|
33
|
+
has_rdoc: true
|
34
|
+
homepage: https://github.com/adhearsion/future-resource
|
35
|
+
licenses: []
|
36
|
+
post_install_message:
|
37
|
+
rdoc_options: []
|
38
|
+
require_paths:
|
39
|
+
- lib
|
40
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
47
|
+
none: false
|
48
|
+
requirements:
|
49
|
+
- - ! '>='
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: '0'
|
52
|
+
requirements: []
|
53
|
+
rubyforge_project: future-resource
|
54
|
+
rubygems_version: 1.6.2
|
55
|
+
signing_key:
|
56
|
+
specification_version: 3
|
57
|
+
summary: Wait on resources being set in the future
|
58
|
+
test_files: []
|