Class: CvssCli::Metric

Inherits:
Object
  • Object
show all
Defined in:
lib/cvss_cli/metric.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, name, desc, values) ⇒ Metric

Returns a new instance of Metric.



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/cvss_cli/metric.rb', line 8

def initialize(key, name, desc, values)
  @key = key
  @name = name
  @desc = desc
  @value = nil # todo or add default value?
  @values = Hash.new
  values.each do |key, value|
    @values[key] = Value.new(key, value['name'], value['desc'], value['numerical'], value['numerical_changed'])
    @value = key if key == 'x' # set default value to Not Defined (X) if the metric has such a value
  end
end

Instance Attribute Details

#descObject (readonly)

Returns the value of attribute desc.



6
7
8
# File 'lib/cvss_cli/metric.rb', line 6

def desc
  @desc
end

#keyObject (readonly)

Returns the value of attribute key.



6
7
8
# File 'lib/cvss_cli/metric.rb', line 6

def key
  @key
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/cvss_cli/metric.rb', line 6

def name
  @name
end

#valueObject (readonly)

Returns the value of attribute value.



6
7
8
# File 'lib/cvss_cli/metric.rb', line 6

def value
  @value
end

#valuesObject (readonly)

Returns the value of attribute values.



6
7
8
# File 'lib/cvss_cli/metric.rb', line 6

def values
  @values
end

Instance Method Details

#clear_valueObject



44
45
46
47
48
49
50
# File 'lib/cvss_cli/metric.rb', line 44

def clear_value
  if @values.values.map { |v| v.key }.include? 'x'
    @value = 'x'
  else
    @value = nil
  end
end

#get_numerical_changed_valueObject



39
40
41
42
# File 'lib/cvss_cli/metric.rb', line 39

def get_numerical_changed_value
  return nil if @value.nil?
  @values[@value].numerical_changed
end

#get_numerical_valueObject



34
35
36
37
# File 'lib/cvss_cli/metric.rb', line 34

def get_numerical_value
  return nil if @value.nil?
  @values[@value].numerical
end

#get_valueObject



29
30
31
32
# File 'lib/cvss_cli/metric.rb', line 29

def get_value
  return nil if @value.nil?
  @values[@value]
end

#set_value(value) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/cvss_cli/metric.rb', line 20

def set_value(value)
  if @values.keys.include? value.downcase
    #puts "Setting #{@name}: #{value.upcase}"
    @value = value.downcase
  else
    puts "Value #{value.upcase} not valid for metric #{@name}. Valid values are: #{(@values.values.collect { |v| v.key.upcase }).join(', ')}".red
  end
end