Module: SvgConform::NodeHelpers

Included in:
Remediations::BaseRemediation, Requirements::BaseRequirement
Defined in:
lib/svg_conform/node_helpers.rb

Overview

Shared helper methods for working with XML nodes

This module provides common utilities used by both requirements and remediations to avoid code duplication and ensure consistency.

Instance Method Summary collapse

Instance Method Details

#element?(node) ⇒ Boolean

Check if a node is an element

Parameters:

  • node (Object)

    the node to check

Returns:

  • (Boolean)

    true if the node is an element



12
13
14
# File 'lib/svg_conform/node_helpers.rb', line 12

def element?(node)
  node.respond_to?(:name) && !node.name.nil?
end

#get_attribute(node, name) ⇒ String?

Get attribute value from a node

Parameters:

  • node (Object)

    the node

  • name (String)

    attribute name

Returns:

  • (String, nil)

    attribute value or nil



27
28
29
30
31
# File 'lib/svg_conform/node_helpers.rb', line 27

def get_attribute(node, name)
  return nil unless node.respond_to?(:[])

  node[name]
end

#has_attribute?(node, name) ⇒ Boolean

Check if node has an attribute

Parameters:

  • node (Object)

    the node

  • name (String)

    attribute name

Returns:

  • (Boolean)

    true if attribute exists



49
50
51
52
53
# File 'lib/svg_conform/node_helpers.rb', line 49

def has_attribute?(node, name)
  return false unless node.respond_to?(:[])

  !node[name].nil?
end

#remove_attribute(node, name) ⇒ Boolean

Remove attribute from a node

Parameters:

  • node (Object)

    the node

  • name (String)

    attribute name

Returns:

  • (Boolean)

    true if successful



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/svg_conform/node_helpers.rb', line 59

def remove_attribute(node, name)
  if node.respond_to?(:remove_attribute)
    node.remove_attribute(name)
    true
  elsif node.respond_to?(:[]=) && node.respond_to?(:[])
    # Fallback for bracket notation
    node[name] = nil
    true
  else
    false
  end
end

#set_attribute(node, name, value) ⇒ Boolean

Set attribute value on a node

Parameters:

  • node (Object)

    the node

  • name (String)

    attribute name

  • value (String)

    attribute value

Returns:

  • (Boolean)

    true if successful



38
39
40
41
42
43
# File 'lib/svg_conform/node_helpers.rb', line 38

def set_attribute(node, name, value)
  return unless node.respond_to?(:[]=)

  node[name] = value
  true
end

#text?(node) ⇒ Boolean

Check if a node is text

Parameters:

  • node (Object)

    the node to check

Returns:

  • (Boolean)

    true if the node is text



19
20
21
# File 'lib/svg_conform/node_helpers.rb', line 19

def text?(node)
  node.respond_to?(:text?) && node.text?
end