Class: MetaBuilder::Parameter
- Inherits:
-
Object
- Object
- MetaBuilder::Parameter
- Extended by:
- Forwardable
- Defined in:
- lib/MetaBuilder/parameter.rb,
lib/MetaBuilder/Qt4/parameter.rb
Overview
A parameter describes a way of storing some information into an instance of an object. No type checking is done on the target object when the actual reading/writing is done. However, the type checking is done upstream by the Description system.
A Parameter consists of several things:
-
a #name, to identify it in a unique fashion;
-
a #type, used to convert to and from String and for user interaction in general;
-
some explanative text, used to inform the user: #long_name and #description
-
two symbols that are used to gain read and write access of the parameter on the target object.
The Parameter class can be used to maintain a set of meta-informations about types in a given object.
Instance Attribute Summary collapse
-
#description ⇒ Object
The (text) description of the parameter.
-
#long_name ⇒ Object
The long name of the parameter, to be translated.
-
#name ⇒ Object
The short name of the parameter.
-
#param_type ⇒ Object
The actual ParameterType subclass instance in charge of handling the conversion from string.
-
#reader_symbol ⇒ Object
The function names that should be used to set the symbol and retrieve it’s current value.
-
#type_spec ⇒ Object
The way the type was specified to the system.
-
#writer_symbol ⇒ Object
The function names that should be used to set the symbol and retrieve it’s current value.
Instance Method Summary collapse
-
#get(target) ⇒ Object
Aquires the value from the backend, and returns it in the form of a string.
-
#get_raw(target) ⇒ Object
Aquires the value from the backend, and returns it.
-
#initialize(name, writer_symbol, reader_symbol, long_name, type, description, attributes = {}) ⇒ Parameter
constructor
Creates a new Parameter with the given symbols.
-
#option_parser_option(parser, name, instance) ⇒ Object
Creates an option in the OptionParser parser, with the name name, that will change the settings of the object instance.
-
#set(target, str) ⇒ Object
Uses the #writer_symbol of the target to set the value of the parameter to the one converted from the String str.
-
#set_raw(target, val) ⇒ Object
Sets directly the target parameter, without type conversion.
Constructor Details
#initialize(name, writer_symbol, reader_symbol, long_name, type, description, attributes = {}) ⇒ Parameter
Creates a new Parameter with the given symbols. Remember that if you don’t intend to use #get, #get_raw, #set and #set_raw, you don’t need to pass meaningful values to writer_symbol and reader_symbol.
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 |
# File 'lib/MetaBuilder/parameter.rb', line 307 def initialize(name, writer_symbol, reader_symbol, long_name, type, description, attributes = {}) @name = name @writer_symbol = writer_symbol @reader_symbol = reader_symbol @description = description @long_name = long_name @type_spec = type type_class = ParameterType.get_param_type(@type_spec) raise "Parameter type not recognized: #{@type_spec}" unless type_class @param_type = type_class.new(@type_spec) @attributes = attributes end |
Instance Attribute Details
#description ⇒ Object
The (text) description of the parameter
282 283 284 |
# File 'lib/MetaBuilder/parameter.rb', line 282 def description @description end |
#long_name ⇒ Object
The long name of the parameter, to be translated
274 275 276 |
# File 'lib/MetaBuilder/parameter.rb', line 274 def long_name @long_name end |
#name ⇒ Object
The short name of the parameter
271 272 273 |
# File 'lib/MetaBuilder/parameter.rb', line 271 def name @name end |
#param_type ⇒ Object
The actual ParameterType subclass instance in charge of handling the conversion from string.
289 290 291 |
# File 'lib/MetaBuilder/parameter.rb', line 289 def param_type @param_type end |
#reader_symbol ⇒ Object
The function names that should be used to set the symbol and retrieve it’s current value. The corresponding functions should read or return a string, and writer(reader) should be a noop.
279 280 281 |
# File 'lib/MetaBuilder/parameter.rb', line 279 def reader_symbol @reader_symbol end |
#type_spec ⇒ Object
The way the type was specified to the system
285 286 287 |
# File 'lib/MetaBuilder/parameter.rb', line 285 def type_spec @type_spec end |
#writer_symbol ⇒ Object
The function names that should be used to set the symbol and retrieve it’s current value. The corresponding functions should read or return a string, and writer(reader) should be a noop.
279 280 281 |
# File 'lib/MetaBuilder/parameter.rb', line 279 def writer_symbol @writer_symbol end |
Instance Method Details
#get(target) ⇒ Object
Aquires the value from the backend, and returns it in the form of a string
344 345 346 |
# File 'lib/MetaBuilder/parameter.rb', line 344 def get(target) return type_to_string(get_raw(target)) end |
#get_raw(target) ⇒ Object
Aquires the value from the backend, and returns it.
349 350 351 |
# File 'lib/MetaBuilder/parameter.rb', line 349 def get_raw(target) target.send(@reader_symbol) end |
#option_parser_option(parser, name, instance) ⇒ Object
Creates an option in the OptionParser parser, with the name name, that will change the settings of the object instance
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 |
# File 'lib/MetaBuilder/parameter.rb', line 355 def option_parser_option(parser, name, instance) obj = instance # copy needed to avoid problems with # overwritten local variables block = proc do |value| set_raw(obj, value) end # The use of #attributes is already deprecated. Don't use !!! # if attributes.has_key?(:option_parser_short) # @param_type.option_parser_raw(parser, attributes[:option_parser_short], # @param_type.option_parser_long_option(name), # description, &block) # else @param_type.option_parser_option(parser, name, description, &block) # end end |
#set(target, str) ⇒ Object
Uses the #writer_symbol of the target to set the value of the parameter to the one converted from the String str
337 338 339 |
# File 'lib/MetaBuilder/parameter.rb', line 337 def set(target, str) set_raw(target, string_to_type(str)) end |
#set_raw(target, val) ⇒ Object
Sets directly the target parameter, without type conversion
331 332 333 |
# File 'lib/MetaBuilder/parameter.rb', line 331 def set_raw(target, val) target.send(@writer_symbol, val) end |