Class: Tweetwine::Promise

Inherits:
BasicObject
Defined in:
lib/tweetwine/promise.rb

Overview

Lazy evaluation via proxy object.

A naive implementation, but it’s enough.

Adapted from the book Ruby Best Practices, by Gregory Brown.

Instance Method Summary collapse

Constructor Details

#initialize(&action) ⇒ Promise

Returns a new instance of Promise.



10
11
12
# File 'lib/tweetwine/promise.rb', line 10

def initialize(&action)
  @action = action
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args, &blk) ⇒ Object



35
36
37
# File 'lib/tweetwine/promise.rb', line 35

def method_missing(*args, &blk)
  __result__.__send__(*args, &blk)
end

Instance Method Details

#__result__Object



14
15
16
17
18
19
20
# File 'lib/tweetwine/promise.rb', line 14

def __result__
  if @action
    @result = @action.call
    @action = nil
  end
  @result
end

#inspectObject



22
23
24
25
26
27
28
# File 'lib/tweetwine/promise.rb', line 22

def inspect
  if @action
    "#<Tweetwine::Promise action=#{@action.inspect}>"
  else
    @result.inspect
  end
end

#respond_to?(method) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
33
# File 'lib/tweetwine/promise.rb', line 30

def respond_to?(method)
  method = method.to_sym
  [:__result__, :inspect].include?(method) || __result__.respond_to?(method)
end