Sha256: 80198a1b43c96606dd10b55d9c508abd78794849204dedf9155ae18cd3e0ee06

Contents?: true

Size: 1.07 KB

Versions: 10

Compression:

Stored size: 1.07 KB

Contents

# Various non-GUI helper functions
module Wx
  # A named parameter in a Wx named-arg parameter list
  Parameter = Struct.new( :name, :default )

  # Convert mixed positional / named args into a list to be passed to
  # an underlying API method. +param_spec+ is an Array of Parameter
  # structs containing the keyword name and default value for each
  # possible argument. +mixed_args+ is an array which may optionally end
  # with a set of named arguments

  def self.args_as_list(param_spec, *mixed_args)
    # get keyword arguments from mixed args if supplied, else empty
    kwa = mixed_args.last.kind_of?(Hash) ? mixed_args.pop : {}
    out_args = []
    param_spec.each_with_index do | param, i |
      if arg = mixed_args[i] # use the supplied list arg 
        out_args << arg
      elsif kwa.key?(param.name) # use the keyword arg
        out_args << kwa[param.name]
      else # use the default argument
        out_args << param.default
      end
    end
    out_args
  rescue
    Kernel.raise ArgumentError, 
                 "Bad arg composition of #{mixed_args.inspect}"
  end
end

Version data entries

10 entries across 10 versions & 2 rubygems

Version Path
wxruby-1.9.8-universal-darwin-9 lib/wx/helpers.rb
wxruby-1.9.8-x86-linux lib/wx/helpers.rb
wxruby-1.9.8-x86-mingw32 lib/wx/helpers.rb
wxruby-1.9.8-x86-mswin32-60 lib/wx/helpers.rb
wxruby-1.9.8-x86_64-linux lib/wx/helpers.rb
wxruby-1.9.9-universal-darwin-9 lib/wx/helpers.rb
wxruby-1.9.9-x86-linux lib/wx/helpers.rb
wxruby-1.9.9-x86-mingw32 lib/wx/helpers.rb
wxruby-1.9.9-x86-mswin32-60 lib/wx/helpers.rb
wxruby-ruby19-1.9.8-x86-darwin-9 lib/wx/helpers.rb