#
# Exploratory program to see what kind of method_missing we would need in a
# module. In the end, this code would have to be generated by the Builder,
# or be provided by a mixin.
#

$LOAD_PATH.unshift File.join(File.dirname(__FILE__), 'lib')
require 'girepository'

module GIRepository
  class ITypeInfo
    def to_ffi
      return :pointer if pointer?
      return GIRepository::IRepository.type_tag_to_string(tag).to_sym
    end
  end
end

module Gtk
  extend FFI::Library

  ffi_lib "gtk-x11-2.0"
  @@gir = GIRepository::IRepository.default
  @@gir.require "Gtk", nil
  def self.method_missing method, *arguments
    go = @@gir.find_by_name "Gtk", method.to_s

    super if go.nil?
    super if go.type != :function

    sym = go.symbol
    args = go.args.map {|a| a.type.to_ffi}
    rt = go.return_type.to_ffi

    puts "attach_function :#{sym}, [#{args.map {|a| ":#{a}"}.join ", "}], :#{rt}"
    Gtk.module_eval do
      attach_function sym, args, rt
      eigenclass = class << self; self; end
      eigenclass.class_eval do
	define_method(method) do |*a|
	  puts "would have sent #{sym} #{a.map.join(", ")}"
	  self.send sym, *a
	end
      end
    end

    #puts Gtk.public_methods - Module.public_methods
    self.send method, *arguments
  end
end

Gtk.init 0, nil
Gtk.flub

Add exploratory test program: Uses repository, generates methods dynamically.
Add exploratory test program: Uses repository, generates methods dynamically.
