Module: Btk

Included in:
Gtk::Widget
Defined in:
lib/btk.rb

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *argv, &block) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/btk.rb', line 54

def method_missing(name,*argv,&block)
  if name.to_s =~ /^sig_(.*)$/
    return self.signal_connect($1,&block)
  else
    ob=eval("Btk.#{name}(*argv,&block)")
    if self.respond_to? 'pack_start'
      opts=Array(Btk.get_options(argv)[:pack])
      default_opts=[true,true,0]
      opts.each_index{|idx|
        default_opts[idx]=opts[idx] unless opts[idx].nil?
      }
      opts=default_opts
      self.pack_start(ob,opts[0],opts[1],opts[2])
    elsif self.respond_to? 'add'
      self.add(ob)
    end
    return ob
  end
end

Class Method Details

.get_options(argv) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/btk.rb', line 8

def self.get_options(argv)
  options={}
  if argv.last.is_a? Hash
    options=argv.pop
  end
  options
end

.method_missing(name, *argv, &block) ⇒ Object



16
17
18
19
20
21
22
23
24
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
# File 'lib/btk.rb', line 16

def self.method_missing(name,*argv,&block)
  real_name=nil
  [Gtk,Object].each{|t|
    if t.const_defined?(name.to_sym)
      real_name="#{t.name}::#{name}"
    end
  }
  raise "undefined class #{name}" unless real_name
  name=real_name
  options=get_options(argv)

  #create object
  ob=eval("#{name}.new(#{parse_params(:argv,argv)})")

  #parse options
  options.each{|key,value|
    #ignore :pack
    next if key==:pack
    ob_method=nil

    #two methods supported
    methods=["#{key}=".to_s,"set_#{key}".to_s]
    methods.each{|method|
      if ob.respond_to?(method)
        ob_method=method
        break
      end
    }
    ob_method or raise "cannot find method in #{methods.join(',')}"
    value=Array(value)
    eval("ob.send(ob_method,#{parse_params(:value,value)})")
  }
  if block_given?
    yield(ob)
  end
  ob
end

.parse_params(name, argv) ⇒ Object



4
5
6
# File 'lib/btk.rb', line 4

def self.parse_params(name,argv)
  (0..Array(argv).size-1).collect{|idx| "#{name}[#{idx}]"}.join(',')
end