Class: CTioga2::Graphics::Styles::CurveStyleFactory

Inherits:
Object
  • Object
show all
Includes:
Log
Defined in:
lib/ctioga2/graphics/styles/factory.rb

Overview

This object is in charge of the generation of the CurveStyle object for the next curve to be drawn.

Defined Under Namespace

Classes: CurveStyleFactoryParameter

Constant Summary collapse

AutoRE =

Switch some parameter back to automatic

/auto/i
DisableRE =

Sets some parameter to false.

/no(ne)?|off/i
CurveStyleGroup =

The CmdGroup for stylistic information about curves.

CmdGroup.new('curve-style', "Curve styles", 
"Set stylistic details about curves", 1)
PlotCommandOptions =

A constant suitable for use as the optional arguments of the plot command.

plot_optional_arguments

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Log

debug, error, fatal, #format_exception, #identify, info, init_logger, logger, set_level, #spawn, warn

Constructor Details

#initializeCurveStyleFactory

Creates a new CurveStyleFactory.



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/ctioga2/graphics/styles/factory.rb', line 221

def initialize
  # Overrides as in the first ctioga
  @override_parameters = {
    'line_style' => LineStyles::Solid,
    'marker_marker' => false,
    'marker_scale' => 0.5 
  }
  @parameters_carrays = {}
  for target, param in self.class.parameters
    set = param.default_set
    if set
      @parameters_carrays[target] = CircularArray.new(set)
    end
  end
end

Instance Attribute Details

#override_parametersObject

A hash containing values that override default ones derived from the CircularArray objects.



214
215
216
# File 'lib/ctioga2/graphics/styles/factory.rb', line 214

def override_parameters
  @override_parameters
end

#parameter_carraysObject

A hash of CircularArray objects.



217
218
219
# File 'lib/ctioga2/graphics/styles/factory.rb', line 217

def parameter_carrays
  @parameter_carrays
end

Class Method Details

.create_commandsObject

Creates two commands for each parameter of the object:

  • a command to set the override

  • a command to choose the sets.



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/ctioga2/graphics/styles/factory.rb', line 161

def self.create_commands
  parameters.each do |target, param|
    override_cmd = 
      Cmd.new("#{param.name}",
              param.short_option,
              "--#{param.name}", 
              [
               CmdArg.new("#{param.type.name}-or-auto") 
              ], {},
              "Sets the #{param.description} for subsequent curves",
              "Sets the #{param.description} for subsequent curves, until cancelled with 'auto' as argument.", CurveStyleGroup) do |plotmaker, value|
      plotmaker.curve_generator.style_factory.
        set_parameter_override(target, value)
    end

    if param.sets
      set_cmd = 
        Cmd.new("#{param.name}-set",
                nil,
                "--#{param.name}-set", 
                [
                 CmdArg.new("#{param.type.name}-set")
                ], {},
                "Chooses a set for the #{param.description} of subsequent curves",
                "Chooses a set for the #{param.description} of subsequent curves", 
                CurveStyleGroup) do |plotmaker, value|
        plotmaker.curve_generator.style_factory.
          set_parameter_set(target, value)
      end
    end
  end
end

.define_parameter(target, name, type, sets, description, short_option = nil) ⇒ Object

Creates a new parameter for the style factory.



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/ctioga2/graphics/styles/factory.rb', line 101

def self.define_parameter(target, name, type, sets, description, 
                          short_option = nil)
  # We define two new types:
  # - first, the color-or-auto type:
  base_type = Commands::CommandType.get_type(type)

  if ! Commands::Interpreter.type("#{base_type.name}-or-auto")
    mb_type = base_type.type.dup
    mb_type.re_shortcuts = (mb_type.re_shortcuts ? 
                                mb_type.re_shortcuts.dup : {}) 
    
    mb_type.re_shortcuts[AutoRE] = 'auto'
    mb_type.re_shortcuts[DisableRE] = false

    # Now, register a type for the type or automatic.
    CmdType.new("#{base_type.name}-or-auto", mb_type,
                "Same thing as type #{base_type.name}, or 'auto'")

  end

  if sets and ! Commands::Interpreter.type("#{base_type.name}-set")
    # Now, register a type for the type or automatic.
    CmdType.new("#{base_type.name}-set",{
                  :type => :set,
                  :subtype => base_type.type,
                  :shortcuts => sets
                } ,
                "Sets of #{base_type.name}")
  end
  param = 
    CurveStyleFactoryParameter.new(name, type, sets, 
                                   description, short_option)
  @parameters ||= {}
  @parameters[target] = param

  @name_to_target ||= {}
  @name_to_target[name] = target
end

.name_to_targetObject

Returns the Hash containing the class parameters.



147
148
149
# File 'lib/ctioga2/graphics/styles/factory.rb', line 147

def self.name_to_target
  return @name_to_target
end

.parametersObject

Returns the Hash containing the class parameters.



141
142
143
# File 'lib/ctioga2/graphics/styles/factory.rb', line 141

def self.parameters
  return @parameters || {}
end

.plot_optional_argumentsObject

This function returns a hash suitable for use with the plot command as optional arguments, that will end up as the one_time hash in #next.



197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/ctioga2/graphics/styles/factory.rb', line 197

def self.plot_optional_arguments
  args = {}
  for option_name, param in @parameters
    args[param.name] = 
      CmdArg.new(param.type)
  end

  # Here, we add the support for a /legend= option
  args['legend'] = CmdArg.new('text')
  @name_to_target['legend'] = 'legend'

  return args
end

Instance Method Details

#next(one_time = {}) ⇒ Object

Gets the style for the next curve. The one_time hash contains values ‘parameter name’ (name, and not target) => value that are used for this time only.



240
241
242
243
244
245
246
247
248
249
# File 'lib/ctioga2/graphics/styles/factory.rb', line 240

def next(one_time = {})
  base = {}
  for target, array in @parameters_carrays
    base[target] = array.next
  end
  base.merge!(@override_parameters)
  base.merge!(hash_name_to_target(one_time))
  # TODO: here, resolve 'links', such as :->color ?
  return CurveStyle.from_hash(base)
end

#set_parameter_override(target, value) ⇒ Object

Sets the override for the given parameter. This corresponds to fixing manually the corresponding element until the override is removed, by a call with a value that matches AutoRE.

The value should ideally be a String that is further converted to the appropriate type. Non-string objects will be left untouched.



261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/ctioga2/graphics/styles/factory.rb', line 261

def set_parameter_override(target, value)
  param = get_parameter(target)
  # Perform automatic type conversion only on strings.
  if value.is_a? String 
    if value =~ AutoRE
      @override_parameters.delete(target)
      return
    end
    if value =~ DisableRE
      value = false
    else
      value = param.type.string_to_type(value)
    end
  end

  @override_parameters[target] = value
end

#set_parameter_set(target, value) ⇒ Object

Sets the CircularArray set corresponding to the named



280
281
282
283
284
285
286
287
# File 'lib/ctioga2/graphics/styles/factory.rb', line 280

def set_parameter_set(target, value)
  param = get_parameter(target)
  # Perform automatic type conversion only on strings.
  if value.is_a? String 
    value = param.sets_type.string_to_type(value)
  end
  @parameters_carrays[target].set = value
end