lib/volt/utils/promise_extensions.rb in volt-0.9.3.pre5 vs lib/volt/utils/promise_extensions.rb in volt-0.9.3.pre6
- old
+ new
@@ -2,22 +2,31 @@
require 'volt/utils/promise'
# A temp patch for promises until https://github.com/opal/opal/pull/725 is released.
class Promise
+ # We made a choice not to support comparitors and << and >> on method_missing
+ # on Promises. This makes it easier to understand what promise proxying does
+ # and how it works. It also prevents confusing situations where you try to
+ # == compare two Promises for example. The cost though is more code to do
+ # comparisons, but we feel it is worth it.
+ def respond_to_missing?(method_name, include_private = false)
+ !!(method_name =~ /[a-z_]\w*[?!=]?/)
+ end
+
def method_missing(method_name, *args, &block)
- promise = self.then do |value|
- value.send(method_name, *args, &block)
- end
+ if respond_to_missing?(method_name)
+ promise = self.then do |value|
+ value.send(method_name, *args, &block)
+ end
- promise
+ promise
+ else
+ super
+ end
end
- def respond_to_missing(method_name, include_private = false)
- true
- end
-
# Allow .each to be called directly on promises
def each(&block)
raise ArgumentError, 'no block given' unless block
self.then do |val|
@@ -53,9 +62,23 @@
end
def value_or_error
@value || @error
end
+
+ # When testing with rspec, add in a custom exception! method that doesn't
+ # swallow ExpectationNotMetError's.
+ if defined?(RSpec::Expectations::ExpectationNotMetError)
+ def exception!(error)
+ if error.is_a?(RSpec::Expectations::ExpectationNotMetError)
+ raise error
+ end
+ @exception = true
+
+ reject!(error)
+ end
+ end
+
# Waits for the promise to resolve (assuming it is blocking on
# the server) and returns the result.
def sync