Class: CTioga2::MetaBuilder::Type

Inherits:
Object
  • Object
show all
Defined in:
lib/ctioga2/metabuilder/type.rb

Overview

A class that handles a parameter type. It has to be subclassed to actually provide a parameter. The subclasses must provide the following:

  • a #string_to_type function to convert from string to the type;

  • a #type_to_string to convert back from type to string

  • an instance #type_name that returns a really small description of the type, to be used for instance to name command-line parameters.

  • a #type_name statement that registers the current class to the Type system.

Moerover, it is a good idea to reimplement the #qt4_create_input_widget method; the default implementation works, but you probably wish it would look better.

Types are implemented using hashes: this way, additionnal parameters can easily be added. The hash must have a :type key that will be interpreted by the children of Type. Examples:

{ :type => :integer}
{ :type => :file, :filter => "Text Files (*.txt)}

And so on. You definitely should document your type and it’s attributes properly, if you ever want that someone uses it.

The list of currently recognised types is here:

:integer

Types::IntegerParameter

:float

Types::FloatParameter

:string

Types::StringParameter

:file

Types::FileParameter

:boolean

Types::BooleanParameter

:list

Types::ListParameter

Additionally to the parameters the given type is requiring, you can pass some other kind of information using this hash, such as option parser short argument, aliases, and so on. This has nothing to do with type conversion, but it is the best place where to put this kind of things, in my humble opinion. The currently recognized such additional parameters are:

  • :option_parser_short: a short option name for option_parser.

  • :namespace: a ruby module that will be searched by #string_to_type for a constant. If one of the given name is found, its value is returned.

  • :shortctus: a hash specifiying strings shortcuts for given values. Elements of this hash that are regular expressions are taken

Constant Summary collapse

@@types =

A hash that makes the :type value of the type argument correspond to a Type child

{ }

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type) ⇒ Type

A default constructor. It should be safe to use it directly for children, unless something more specific is needed. Any descendent should always register type as @type - or, even better, call super.



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/ctioga2/metabuilder/type.rb', line 107

def initialize(type)
  if type.is_a?(Symbol)
    type = {:type => type}
  end
  @type = type
  if @type[:shortcuts]
    @shortcuts = @type[:shortcuts]
    @re_shortcuts = {}
    for k,v in @shortcuts
      if k.is_a? Regexp
        @re_shortcuts[k] = v
      end
    end
  end
end

Instance Attribute Details

#re_shortcutsObject

A hash Regexp -> value. All elements will be looked for matches for every single string conversion, so don’t dump too many of them here.



101
102
103
# File 'lib/ctioga2/metabuilder/type.rb', line 101

def re_shortcuts
  @re_shortcuts
end

#shortcutsObject

A hash shortcut -> value. Can be nil



96
97
98
# File 'lib/ctioga2/metabuilder/type.rb', line 96

def shortcuts
  @shortcuts
end

#typeObject

The initial type specification that was given to the Type



93
94
95
# File 'lib/ctioga2/metabuilder/type.rb', line 93

def type
  @type
end

Class Method Details

.from_string(type, string) ⇒ Object

Shortcut to convert directly a string to the given type specification. Handy shortcut.



159
160
161
# File 'lib/ctioga2/metabuilder/type.rb', line 159

def self.from_string(type, string)
  return get_type(type).string_to_type(string)
end

.get_param_type(type) ⇒ Object

This function converts a ‘description’ (see the Type) of the type wanted into a Type child. As a special treat, a lone symbol is converted into => :symbol

Raises:



144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/ctioga2/metabuilder/type.rb', line 144

def self.get_param_type(type)
  if type.is_a?(Symbol)
    type = {:type => type}
  end
  raise InvalidType,"The type argument must be a Hash" unless 
    type.is_a?(Hash)
  begin
    return @@types.fetch(type[:type])
  rescue
    raise InvalidType, "Type #{type[:type]} unknown to the type system"
  end
end

.get_type(type) ⇒ Object

Returns a Type child instance suitable for conversion of the given type specification



165
166
167
168
169
170
# File 'lib/ctioga2/metabuilder/type.rb', line 165

def self.get_type(type)
  if type.is_a? Type
    return type
  end
  return get_param_type(type).new(type)
end

.type_name(name, public_name = nil, default_value = nil) ⇒ Object

This class function actually registers the current type to the Type ancestor. name should be a symbol. Moreover, if the second argument is provided, it automatically creates a #type_name instance method returning this value.



127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/ctioga2/metabuilder/type.rb', line 127

def self.type_name(name, public_name = nil, default_value = nil)
  if @@types.has_key?(name)
    warn "Redefining type #{name} " +
      "from #{@@types[name]} to #{self}"
  end
  @@types[name] = self
  self.send(:define_method,:type_name) do
    public_name
  end
  self.send(:define_method,:default_value) do
    default_value
  end
end

Instance Method Details

#boolean?Boolean

Whether the type is a boolean. Booleans are special cased for their use in the command-line.

Returns:

  • (Boolean)


257
258
259
# File 'lib/ctioga2/metabuilder/type.rb', line 257

def boolean?
  return false
end

#default_valueObject

Returns a default value for the given type. This is reimplemented systematically from children, with the Type::type_name statement.



215
216
# File 'lib/ctioga2/metabuilder/type.rb', line 215

def default_value
end

#option_parser_long_option(name, param = nil) ⇒ Object

Returns a value to be fed to OptionParser#on as a ‘long’ option. It is separated from the rest to allow easy redefinition (in special cases). name is the name of the option.



249
250
251
252
253
# File 'lib/ctioga2/metabuilder/type.rb', line 249

def option_parser_long_option(name, param = nil)
  param ||= type_name
  param = param.gsub(/\s+/, '_')
  return "--#{name} #{param.upcase}"
end

#option_parser_option(parser, name, desc, &block) ⇒ Object

Creates an option for the OptionParser parser. The block is fed with the converted value. The default implementation should be fine for most classes, but this still leaves the room for reimplementation if necessary. The parameters are:

  • parser: the OptionParser;

  • name: the name of the option;

  • desc: it description,

  • block: the block used to set the data.



237
238
239
240
241
242
243
# File 'lib/ctioga2/metabuilder/type.rb', line 237

def option_parser_option(parser, name, desc, &block)
  args = [option_parser_long_option(name), desc]
  if @type.has_key?(:option_parser_short)
    args.unshift(@type[:option_parser_short])
  end
  option_parser_raw(parser, *args, &block)
end

#string_to_type(string) ⇒ Object

This function converts the given string to the appropriate type. It is a wrapper around the #string_to_type_internal function that can take advantage of a few general features. It is recommanded to define a #string_to_type_internal function rather to redefine #string_to_type



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/ctioga2/metabuilder/type.rb', line 179

def string_to_type(string)
  # First, shortcuts:
  if @shortcuts and @shortcuts.key? string
    return stt_run_hook(@shortcuts[string])
  end
  if @re_shortcuts
    for k, v in @re_shortcuts
      if string =~ k
        return stt_run_hook(v)
      end
    end
  end
  # Then, constants lookup.
  if @type.key?(:namespace)
    begin
      return stt_run_hook(lookup_const(string))
    rescue Exception
    end
  end
  return stt_run_hook(string_to_type_internal(string))
end

#type_nameObject

Returns a type name suitable for displaying, for instance, in an option parser, or inside a dialog box, and so on. Has to be one word (not to confuse the option parser, for instance); it is better if it is lowercase.



223
224
225
# File 'lib/ctioga2/metabuilder/type.rb', line 223

def type_name
  return 'notype'
end

#type_to_string(type) ⇒ Object

This function does the exact opposite of the #string_to_type one. It defaults to using the to_s methods of the parameter. Be careful: it is absolutely important that for any valid type,

string_to_type(type_to_string(type)) == type


207
208
209
# File 'lib/ctioga2/metabuilder/type.rb', line 207

def type_to_string(type)
  return type_to_string_internal(type)
end