Class: Future::Value
- Inherits:
-
Object
show all
- Defined in:
- lib/futurevalue/value.rb
Constant Summary
collapse
- MINIMUM_WORKERS =
7
Class Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(&block) ⇒ Value
Returns a new instance of Value.
11
12
13
14
15
16
17
18
19
20
21
22
|
# File 'lib/futurevalue/value.rb', line 11
def initialize(&block)
self.class.pool.workers = MINIMUM_WORKERS if self.class.pool.workers < MINIMUM_WORKERS
@response_queue = Queue.new
self.class.pool << lambda do
begin
@response_queue << block.call
rescue Exception => @exception
@response_queue << nil
end
end
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
37
38
39
|
# File 'lib/futurevalue/value.rb', line 37
def method_missing(method, *args, &block)
self.value.send(method, *args, &block)
end
|
Class Attribute Details
.pool ⇒ Object
Returns the value of attribute pool.
8
9
10
|
# File 'lib/futurevalue/value.rb', line 8
def pool
@pool
end
|
Instance Method Details
#inspect ⇒ Object
49
50
51
52
53
54
55
56
57
58
59
|
# File 'lib/futurevalue/value.rb', line 49
def inspect
if !ready?
"#<#{self.class.name} @value=[pending]>"
else
unless @exception
"#<#{self.class.name} @value=#{self.value.inspect}>"
else
"#<#{self.class.name} #{@exception.inspect}>"
end
end
end
|
#ready? ⇒ Boolean
33
34
35
|
# File 'lib/futurevalue/value.rb', line 33
def ready?
@response_queue.nil? || !@response_queue.empty?
end
|
#to_s ⇒ Object
41
42
43
|
# File 'lib/futurevalue/value.rb', line 41
def to_s
self.value.to_s
end
|
#to_str ⇒ Object
45
46
47
|
# File 'lib/futurevalue/value.rb', line 45
def to_str
self.value.to_str
end
|
#value ⇒ Object
24
25
26
27
28
29
30
31
|
# File 'lib/futurevalue/value.rb', line 24
def value
if @response_queue
@value = @response_queue.pop
@response_queue = nil
end
raise @exception if @exception
@value
end
|