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

Inherits:
Object
  • Object
show all
Includes:
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 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.



17
18
19
20
21
22
23
24
# File 'lib/abide_dev_utils/xccdf/parser/objects.rb', line 17

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



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

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]
  else
    super
  end
end

Instance Attribute Details

#child_labelsObject (readonly)

Returns the value of attribute child_labels.



15
16
17
# File 'lib/abide_dev_utils/xccdf/parser/objects.rb', line 15

def child_labels
  @child_labels
end

#childrenObject (readonly)

Returns the value of attribute children.



15
16
17
# File 'lib/abide_dev_utils/xccdf/parser/objects.rb', line 15

def children
  @children
end

Returns the value of attribute link_labels.



15
16
17
# File 'lib/abide_dev_utils/xccdf/parser/objects.rb', line 15

def link_labels
  @link_labels
end

Class Method Details

.xpathObject

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



30
31
32
# File 'lib/abide_dev_utils/xccdf/parser/objects.rb', line 30

def self.xpath
  nil
end

Instance Method Details



173
174
175
176
# File 'lib/abide_dev_utils/xccdf/parser/objects.rb', line 173

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


178
179
180
# File 'lib/abide_dev_utils/xccdf/parser/objects.rb', line 178

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

#all_valuesObject



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

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

#find_child_by_attribute(attribute, recurse: false) ⇒ Object



154
155
156
# File 'lib/abide_dev_utils/xccdf/parser/objects.rb', line 154

def find_child_by_attribute(attribute, recurse: false)
  find_children_by_attribute(attribute, recurse: recurse).first
end

#find_child_by_attribute_value(attribute, value, recurse: false) ⇒ Object



169
170
171
# File 'lib/abide_dev_utils/xccdf/parser/objects.rb', line 169

def find_child_by_attribute_value(attribute, value, recurse: false)
  find_children_by_attribute_value(attribute, value, recurse: recurse).first
end

#find_child_by_class(klass, recurse: false) ⇒ Object



125
126
127
128
129
# File 'lib/abide_dev_utils/xccdf/parser/objects.rb', line 125

def find_child_by_class(klass, recurse: false)
  return recursive_find_child { |child| child.is_a?(klass) } if recurse

  find_children_by_class(klass).first
end

#find_child_by_xpath(xpath, recurse: false) ⇒ Object



137
138
139
140
141
# File 'lib/abide_dev_utils/xccdf/parser/objects.rb', line 137

def find_child_by_xpath(xpath, recurse: false)
  return recursive_find_child { |child| child.xpath == xpath } if recurse

  find_children_by_xpath(xpath).first
end

#find_children_by_attribute(attribute, recurse: false) ⇒ Object



143
144
145
146
147
148
149
150
151
152
# File 'lib/abide_dev_utils/xccdf/parser/objects.rb', line 143

def find_children_by_attribute(attribute, recurse: false)
  pr = proc do |child|
    next unless child.instance_of?(AbideDevUtils::XCCDF::Parser::Objects::AttributeValue)

    child.attribute == attribute
  end
  return recursive_select_children(&pr) if recurse

  children.select(&pr)
end

#find_children_by_attribute_value(attribute, value, recurse: false) ⇒ Object



158
159
160
161
162
163
164
165
166
167
# File 'lib/abide_dev_utils/xccdf/parser/objects.rb', line 158

def find_children_by_attribute_value(attribute, value, recurse: false)
  pr = proc do |child|
    next unless child.instance_of?(AbideDevUtils::XCCDF::Parser::Objects::AttributeValue)

    child.attribute == attribute && child.value == value
  end
  return recursive_select_children(&pr) if recurse

  children.select(&pr)
end

#find_children_by_class(klass, recurse: false) ⇒ Object



119
120
121
122
123
# File 'lib/abide_dev_utils/xccdf/parser/objects.rb', line 119

def find_children_by_class(klass, recurse: false)
  return recursive_select_children { |child| child.instance_of?(klass) } if recurse

  children.select { |child| child.instance_of?(klass) }
end

#find_children_by_xpath(xpath, recurse: false) ⇒ Object



131
132
133
134
135
# File 'lib/abide_dev_utils/xccdf/parser/objects.rb', line 131

def find_children_by_xpath(xpath, recurse: false)
  return recursive_select_children { |child| child.xpath == xpath } if recurse

  children.select { |child| child.xpath == xpath }
end

#find_children_that_respond_to(method, recurse: false) ⇒ Object



113
114
115
116
117
# File 'lib/abide_dev_utils/xccdf/parser/objects.rb', line 113

def find_children_that_respond_to(method, recurse: false)
  return recursive_select_children { |child| child.respond_to?(method) } if recurse

  children.select { |c| c.respond_to?(method.to_sym) }
end

#labelObject



82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/abide_dev_utils/xccdf/parser/objects.rb', line 82

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

#recursive_find_child(children_to_search = children, &block) ⇒ Object



109
110
111
# File 'lib/abide_dev_utils/xccdf/parser/objects.rb', line 109

def recursive_find_child(children_to_search = children, &block)
  rescursive_select_children(children_to_search, &block).first
end

#recursive_select_children(children_to_search = children, &block) ⇒ Object



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

def recursive_select_children(children_to_search = children, &block)
  search_hits = []
  children_to_search.each do |child|
    found = yield child
    if found
      search_hits << child
    elsif child.respond_to?(:children)
      search_hits << recursive_select_children(child.children, &block)
    end
  end
  search_hits.flatten.compact.uniq
end

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

Returns:

  • (Boolean)


74
75
76
77
78
79
80
# File 'lib/abide_dev_utils/xccdf/parser/objects.rb', line 74

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



38
39
40
# File 'lib/abide_dev_utils/xccdf/parser/objects.rb', line 38

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