Sha256: ecc4311cb5580040a29e9bc48e0c0865f5ef06e6e766cc072d98f3e2a89e3a9f

Contents?: true

Size: 1.15 KB

Versions: 1

Compression:

Stored size: 1.15 KB

Contents

# encoding: utf-8

module Hatetepe
  class Promise < ::Promise
    include ::Promise::Progress

    private

    def defer
      EM.next_tick { yield }
    end

    def wait
      fiber = Fiber.current
      resume = proc do |arg|
        defer { fiber.resume(arg) }
      end

      self.then(resume, resume)
      Fiber.yield
    end

    module Attribute
      def self.included(klass)
        klass.extend(ClassMethods)
      end

      def initialize(*args)
        super(*args)
        promises.map { |name| send(name) }
      end

      private

      def promises
        self.class.send(:promises)
      end

      def get_or_set_promise(ivar)
        if instance_variable_defined?(ivar)
          instance_variable_get(ivar)
        else
          instance_variable_set(ivar, Promise.new)
        end
      end

      module ClassMethods
        private

        def promises
          @promises ||= []
        end

        def promise(name)
          promises << name
          define_promise(name, "@#{name}")
        end

        def define_promise(name, ivar)
          define_method(name) { get_or_set_promise(ivar) }
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hatetepe-0.6.0.pre.2 lib/hatetepe/promise.rb