Class: Ruber::KTextEditorWrapper::InterfaceProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/ruber/editor/ktexteditor_wrapper.rb

Overview

Helper class which, given a Qt::Object implementing different interfaces, allows to access method provided by one interface without exposing the object itself.

Instance Method Summary collapse

Constructor Details

#initialize(obj) ⇒ InterfaceProxy

Creates a new InterfaceProxy. obj is the Qt::Object to cast to the appropriate interface



53
54
55
56
# File 'lib/ruber/editor/ktexteditor_wrapper.rb', line 53

def initialize obj
  @obj = obj
  @interface = nil
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &blk) ⇒ Object

Passes the method call to the object cast to the current interface. If the argument list contains instances of classes which mix-in KTextEditorWrapper, they’re replaced with the object they wrap before being passed to the interface.



79
80
81
82
# File 'lib/ruber/editor/ktexteditor_wrapper.rb', line 79

def method_missing name, *args, &blk
  args.map!{|a| a.is_a?(KTextEditorWrapper) ? a.send(:internal) : a}
  @interface.send name, *args, &blk
end

Instance Method Details

#interface=(iface) ⇒ Object

Sets the interface to which the object is going to be cast. iface can be either the name of an interface, as symbol or string, both in camelcase or snakecase (withouth the KTextEditor namespace) or the class itself. After a call to this method (and until the next one) method_missing will redirect all unknown method calls to this interface.



65
66
67
68
69
70
71
72
# File 'lib/ruber/editor/ktexteditor_wrapper.rb', line 65

def interface= iface
  cls = if iface.is_a? Class then iface
  else
    name = iface.to_s.split('_').map(&:capitalize).join ''
    KTextEditor.const_get(name)
  end
  @interface = @obj.qobject_cast cls
end