Class: AbideDevUtils::XCCDF::Parser::Helpers::ElementChildren::SearchChildren

Inherits:
Object
  • Object
show all
Defined in:
lib/abide_dev_utils/xccdf/parser/helpers.rb

Overview

Implements methods that allow for searching an XCCDF Element's children

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(children) ⇒ SearchChildren

Returns a new instance of SearchChildren.



17
18
19
# File 'lib/abide_dev_utils/xccdf/parser/helpers.rb', line 17

def initialize(children)
  @children = children
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



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

def children
  @children
end

Instance Method Details

#find_child_by_attribute(attribute, recurse: false) ⇒ Object



79
80
81
# File 'lib/abide_dev_utils/xccdf/parser/helpers.rb', line 79

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



94
95
96
# File 'lib/abide_dev_utils/xccdf/parser/helpers.rb', line 94

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



50
51
52
53
54
# File 'lib/abide_dev_utils/xccdf/parser/helpers.rb', line 50

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



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

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



68
69
70
71
72
73
74
75
76
77
# File 'lib/abide_dev_utils/xccdf/parser/helpers.rb', line 68

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



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

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



44
45
46
47
48
# File 'lib/abide_dev_utils/xccdf/parser/helpers.rb', line 44

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



56
57
58
59
60
# File 'lib/abide_dev_utils/xccdf/parser/helpers.rb', line 56

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



38
39
40
41
42
# File 'lib/abide_dev_utils/xccdf/parser/helpers.rb', line 38

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

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



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

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



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/abide_dev_utils/xccdf/parser/helpers.rb', line 21

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