# class DBus::ProxyObject

D-Bus proxy object class

Class representing a remote object in an external application. Typically, calling a method on an instance of a ProxyObject sends a message over the bus so that the method is executed remotely on the correctponding object.

### Attributes

bus[R]

The bus the object is reachable via.

default_iface[RW]

@return [String] The name of the default interface of the object.

destination[R]

The (remote) destination of the object.

introspected[RW]

Flag determining whether the object has been introspected.

path[R]

The path to the object.

subnodes[RW]

The names of direct subnodes of the object in the tree.

### Public Class Methods

new(bus, dest, path) click to toggle source

Creates a new proxy object living on the given bus at destination dest on the given path.

    # File lib/dbus/proxy_object.rb, line 33
    def initialize(bus, dest, path)
      @bus, @destination, @path = bus, dest, path
      @interfaces = Hash.new
      @subnodes = Array.new
    end

### Public Instance Methods

[](intfname) click to toggle source

Retrieves an interface of the proxy object @param [String] intfname @return [ProxyObjectInterface]

    # File lib/dbus/proxy_object.rb, line 47
    def [](intfname)
      @interfaces[intfname]
    end

[]=(intfname, intf) click to toggle source

Maps the given interface name intfname to the given interface _intf. @param [String] intfname @param [ProxyObjectInterface] intf @return [ProxyObjectInterface]

    # File lib/dbus/proxy_object.rb, line 55
    def []=(intfname, intf)
      @interfaces[intfname] = intf
    end

define_shortcut_methods() click to toggle source

For each non duplicated method name in any interface present on the caller, defines a shortcut method dynamically. This function is automatically called when a {ProxyObject} is introspected.

    # File lib/dbus/proxy_object.rb, line 73
    def define_shortcut_methods
      # builds a list of duplicated methods
      dup_meths, univocal_meths = [],{}
      @interfaces.each_value do |intf|
        intf.methods.each_value do |meth|
          name = meth.name.to_sym
          # don't overwrite instance methods!
          if dup_meths.include? name or self.class.instance_methods.include? name
            next
          elsif univocal_meths.include? name
            univocal_meths.delete name
            dup_meths << name
          else
            univocal_meths[name] = intf
          end
        end
      end
      univocal_meths.each do |name, intf|
        # creates a shortcut function that forwards each call to the method on
        # the appropriate intf
        singleton_class.class_eval do
          define_method name do |*args, &reply_handler|
            intf.method(name).call(*args, &reply_handler)
          end
        end
      end
    end

has_iface?(name) click to toggle source

Returns whether the object has an interface with the given name.

    # File lib/dbus/proxy_object.rb, line 102
    def has_iface?(name)
      raise "Cannot call has_iface? if not introspected" if not @introspected
      @interfaces.member?(name)
    end

interfaces() click to toggle source

Returns the interfaces of the object.

    # File lib/dbus/proxy_object.rb, line 40
    def interfaces
      @interfaces.keys
    end

introspect() click to toggle source

Introspects the remote object. Allows you to find and select interfaces on the object.

    # File lib/dbus/proxy_object.rb, line 61
    def introspect
      # Synchronous call here.
      xml = @bus.introspect_data(@destination, @path)
      ProxyObjectFactory.introspect_into(self, xml)
      define_shortcut_methods()
      xml
    end

on_signal(name, &block) click to toggle source

Registers a handler, the code block, for a signal with the given name. It uses default_iface which must have been set. @return [void]

    # File lib/dbus/proxy_object.rb, line 110
    def on_signal(name, &block)
      if @default_iface and has_iface?(@default_iface)
        @interfaces[@default_iface].on_signal(name, &block)
      else
        # TODO improve
        raise NoMethodError
      end
    end

### Private Instance Methods

method_missing(name, *args, &reply_handler) click to toggle source

Handles all unkown methods, mostly to route method calls to the default interface.

    # File lib/dbus/proxy_object.rb, line 124
    def method_missing(name, *args, &reply_handler)
      if @default_iface and has_iface?(@default_iface)
        begin
          @interfaces[@default_iface].method(name).call(*args, &reply_handler)
        rescue NameError => e
          # interesting, foo.method("unknown")
          # raises NameError, not NoMethodError
          raise unless e.to_s =~ /undefined method `#{name}'/
          # BTW e.exception("...") would preserve the class.
          raise NoMethodError,"undefined method `#{name}' for DBus interface `#{@default_iface}' on object `#{@path}'"
        end
      else
        # TODO distinguish:
        # - di not specified
        #TODO
        # - di is specified but not found in introspection data
        raise NoMethodError, "undefined method `#{name}' for DBus interface `#{@default_iface}' on object `#{@path}'"
      end
    end

singleton_class() click to toggle source

Returns the singleton class of the object.

    # File lib/dbus/proxy_object.rb, line 145
    def singleton_class
      (class << self ; self ; end)
    end