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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent = nil) ⇒ SectionWithAttributes

Constructor

Parameters:


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.

Returns:


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.

Returns:

  • (Array<Hash>)

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

def attributes
  []
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.

Parameters:

  • hash (Hash)

    content of the corresponding section of the profile. Each element of the hash corresponds to one of the attributes defined in the section.

  • parent (#parent, #section_name) (defaults to: nil)

    parent section

Returns:


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

#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.

Parameters:


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

#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"

Returns:

  • (String)

    Section name


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).

Returns:

  • (Hash)

    each element of the hash corresponds to one of the attributes defined in the section. Blank attributes are not included.


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