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

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

Overview

Base class for XCCDF element objects

Constant Summary collapse

UNICODE_SYMBOLS =
{
  vertical: "\u2502",
  horizontal: "\u2500",
  tee: "\u251C",
  corner: "\u2514"
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers::XPath

find_element

Methods included from DiffableObject

#check_diffable!, #correlate_added_removed, #diff, #diff_ambiguous, #diff_array_obj, #diff_benchmark, #diff_change_result, #diff_check, #diff_complex_check, #diff_group, #diff_plain_obj, #diff_profile, #diff_rule, #diff_str_obj, #diff_value, #diff_xccdf_select, #process_details_hash!, #result_from_details_hash

Constructor Details

#initialize(*_args, parent_node: nil, **_kwargs) ⇒ ElementBase

Returns a new instance of ElementBase.



28
29
30
31
32
33
34
35
36
# File 'lib/abide_dev_utils/xccdf/parser/objects.rb', line 28

def initialize(*_args, parent_node: nil, **_kwargs)
  @parent = parent_node
  @children = []
  @links = []
  @link_labels = []
  @child_labels = []
  @label_method_values = {}
  @similarity_methods = []
end

Instance Attribute Details

#child_labelsObject (readonly)

Returns the value of attribute child_labels.



26
27
28
# File 'lib/abide_dev_utils/xccdf/parser/objects.rb', line 26

def child_labels
  @child_labels
end

#childrenObject (readonly)

Returns the value of attribute children.



26
27
28
# File 'lib/abide_dev_utils/xccdf/parser/objects.rb', line 26

def children
  @children
end

Returns the value of attribute link_labels.



26
27
28
# File 'lib/abide_dev_utils/xccdf/parser/objects.rb', line 26

def link_labels
  @link_labels
end

Returns the value of attribute links.



26
27
28
# File 'lib/abide_dev_utils/xccdf/parser/objects.rb', line 26

def links
  @links
end

#parentObject (readonly)

Returns the value of attribute parent.



26
27
28
# File 'lib/abide_dev_utils/xccdf/parser/objects.rb', line 26

def parent
  @parent
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.



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

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.



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

def self.xpath
  nil
end

Instance Method Details

#<=>(other) ⇒ Object



72
73
74
75
76
# File 'lib/abide_dev_utils/xccdf/parser/objects.rb', line 72

def <=>(other)
  return nil unless other.is_a?(self.class)

  label <=> other.label
end


102
103
104
105
106
# File 'lib/abide_dev_utils/xccdf/parser/objects.rb', line 102

def add_link(object)
  define_child_method(object.label, linked: true)
  @links << object
  @link_labels << object.label unless @link_labels.include?(object.label)
end


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

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

#all_valuesObject



63
64
65
66
# File 'lib/abide_dev_utils/xccdf/parser/objects.rb', line 63

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

#ancestorsObject



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

def ancestors
  return [] if root?

  [parent] + parent.ancestors
end

#depthObject



144
145
146
147
148
# File 'lib/abide_dev_utils/xccdf/parser/objects.rb', line 144

def depth
  return 0 if root?

  1 + parent.depth
end

#descendantsObject



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

def descendants
  return [] if leaf?

  children + children.map(&:descendants).flatten
end

#find_similarity(other) ⇒ Object



92
93
94
95
96
97
98
99
100
# File 'lib/abide_dev_utils/xccdf/parser/objects.rb', line 92

def find_similarity(other)
  return [] unless other.is_a?(self.class)

  @similarity_methods.each_with_object([]) do |method, ary|
    val = send(method)
    other_val = other.send(method)
    ary << [method, val, other_val, val.eql?(other_val)]
  end
end

#inspectObject



68
69
70
# File 'lib/abide_dev_utils/xccdf/parser/objects.rb', line 68

def inspect
  "<#{self.class}:#{object_id}:\"#{self}\">"
end

#labelObject



78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/abide_dev_utils/xccdf/parser/objects.rb', line 78

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

#leaf?Boolean

Returns:

  • (Boolean)


122
123
124
# File 'lib/abide_dev_utils/xccdf/parser/objects.rb', line 122

def leaf?
  children.empty?
end


150
151
152
# File 'lib/abide_dev_utils/xccdf/parser/objects.rb', line 150

def print_tree
  puts tree_string_parts.join("\n")
end

#rootObject



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

def root
  return self if root?

  parent.root
end

#root?Boolean

Returns:

  • (Boolean)


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

def root?
  parent.nil?
end

#siblingsObject



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

def siblings
  return [] if root?

  parent.children.reject { |child| child == self }
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’.



59
60
61
# File 'lib/abide_dev_utils/xccdf/parser/objects.rb', line 59

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