Module: DeferrableGratification::Primitives

Included in:
DeferrableGratification
Defined in:
lib/deferrable_gratification/primitives.rb

Overview

Trivial operations which return Deferrables.

Used internally by the library, and may be useful in cases where you need to return a Deferrable to keep API compatibility but the result is already available.

DeferrableGratification extends this module, and thus the methods here are accessible via the DG alias.

Instance Method Summary collapse

Instance Method Details

#blankObject

Return a completely uninteresting Deferrable.



53
54
55
# File 'lib/deferrable_gratification/primitives.rb', line 53

def blank
  DeferrableGratification::DefaultDeferrable.new
end

#const(value) ⇒ Object

Return a Deferrable which immediately succeeds, passing a constant value to callbacks.



23
24
25
# File 'lib/deferrable_gratification/primitives.rb', line 23

def const(value)
  success(value)
end

#failure(message) ⇒ Object #failure(exception_class) ⇒ Object #failure(exception_class, message) ⇒ Object #failure(exception) ⇒ Object

Return a Deferrable which immediately fails with an exception.

Overloads:

  • #failure(message) ⇒ Object

    Passes RuntimeError.new(message) to errbacks.

  • #failure(exception_class) ⇒ Object

    Passes exception_class.new to errbacks.

  • #failure(exception_class, message) ⇒ Object

    Passes exception_class.new(message) to errbacks.

  • #failure(exception) ⇒ Object

    Passes exception to errbacks.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/deferrable_gratification/primitives.rb', line 37

def failure(exception_class_or_message, message_or_nil = nil)
  blank.tap do |d|
    d.fail(
      case exception_class_or_message
      when Exception
        raise ArgumentError, "can't specify both exception and message" if message_or_nil
        exception_class_or_message
      when Class
        exception_class_or_message.new(message_or_nil)
      else
        RuntimeError.new(exception_class_or_message.to_s)
      end)
  end
end

#success(*values) ⇒ Object

Return a Deferrable which immediately succeeds, passing 0 or more values to callbacks.



17
18
19
# File 'lib/deferrable_gratification/primitives.rb', line 17

def success(*values)
  blank.tap {|d| d.succeed(*values) }
end