Class: Future::Value

Inherits:
Object
  • 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

.poolObject (readonly)

Returns the value of attribute pool.



8
9
10
# File 'lib/futurevalue/value.rb', line 8

def pool
  @pool
end

Instance Method Details

#inspectObject



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

Returns:

  • (Boolean)


33
34
35
# File 'lib/futurevalue/value.rb', line 33

def ready?
  @response_queue.nil? || !@response_queue.empty?
end

#to_sObject



41
42
43
# File 'lib/futurevalue/value.rb', line 41

def to_s
  self.value.to_s
end

#to_strObject



45
46
47
# File 'lib/futurevalue/value.rb', line 45

def to_str
  self.value.to_str
end

#valueObject

Raises:

  • (@exception)


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