Module: Wx::KeywordConstructor::ClassMethods

Defined in:
lib/wx/keyword_ctors.rb

Defined Under Namespace

Classes: Parameter

Constant Summary collapse

STANDARD_DEFAULTS =

Common Wx constructor argument keywords, with their default values.

{
  :id        => -1,
  :size      => Wx::DEFAULT_SIZE,
  :pos       => Wx::DEFAULT_POSITION,
  :style     => 0,
  :title     => '',
  :validator => Wx::DEFAULT_VALIDATOR,
  :choices   => [] # for Choice, ComboBox etc
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#param_specObject



114
115
116
# File 'lib/wx/keyword_ctors.rb', line 114

def param_spec
  @param_spec ||= []
end

Instance Method Details

#args_as_hash(*mixed_args) ⇒ Object



153
154
155
156
157
158
159
# File 'lib/wx/keyword_ctors.rb', line 153

def args_as_hash(*mixed_args)
  kwa = mixed_args.last.kind_of?(Hash) ? mixed_args.pop : {}
  param_spec.zip(mixed_args) do | param, arg |
    kwa[param.name] = arg if arg
  end
  kwa 
end

#args_as_list(*mixed_args) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/wx/keyword_ctors.rb', line 134

def args_as_list(*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

#describe_constructorObject



161
162
163
164
165
# File 'lib/wx/keyword_ctors.rb', line 161

def describe_constructor()
  param_spec.inject("") do | desc, param |
    desc << "#{param.name} (#{param.default.class.name})\n"
  end
end

#wx_ctor_params(*params) ⇒ Object

Adds a list of named parameters params to the parameter specification for this Wx class’s constructor. Each parameter should be specified as a either a common known symbol, such as :size or :pos: or :style: (corresponding to the common constructor arguments in WxWidgets API), or a single-key with the key the name of the argument, and the value a default value.

Parameters should be specified in the order they occur in the Wx API constructor



127
128
129
130
131
132
# File 'lib/wx/keyword_ctors.rb', line 127

def wx_ctor_params(*params)
  self.param_spec += params.map do | param |
    param.kind_of?(Hash) ? Parameter[*param.to_a.flatten] : 
      Parameter[param, STANDARD_DEFAULTS[param] ]
  end
end