Module: Wx::KeywordConstructor

Defined in:
lib/wx/keyword_ctors.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/wx/keyword_ctors.rb', line 150

def self.included(klass)
  klass.extend ClassMethods
  klass.module_eval do

    alias :pre_wx_kwctor_init :initialize

    # The new definition of initialize; accepts a parent arg
    # mixed_args, which may zero or more position args, optionally
    # terminated with hash keyword args, and an optional block
    def initialize(parent = :default_ctor, *mixed_args, &block)
      # allow zero-args ctor for use with XRC
      if parent == :default_ctor
        pre_wx_kwctor_init()
        return
      end

      begin
        real_args = [ parent ] + self.class.args_as_list(*mixed_args)
        pre_wx_kwctor_init(*real_args)
      rescue => err
        msg = "Error initializing #{self.inspect}\n"+
              " : #{err.message} \n" +
              "Correct parameters for #{self.class.name}.new are:\n" + 
               self.class.describe_constructor()

        new_err = err.class.new(msg)
        new_err.set_backtrace(caller)
        Kernel.raise new_err
      end

      # If a block was given, pass the newly created Window instance
      # into it; use block
      if block
        if block.arity == -1 or block.arity == 0
          self.instance_eval(&block)
        elsif block.arity == 1
          block.call(self)
        else 
          Kernel.raise ArgumentError, 
                       "Block to initialize accepts zero or one arg"
        end
      end
    end
  end
  
  # Any class inheriting from a class including this module must have
  # its own copy of the param_spec
  def klass.inherited(sub_klass)
    sub_klass.instance_variable_set(:@param_spec, 
                                    instance_variable_get(:@param_spec) )
  end
end