/
invalid_value.rb
115 lines (104 loc) · 3.9 KB
/
invalid_value.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# encoding: utf-8
# Copyright (c) [2017] SUSE LLC
#
# All Rights Reserved.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of version 2 of the GNU General Public License as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, contact SUSE LLC.
#
# To contact SUSE LLC about this file by physical or electronic mail, you may
# find current contact information at www.suse.com.
require "y2storage/autoinst_issues/issue"
module Y2Storage
module AutoinstIssues
# Represents an AutoYaST situation where an invalid value was given.
#
# @example Invalid value 'auto' for attribute :size on /home partition
# section = AutoinstProfile::PartitioningSection.new_from_hashes({"size" => "auto"})
# problem = InvalidValue.new(section, :size)
# problem.value #=> "auto"
# problem.attr #=> :size
class InvalidValue < Issue
# @return [Symbol] Name of the missing attribute
attr_reader :attr
# @return [Integer,String,Symbol] New value or :skip to skip the section
attr_reader :new_value
# @return [Array,Range] Expected values
attr_reader :expected_values
# @param section [#parent,#section_name] Section where it was detected
# (see {AutoinstProfile})
# @param attr [Symbol] Name of the invalid attribute
# @param new_value [Integer,String,Symbol] New value or :skip to skip the section
# @param expected_values [Array,Range,nil] Expected values
attr_reader :expected_values
def initialize(section, attr, new_value = :skip, expected_values = nil)
@section = section
@attr = attr
@new_value = new_value
@expected_values = expected_values
end
# Return the invalid value
#
# @return [Integer,String,Symbol] Invalid value
def value
section.public_send(attr)
end
# Return problem severity
#
# @return [Symbol] :warn
def severity
:warn
end
# Return the error message to be displayed
#
# @return [String] Error message
# @see Issue#message
def message
# TRANSLATORS: 'value' is a generic value (number or string) 'attr' is an AutoYaST element
# name.
message = _("Invalid value '%{value}' for attribute '%{attr}'. ") %
{ value: value, attr: attr }
message += expected_values_message if expected_values
message + new_value_message
end
private
# Return a messsage explaining what should be done with the value.
def new_value_message
if new_value == :skip
# TRANSLATORS: it refers to an AutoYaST profile section
_("The section will be skipped.")
else
# TRANSLATORS: 'value' is the value for an AutoYaST element (a number or a string)
_("Replaced by '%{value}'.") % { value: human_value(new_value) }
end
end
# Return a messsage explaining expected values
def expected_values_message
case expected_values
when Range
_("Expected a value between '%{min}' and '%{max}'. ") %
{ min: human_value(expected_values.first), max: human_value(expected_values.last) }
when Array
_("Expected one of these values: %{values}. ") %
{ values: expected_values.join(", ") }
end
end
def human_value(value)
if value.respond_to?(:to_human_string)
value.to_human_string
else
value.to_s
end
end
end
end
end