Class: CTioga2::MetaBuilder::Type
- Inherits:
-
Object
- Object
- CTioga2::MetaBuilder::Type
- 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
Direct Known Subclasses
CTioga2::MetaBuilder::Types::AlignedPointType, CTioga2::MetaBuilder::Types::ArrayParameter, CTioga2::MetaBuilder::Types::AxisType, CTioga2::MetaBuilder::Types::BijectionType, CTioga2::MetaBuilder::Types::BooleanParameter, CTioga2::MetaBuilder::Types::BoxType, CTioga2::MetaBuilder::Types::DataPointType, CTioga2::MetaBuilder::Types::DateTimeParameter, CTioga2::MetaBuilder::Types::DimensionType, CTioga2::MetaBuilder::Types::FloatParameter, CTioga2::MetaBuilder::Types::FloatRangeParameter, CTioga2::MetaBuilder::Types::FunctionBasedType, CTioga2::MetaBuilder::Types::IntegerParameter, CTioga2::MetaBuilder::Types::LaTeXFontBaseType, CTioga2::MetaBuilder::Types::LevelType, CTioga2::MetaBuilder::Types::LineStyleType, CTioga2::MetaBuilder::Types::ListParameter, CTioga2::MetaBuilder::Types::LocationType, CTioga2::MetaBuilder::Types::MarginsType, CTioga2::MetaBuilder::Types::MarkerType, CTioga2::MetaBuilder::Types::PartialFloatRangeType, CTioga2::MetaBuilder::Types::PointType, CTioga2::MetaBuilder::Types::REListParameter, CTioga2::MetaBuilder::Types::StringOrRegexpParameter, CTioga2::MetaBuilder::Types::StringParameter, CTioga2::MetaBuilder::Types::TiogaColorType
Constant Summary collapse
- @@types =
A hash that makes the :type value of the type argument correspond to a Type child
{ }
Instance Attribute Summary collapse
-
#passthrough ⇒ Object
If the given string matches this regular expression, it is passed through without further modification.
-
#re_shortcuts ⇒ Object
A hash Regexp -> value.
-
#shortcuts ⇒ Object
A hash shortcut -> value.
-
#type ⇒ Object
The initial type specification that was given to the Type.
Class Method Summary collapse
-
.from_string(type, string) ⇒ Object
Shortcut to convert directly a string to the given type specification.
-
.get_param_type(type) ⇒ Object
This function converts a ‘description’ (see the Type) of the type wanted into a Type child.
-
.get_type(type) ⇒ Object
Returns a Type child instance suitable for conversion of the given type specification.
-
.type_name(name, public_name = nil, default_value = nil) ⇒ Object
This class function actually registers the current type to the Type ancestor.
Instance Method Summary collapse
-
#boolean? ⇒ Boolean
Whether the type is a boolean.
-
#default_value ⇒ Object
Returns a default value for the given type.
-
#initialize(type) ⇒ Type
constructor
A default constructor.
-
#option_parser_long_option(name, param = nil) ⇒ Object
Returns a value to be fed to OptionParser#on as a ‘long’ option.
-
#option_parser_option(parser, name, desc, &block) ⇒ Object
Creates an option for the OptionParser parser.
-
#string_to_type(string) ⇒ Object
This function converts the given string to the appropriate type.
-
#type_name ⇒ Object
Returns a type name suitable for displaying, for instance, in an option parser, or inside a dialog box, and so on.
-
#type_to_string(type) ⇒ Object
This function does the exact opposite of the #string_to_type one.
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.
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/ctioga2/metabuilder/type.rb', line 111 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 if @type[:passthrough] @passthrough = @type[:passthrough] end end |
Instance Attribute Details
#passthrough ⇒ Object
If the given string matches this regular expression, it is passed through without further modification.
105 106 107 |
# File 'lib/ctioga2/metabuilder/type.rb', line 105 def passthrough @passthrough end |
#re_shortcuts ⇒ Object
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 |
#shortcuts ⇒ Object
A hash shortcut -> value. Can be nil
96 97 98 |
# File 'lib/ctioga2/metabuilder/type.rb', line 96 def shortcuts @shortcuts end |
#type ⇒ Object
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.
167 168 169 |
# File 'lib/ctioga2/metabuilder/type.rb', line 167 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
152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/ctioga2/metabuilder/type.rb', line 152 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
173 174 175 176 177 178 |
# File 'lib/ctioga2/metabuilder/type.rb', line 173 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.
135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/ctioga2/metabuilder/type.rb', line 135 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.
269 270 271 |
# File 'lib/ctioga2/metabuilder/type.rb', line 269 def boolean? return false end |
#default_value ⇒ Object
Returns a default value for the given type. This is reimplemented systematically from children, with the Type::type_name statement.
227 228 |
# File 'lib/ctioga2/metabuilder/type.rb', line 227 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.
261 262 263 264 265 |
# File 'lib/ctioga2/metabuilder/type.rb', line 261 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.
249 250 251 252 253 254 255 |
# File 'lib/ctioga2/metabuilder/type.rb', line 249 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
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/ctioga2/metabuilder/type.rb', line 187 def string_to_type(string) # First, passthrough if @passthrough && @passthrough === string return stt_run_hook(string) end # 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_name ⇒ Object
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.
235 236 237 |
# File 'lib/ctioga2/metabuilder/type.rb', line 235 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
219 220 221 |
# File 'lib/ctioga2/metabuilder/type.rb', line 219 def type_to_string(type) return type_to_string_internal(type) end |