Module: Goo::PropsInit

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.append_features(klass) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/goocanvas.rb', line 34

def self.append_features(klass)
  super
  arity = klass.instance_method(:initialize).arity
  raise 'the initialize method of a class including PropsInit must have a fixed arity' if arity < 0
  args_list = (1..arity).collect { |i| "param#{i}" }.join(", ")
  klass.module_eval <<-END
    alias :_initialize :initialize
    def initialize(#{args_list}, *args)
      _initialize(#{args_list})
      init_props(*args)
    end
    
    alias :_set_property :set_property
    def set_property(prop_name, value)
      pspec = self.class.property(prop_name)
      value = value.to_goo if pspec.value_type.name =~ /^GooCairo/ and value.respond_to?(:to_goo)
      _set_property(prop_name, value)
    end        
  END
end

Instance Method Details

#init_props(*args) ⇒ Object



29
30
31
32
# File 'lib/goocanvas.rb', line 29

def init_props(*args)
  hash = Goo.args_to_hash(args)
  hash.each_pair { |key, value| set_property(key.to_s.gsub(/-/, '_').to_sym, value) }
end