Sha256: 00758cdbffd855068a22b8fed25617903bcea2c80dd4642793a0a95b3caa48aa

Contents?: true

Size: 1.83 KB

Versions: 1

Compression:

Stored size: 1.83 KB

Contents

class ASS::CallbackFactory

  module ServiceMethods
    def resend
      throw(:__ass_resend)
    end
    
    def discard
      throw(:__ass_discard)
    end
    
    def header
      @__header__
    end

    def payload
      @__payload__
    end

    def method
      @__method__
    end

    def data
      @__data__
    end

    def meta
      @__meta__
    end

    def version
      @__version__
    end

    def call(name,method,data=nil,opts={},meta=nil)
      @__service__.call(name,method,data,opts,meta)
    end

    def cast(name,method,data=nil,opts={},meta=nil)
      @__service__.cast(name,method,data,opts,meta)
    end
  end
  
  def initialize(callback)
    @factory = build_factory(callback)
  end

  def callback_for(server,header,payload)
    # method,data
    if @factory.is_a? Class
      if @factory.respond_to? :version
        klass = @factory.get_version(payload["version"])
      else
        klass = @factory
      end
      obj = klass.new
    else
      obj = @factory
    end
    obj.instance_variable_set("@__service__",server)
    obj.instance_variable_set("@__header__",header)
    obj.instance_variable_set("@__payload__",payload)
    obj.instance_variable_set("@__method__",payload["method"])
    obj.instance_variable_set("@__data__",payload["data"])
    obj.instance_variable_set("@__meta__",payload["meta"])
    obj.instance_variable_set("@__version__",payload["version"])
    obj
  end

  private

  def build_factory(callback)
    c = case callback
        when Proc
          Class.new &callback
        when Class
          callback
        when Module
          Class.new { include callback }
        else
          raise "can build factory from one of Proc, Class, Module"
        end
    case c
    when Class
      c.instance_eval { include ServiceMethods }
    else
      c.extend ServiceMethods
    end
    c
  end
  

  
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
donkey-0.1.0 lib/ass/callback_factory.rb