Class: Conn
Instance Method Summary collapse
- #handle(req) ⇒ Object
-
#initialize(params) ⇒ Conn
constructor
A new instance of Conn.
- #request(method, *args) ⇒ Object
Constructor Details
#initialize(params) ⇒ Conn
Returns a new instance of Conn.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/dnode/conn.rb', line 10 def initialize params @block = params[:block] || lambda {} @instance = params[:instance] || {} @conn = params[:conn] @scrub = Scrub.new @remote = {} request('methods', if @instance.is_a? Proc then @instance.call(*[@remote,self][0..@instance.arity-1]) else @instance end ) end |
Instance Method Details
#handle(req) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/dnode/conn.rb', line 25 def handle req args = @scrub.unscrub(req) do |id| lambda { |*argv| self.request(id, *argv) } end if req['method'].is_a? Integer then id = req['method'] cb = @scrub.callbacks[id] if cb.arity < 0 then cb.call(*JSObject.create(args)) else argv = *JSObject.create(args) padding = argv.length.upto(cb.arity - 1).map{ nil } argv = argv.concat(padding).take(cb.arity) cb.call(*argv) end elsif req['method'] == 'methods' then @remote.update(args[0]) js = JSObject.create(@remote) if @block.arity === 0 then @block.call else @block.call(*[ js, self ][ 0 .. @block.arity - 1 ]) end self.emit('remote', js) self.emit('ready') end end |
#request(method, *args) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/dnode/conn.rb', line 55 def request method, *args scrubbed = @scrub.scrub(args) @conn.send_data(JSON( { :method => ( if method.respond_to? :match and method.match(/^\d+$/) then method.to_i else method end ), :links => [], }.merge(scrubbed) ) + "\n") end |