Sha256: fe4487f8890f095b91d70d48437971495246109ee7ef9269325b5e96791ba0ca

Contents?: true

Size: 1002 Bytes

Versions: 4

Compression:

Stored size: 1002 Bytes

Contents

require 'weary/response'

module Weary
  # A handy abstract proxy class that is used to proxy a domain model
  # in an asynchronous fashion. Useful if you're not interested in a
  # Weary::Response object, but you want to pass that into a domain object
  # when the response is available.
  class Deferred < defined?(::BasicObject) ? ::BasicObject : ::Object

    instance_methods.each {|m| undef_method(m) if m.to_s !~ /(?:^__|^nil\?$|^send$|^object_id$)/ } \
      unless defined? ::BasicObject

    attr_reader :callable

    def initialize(future, model, callable=nil)
      @future = future
      @model = model
      @callable = callable || ::Proc.new{|klass, response| klass.new(response) }
      @target = nil
    end

    def complete?
      !!@target
    end

    def method_missing(method, *args, &block)
      if @model.method_defined? method
        @target ||= @callable.call @model, @future
        @target.send(method, *args, &block)
      else
        super
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
weary-1.1.3 lib/weary/deferred.rb
weary-1.1.2 lib/weary/deferred.rb
weary-1.1.1 lib/weary/deferred.rb
weary-1.1.0 lib/weary/deferred.rb