Class: Y2Network::AutoinstProfile::SectionWithAttributes

Inherits:
Object
  • Object
show all
Includes:
Yast::Logger
Defined in:
src/lib/y2network/autoinst_profile/section_with_attributes.rb

Overview

TODO:

This class is supposed to live in autoyast2 or yast2. When done, please, adapt yast2-storage-ng accordingly.

Abstract base class for some AutoYaST profile sections

Constant Summary collapse

VALUES_TO_SKIP =

Values to skip when exporting

[nil, [], ""].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent = nil) ⇒ SectionWithAttributes

Constructor


83
84
85
# File 'src/lib/y2network/autoinst_profile/section_with_attributes.rb', line 83

def initialize(parent = nil)
  @parent = parent
end

Instance Attribute Details

#parent#parent, #section_name (readonly)

This value only makes sense when new_from_hashes is used.


78
79
80
# File 'src/lib/y2network/autoinst_profile/section_with_attributes.rb', line 78

def parent
  @parent
end

Class Method Details

.attributesArray<Hash>

Description of the attributes in the section.

To be defined by each subclass. Each entry contains a hash with the mandatory key :name and an optional key :xml_name; the values are symbols.


40
41
42
# File 'src/lib/y2network/autoinst_profile/section_with_attributes.rb', line 40

def attributes
  []
end

.define_attr_accessorsObject (protected)

Macro used in the subclasses to define accessors for all the attributes defined by attributes


68
69
70
71
72
# File 'src/lib/y2network/autoinst_profile/section_with_attributes.rb', line 68

def define_attr_accessors
  attributes.each do |attribute|
    attr_accessor attribute[:name]
  end
end

.new_from_hashes(hash, parent = nil) ⇒ SectionWithAttributes

Creates an instance based on the profile representation used by the AutoYaST modules (nested arrays and hashes).

This method provides no extra validation, type conversion or initialization to default values. Those responsibilities belong to the AutoYaST modules. The hash is expected to be valid and contain the relevant information. Attributes are set to nil for missing keys and for blank values.


58
59
60
61
62
# File 'src/lib/y2network/autoinst_profile/section_with_attributes.rb', line 58

def new_from_hashes(hash, parent = nil)
  result = new(parent)
  result.init_from_hashes(hash)
  result
end

Instance Method Details

#attribute_key(attribute) ⇒ Object (protected)


147
148
149
# File 'src/lib/y2network/autoinst_profile/section_with_attributes.rb', line 147

def attribute_key(attribute)
  (attribute[:xml_name] || attribute[:name]).to_s
end

#attribute_name(key) ⇒ Object (protected)


164
165
166
167
168
# File 'src/lib/y2network/autoinst_profile/section_with_attributes.rb', line 164

def attribute_name(key)
  attribute = attributes.detect { |a| a[:xml_name] == key.to_sym || a[:name] == key.to_sym }
  return nil unless attribute
  attribute[:name]
end

#attribute_scalar(element) ⇒ Object (protected)


160
161
162
# File 'src/lib/y2network/autoinst_profile/section_with_attributes.rb', line 160

def attribute_scalar(element)
  element.respond_to?(:to_hashes) ? element.to_hashes : element
end

#attribute_skip?(value) ⇒ Boolean (protected)

Whether an attribute must be skipped during import/export.


143
144
145
# File 'src/lib/y2network/autoinst_profile/section_with_attributes.rb', line 143

def attribute_skip?(value)
  VALUES_TO_SKIP.include?(value)
end

#attribute_value(attribute) ⇒ Object (protected)


151
152
153
154
155
156
157
158
# File 'src/lib/y2network/autoinst_profile/section_with_attributes.rb', line 151

def attribute_value(attribute)
  value = public_send(attribute[:name])
  if value.is_a?(Array)
    value.map { |v| attribute_scalar(v) }
  else
    attribute_scalar(value)
  end
end

#attributesObject (protected)


133
134
135
# File 'src/lib/y2network/autoinst_profile/section_with_attributes.rb', line 133

def attributes
  self.class.attributes
end

#init_from_hashes(hash) ⇒ Object

Method used by new_from_hashes to populate the attributes.

By default, it simply assigns the non-empty hash values to the corresponding attributes, logging unknown keys. The subclass is expected to refine this behavior if needed.


94
95
96
# File 'src/lib/y2network/autoinst_profile/section_with_attributes.rb', line 94

def init_from_hashes(hash)
  init_scalars_from_hash(hash)
end

#init_scalars_from_hash(hash) ⇒ Object (protected)


170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'src/lib/y2network/autoinst_profile/section_with_attributes.rb', line 170

def init_scalars_from_hash(hash)
  hash.each_pair do |key, value|
    name = attribute_name(key)

    if name.nil?
      log.warn "Attribute #{key} not recognized by #{self.class}. Check the XML schema."
      next
    end

    # This method only reads scalar values
    next if value.is_a?(Array) || value.is_a?(Hash)

    if attribute_skip?(value)
      log.debug "Ignored blank value (#{value}) for #{key}"
      next
    end

    public_send(:"#{name}=", value)
  end
end

#section_nameString

Returns the section name

In some cases, the section name does not match with the XML name and this method should be redefined.

Examples:

section = PartitioningSection.new
section.section_name #=> "partitioning"

124
125
126
127
128
129
# File 'src/lib/y2network/autoinst_profile/section_with_attributes.rb', line 124

def section_name
  klass_name = self.class.name.split("::").last
  klass_name
    .gsub(/([a-z])([A-Z])/, "\\1_\\2").downcase
    .chomp("_section")
end

#to_hashesHash

Content of the section in the format used by the AutoYaST modules (nested arrays and hashes).


104
105
106
107
108
109
110
111
112
# File 'src/lib/y2network/autoinst_profile/section_with_attributes.rb', line 104

def to_hashes
  attributes.each_with_object({}) do |attribute, result|
    value = attribute_value(attribute)
    next if attribute_skip?(value)

    key = attribute_key(attribute)
    result[key] = value
  end
end