Class: Saxxy::NodeRule

Inherits:
Object
  • Object
show all
Defined in:
lib/saxxy/node_rule.rb

Overview

NodeRule describes a rule that will be tested upon an XML node and will check if the node satisfies this NodeRule.

The NodeRule consists of two parts. The `element` part which refers to what should hold for the node's name. It can be either a String (where the strict equality is should hold) or a Regexp (where the Regexp must match the node name).

The other part is the `attributes` part which refers to what should hold for the attributes of the node. It consists of key-value pairs where the key is the attribute to check and the value is what should hold for that attribute.

Author:

  • rubymaniac

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(element, attributes = {}) ⇒ NodeRule

Initializes a NodeRule with an `element` part and an `attributes` part.


34
35
36
37
# File 'lib/saxxy/node_rule.rb', line 34

def initialize(element, attributes = {})
  @element = element
  @attributes = Saxxy::Helpers.stringify_keys(attributes)
end

Instance Attribute Details

#attributesHash<String, String|Regexp> (readonly)


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/saxxy/node_rule.rb', line 25

class NodeRule
  attr_reader :element, :attributes

  # Initializes a NodeRule with an `element` part and an `attributes` part.
  #
  # @param element [String|Regexp] what should hold for the node name
  # @param attributes [Hash<String, String|Regexp>]
  #   what should hold for node's attributes
  #
  def initialize(element, attributes = {})
    @element = element
    @attributes = Saxxy::Helpers.stringify_keys(attributes)
  end

  # Checks whether this NodeRule matches a node.
  #
  # @param element_name [String] node's name
  # @param attrs [Hash<String, String>] node's attributes
  #
  # @return [Boolean] whether this NodeRule matches the node
  #
  def matches(element_name, attrs = {})
    match_element_name(element_name) && match_attributes(attrs)
  end

  # Checks whether this NodeRule is equal to another.
  #
  # @param rule [NodeRule] the other NodeRule
  #
  # @return [Boolean] whether this NodeRule equals rule
  #
  def equals(rule)
    element == rule.element && attributes == rule.attributes
  end

  # Checks whether this NodeRule matches only the name of a node.
  #
  # @param element_name [String] node's name
  #
  # @return [Boolean] whether this NodeRule matches node's name
  #
  def match_element_name(element_name)
    match(element, element_name)
  end

  # Checks whether this NodeRule matches only the attributes of a node.
  #
  # @param attrs [Hash<String, String>] node's attributes
  #
  # @return [Boolean] whether this NodeRule matches node's attributes
  #
  def match_attributes(attrs)
    attrs = Saxxy::Helpers.stringify_keys(attrs)
    attributes.reduce(true) do |b, (k, v)|
      value = attrs[k]
      b && ((!value.nil? && match(v, value)) || (v.nil? && value.nil?))
    end
  end

  private
  def match(obj, value)
    obj.is_a?(Regexp) ? !obj.match(value).nil? : obj == value
  end
end

#elementString|Regexp (readonly)


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/saxxy/node_rule.rb', line 25

class NodeRule
  attr_reader :element, :attributes

  # Initializes a NodeRule with an `element` part and an `attributes` part.
  #
  # @param element [String|Regexp] what should hold for the node name
  # @param attributes [Hash<String, String|Regexp>]
  #   what should hold for node's attributes
  #
  def initialize(element, attributes = {})
    @element = element
    @attributes = Saxxy::Helpers.stringify_keys(attributes)
  end

  # Checks whether this NodeRule matches a node.
  #
  # @param element_name [String] node's name
  # @param attrs [Hash<String, String>] node's attributes
  #
  # @return [Boolean] whether this NodeRule matches the node
  #
  def matches(element_name, attrs = {})
    match_element_name(element_name) && match_attributes(attrs)
  end

  # Checks whether this NodeRule is equal to another.
  #
  # @param rule [NodeRule] the other NodeRule
  #
  # @return [Boolean] whether this NodeRule equals rule
  #
  def equals(rule)
    element == rule.element && attributes == rule.attributes
  end

  # Checks whether this NodeRule matches only the name of a node.
  #
  # @param element_name [String] node's name
  #
  # @return [Boolean] whether this NodeRule matches node's name
  #
  def match_element_name(element_name)
    match(element, element_name)
  end

  # Checks whether this NodeRule matches only the attributes of a node.
  #
  # @param attrs [Hash<String, String>] node's attributes
  #
  # @return [Boolean] whether this NodeRule matches node's attributes
  #
  def match_attributes(attrs)
    attrs = Saxxy::Helpers.stringify_keys(attrs)
    attributes.reduce(true) do |b, (k, v)|
      value = attrs[k]
      b && ((!value.nil? && match(v, value)) || (v.nil? && value.nil?))
    end
  end

  private
  def match(obj, value)
    obj.is_a?(Regexp) ? !obj.match(value).nil? : obj == value
  end
end

Instance Method Details

#equals(rule) ⇒ Boolean

Checks whether this NodeRule is equal to another.


56
57
58
# File 'lib/saxxy/node_rule.rb', line 56

def equals(rule)
  element == rule.element && attributes == rule.attributes
end

#match_attributes(attrs) ⇒ Boolean

Checks whether this NodeRule matches only the attributes of a node.


76
77
78
79
80
81
82
# File 'lib/saxxy/node_rule.rb', line 76

def match_attributes(attrs)
  attrs = Saxxy::Helpers.stringify_keys(attrs)
  attributes.reduce(true) do |b, (k, v)|
    value = attrs[k]
    b && ((!value.nil? && match(v, value)) || (v.nil? && value.nil?))
  end
end

#match_element_name(element_name) ⇒ Boolean

Checks whether this NodeRule matches only the name of a node.


66
67
68
# File 'lib/saxxy/node_rule.rb', line 66

def match_element_name(element_name)
  match(element, element_name)
end

#matches(element_name, attrs = {}) ⇒ Boolean

Checks whether this NodeRule matches a node.


46
47
48
# File 'lib/saxxy/node_rule.rb', line 46

def matches(element_name, attrs = {})
  match_element_name(element_name) && match_attributes(attrs)
end