Class: WebFunction::Promise

Inherits:
Object
  • Object
show all
Defined in:
lib/web_function/promise.rb

Defined Under Namespace

Classes: Path

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pipeline, path) ⇒ Promise

Returns a new instance of Promise.



3
4
5
6
7
# File 'lib/web_function/promise.rb', line 3

def initialize(pipeline, path)
  @pipeline = pipeline
  @path = Path.new(path)
  @value = nil
end

Instance Attribute Details

#valueObject



64
65
66
67
68
69
70
# File 'lib/web_function/promise.rb', line 64

def value
  unless @value
    raise UnresolvedPromise
  end

  @value
end

Instance Method Details

#[](key) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/web_function/promise.rb', line 56

def [](key)
  if @value
    @value[key]
  else
    @path[key]
  end
end

#resolveObject



72
73
74
75
76
77
78
79
80
# File 'lib/web_function/promise.rb', line 72

def resolve
  if @value
    return @value
  end

  @pipeline.execute

  value
end

#to_json(*args) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/web_function/promise.rb', line 48

def to_json(*args)
  if @value
    @value.to_json(*args)
  else
    @path.to_json(*args)
  end
end

#to_sObject



40
41
42
43
44
45
46
# File 'lib/web_function/promise.rb', line 40

def to_s
  if @value
    @value.to_s
  else
    @path.to_s
  end
end