Class: AbideDevUtils::XCCDF::Parser::Objects::ElementBase

Inherits:
Object
  • Object
show all
Extended by:
Helpers::XPath
Includes:
Helpers::ElementChildren, Helpers::XPath, DigestObject
Defined in:
lib/abide_dev_utils/xccdf/parser/objects.rb

Overview

Base class for XCCDF element objects

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers::XPath

find_element

Methods included from Helpers::ElementChildren

#search_children

Methods included from DigestObject

#digest, #digest_equal?, #digest_similarity, #digestable_instance_variables, #exclude_from_digest, #labeled_self_digest, #non_compatible?, #normalize_exclusion, #sorted_digest_parts, #split_labeled_digest

Constructor Details

#initialize(*_args, **_kwargs) ⇒ ElementBase

Returns a new instance of ElementBase.



21
22
23
24
25
26
27
28
# File 'lib/abide_dev_utils/xccdf/parser/objects.rb', line 21

def initialize(*_args, **_kwargs)
  @children = []
  @links = []
  @link_labels = []
  @child_labels = []
  @label_method_values = {}
  exclude_from_digest(%i[@digest @children @child_labels @label @exclude_from_digest @label_method_values])
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object

Allows access to child objects by label



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
# File 'lib/abide_dev_utils/xccdf/parser/objects.rb', line 61

def method_missing(method_name, *args, &block)
  m_name_string = method_name.to_s.downcase
  return @label_method_values[m_name_string] if @label_method_values.key?(m_name_string)

  label_str = m_name_string.start_with?('linked_') ? m_name_string.split('_')[1..].join('_') : m_name_string
  if m_name_string.start_with?('linked_') && @link_labels.include?(label_str)
    found = @links.select { |link| link.label == label_str }
    @label_method_values["linked_#{label_str}"] = if found.length == 1
                                                    found.first
                                                  else
                                                    found
                                                  end
    @label_method_values["linked_#{label_str}"]
  elsif @child_labels.include?(label_str)
    found = @children.select { |child| child.label == label_str }
    @label_method_values[label_str] = if found.length == 1
                                        found.first
                                      else
                                        found
                                      end
    @label_method_values[label_str]
  elsif search_children.respond_to?(method_name)
    search_children.send(method_name, *args, &block)
  else
    super
  end
end

Instance Attribute Details

#child_labelsObject (readonly)

Returns the value of attribute child_labels.



19
20
21
# File 'lib/abide_dev_utils/xccdf/parser/objects.rb', line 19

def child_labels
  @child_labels
end

#childrenObject (readonly)

Returns the value of attribute children.



19
20
21
# File 'lib/abide_dev_utils/xccdf/parser/objects.rb', line 19

def children
  @children
end

Returns the value of attribute link_labels.



19
20
21
# File 'lib/abide_dev_utils/xccdf/parser/objects.rb', line 19

def link_labels
  @link_labels
end

Class Method Details

.xmlnsObject

For subclasses that are associated with a specific XCCDF element that has valid namespace prefix, this method returns that namespaces. May be overridden by subclasses if they have a different valid namespace prefix.



43
44
45
# File 'lib/abide_dev_utils/xccdf/parser/objects.rb', line 43

def self.xmlns
  'xccdf'
end

.xpathObject

For subclasses that are associated with a specific XCCDF element, this method returns the element's xpath name. Must be overridden by subclasses that implement this method.



34
35
36
# File 'lib/abide_dev_utils/xccdf/parser/objects.rb', line 34

def self.xpath
  nil
end

Instance Method Details



111
112
113
114
# File 'lib/abide_dev_utils/xccdf/parser/objects.rb', line 111

def add_link(object)
  @links << object
  @link_labels << object.label unless @link_labels.include?(object.label)
end


116
117
118
# File 'lib/abide_dev_utils/xccdf/parser/objects.rb', line 116

def add_links(objects)
  objects.each { |object| add_object_as_child(object) }
end

#all_valuesObject



55
56
57
58
# File 'lib/abide_dev_utils/xccdf/parser/objects.rb', line 55

def all_values
  @child_labels.map { |label| send(label.to_sym) }
  @label_method_values
end

#labelObject



97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/abide_dev_utils/xccdf/parser/objects.rb', line 97

def label
  return @label if defined?(@label)

  @label = case self.class.name
           when 'AbideDevUtils::XCCDF::Parser::Objects::AttributeValue'
             @attribute.to_s
           when /AbideDevUtils::XCCDF::Parser::Objects::(ShortText|LongText)/
             'text'
           else
             self.class.name.split('::').last.split(/(?=[A-Z])/).join('_').downcase
           end
  @label
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


89
90
91
92
93
94
95
# File 'lib/abide_dev_utils/xccdf/parser/objects.rb', line 89

def respond_to_missing?(method_name, include_private = false)
  m_name_string = method_name.to_s.downcase
  label_str = m_name_string.start_with?('linked_') ? m_name_string.split('_')[1..].join('_') : m_name_string
  (m_name_string.start_with?('linked_') && @link_labels.include?(label_str)) ||
    @child_labels.include?(label_str) ||
    super
end

#xccdf_typeObject

Takes the last segment of the class name, splits on captial letters, and returns a downcased string joined by dashes. This gives us the XCCDF element type. Example: 'AbideDevUtils::XCCDF::Parser::Objects::ComplexCheck' returns 'complex-check'.



51
52
53
# File 'lib/abide_dev_utils/xccdf/parser/objects.rb', line 51

def xccdf_type
  self.class.name.split('::').last.split(/(?=[A-Z])/).reject { |x| x == 'Xccdf' }.join('-').downcase
end