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.



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

def initialize
  self.categories= Hash.new { |hash, key| hash[key] = [] }
  self.settings= Setting.new()
  self.next_category_number = 0
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

#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



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

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

    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



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

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



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

def category_by_name(name)
  cat = self.categories.keys.select(){|c| c.name.eql?(name)}.first
  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.



78
79
80
# File 'lib/setup_configuration/setup_config.rb', line 78

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.



86
87
88
# File 'lib/setup_configuration/setup_config.rb', line 86

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

#find_param_by_number(number) ⇒ Object

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



94
95
96
# File 'lib/setup_configuration/setup_config.rb', line 94

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

#next_category_number!Object



58
59
60
61
62
# File 'lib/setup_configuration/setup_config.rb', line 58

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

#parametersObject

Gets all known parameters.



69
70
71
72
# File 'lib/setup_configuration/setup_config.rb', line 69

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



64
65
66
# File 'lib/setup_configuration/setup_config.rb', line 64

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

#validate_paramsObject

Validates the uniqueness of parameter keys and numbers.



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

def validate_params()

  categories.each() do |key, value|
    throw RuntimeError.new("ERROR: category '#{key}' contains more than 150 parameters. Reduce parameter count.") if value.size >150
  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