Class: SetupConfiguration::Suite

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/setup_configuration/setup_config.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSuite

Returns a new instance of Suite.



24
25
26
27
28
29
# File 'lib/setup_configuration/setup_config.rb', line 24

def initialize
  self.categories= Hash.new { |hash, key| hash[key] = [] }
  self.settings= Setting.new()
  self.next_category_number = 0
  @maximum_numbers_per_category = 150
end

Instance Attribute Details

#abbreviationObject

Returns the value of attribute abbreviation.



20
21
22
# File 'lib/setup_configuration/setup_config.rb', line 20

def abbreviation
  @abbreviation
end

#categoriesObject

Returns the value of attribute categories.



17
18
19
# File 'lib/setup_configuration/setup_config.rb', line 17

def categories
  @categories
end

#maximum_numbers_per_categoryObject (readonly)

Returns the value of attribute maximum_numbers_per_category.



22
23
24
# File 'lib/setup_configuration/setup_config.rb', line 22

def maximum_numbers_per_category
  @maximum_numbers_per_category
end

#nameObject

Returns the value of attribute name.



19
20
21
# File 'lib/setup_configuration/setup_config.rb', line 19

def name
  @name
end

#next_category_numberObject

Returns the value of attribute next_category_number.



21
22
23
# File 'lib/setup_configuration/setup_config.rb', line 21

def next_category_number
  @next_category_number
end

#settingsObject

Returns the value of attribute settings.



18
19
20
# File 'lib/setup_configuration/setup_config.rb', line 18

def settings
  @settings
end

Instance Method Details

#assign_param_ref(ref) ⇒ Object

validate_params



141
142
143
144
145
146
147
148
149
# File 'lib/setup_configuration/setup_config.rb', line 141

def assign_param_ref(ref)
    param = self.parameters().detect(){|p| p.key.eql?(ref.key) && p.param?}

    if param
      ref.assign(param)
    else
      raise RuntimeError.new("ERROR: reference to unknown parameter with key '#{ref.key}'")
    end
end

#category(category, &category_params) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/setup_configuration/setup_config.rb', line 31

def category(category, &category_params)
  if category_params

    #this code calls instance_eval and delivers the context object
    parameter_factory = ParameterFactory.new()
    parameter_factory.instance_eval(&category_params)
    cat = category_by_name(category)
    categories[cat] << parameter_factory.params()

    # this .instance_eval call returns the last value of the last executed code (an array from method param in Parameters)
    #categories[category] << SetupConfiguration::Parameters.new().instance_eval(&category_params)

    # flatten is needed: Parameters#param returns an array which is inserted in an array...
    categories[cat].flatten!
  else
    $stderr.puts "WARNING: Empty category '#{category}' will be ignored. "
  end
end

#category_by_name(name) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/setup_configuration/setup_config.rb', line 50

def category_by_name(name)
  cat = self.categories.keys.detect(){|c| c.name.eql?(name)}
  unless cat
    cat = Category.new
    cat.number = self.next_category_number!
    cat.name = name
  end
  cat
end

#find_param(key) ⇒ Object

Finds a Parameter with the given key. If there is no such parameter the method returns nil.



80
81
82
# File 'lib/setup_configuration/setup_config.rb', line 80

def find_param(key)
  find_param_by_key(key)
end

#find_param_by_key(key) ⇒ Object

Finds a Parameter with the given key. If there is no such parameter the method returns nil.



88
89
90
# File 'lib/setup_configuration/setup_config.rb', line 88

def find_param_by_key(key)
  self.parameters().detect(){|p| p.key.eql?(key)}
end

#find_param_by_number(number) ⇒ Object

Finds a Parameter with the given number. If there is no such parameter the method returns nil.



96
97
98
# File 'lib/setup_configuration/setup_config.rb', line 96

def find_param_by_number(number)
  self.parameters().detect(){|p| p.number.eql?(number)}
end

#next_category_number!Object



60
61
62
63
64
# File 'lib/setup_configuration/setup_config.rb', line 60

def next_category_number!
  number = next_category_number
  self.next_category_number+=1
 number
end

#parametersObject

Gets all known parameters.



71
72
73
74
# File 'lib/setup_configuration/setup_config.rb', line 71

def parameters
  # cache parameters so sorting is necessary only once - this saves a lot of time...
  @parameters ||= categories.values.flatten.sort
end

#setting(&setting_params) ⇒ Object



66
67
68
# File 'lib/setup_configuration/setup_config.rb', line 66

def setting(&setting_params)
  settings.instance_eval(&setting_params) if setting_params
end

#validate_paramsObject

Validates the uniqueness of parameter keys and numbers.



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
139
# File 'lib/setup_configuration/setup_config.rb', line 103

def validate_params

  categories.each() do |key, value|
    throw RuntimeError.new("ERROR: category '#{key}' contains more than #{maximum_numbers_per_category} parameters. Reduce parameter count.") if value.size >maximum_numbers_per_category
  end

  keys=[]
  numbers=[]
  # slicer contains parameter with number 0... therefore valid parameter numbers starts at 0
  valid_param_numbers=SetupConfiguration.parameter_range()

  self.parameters().each() do |p|

    $stderr.puts "WARNING: parameter number 404 is reserved for machine type. you are using it for '#{p.key}'." if p.number.eql?(404)
    throw RuntimeError.new("ERROR: parameter number '#{p.number}' not supported. Number must be in range #{valid_param_numbers}.") unless valid_param_numbers.member?(p.number)

    if p.param?
      if keys.include? p.key
        raise RuntimeError.new("ERROR: parameter key '#{p.key}' defined more than once")
      else
        keys << p.key
      end


      if numbers.include? p.number
        raise RuntimeError.new("ERROR: parameter number '#{p.number}' defined more than once")
      else
        numbers << p.number
      end
    else
      assign_param_ref(p)
    end#p.param?

  end
  #force fresh sort of parameters
  @parameters = nil
end