Module: Duxml::PatternMaker

Includes:
Duxml
Included in:
Grammar
Defined in:
lib/duxml/meta/grammar/pattern_maker.rb

Overview

helper methods to create Patterns from a given Element’s relationships with its members

Instance Attribute Summary

Attributes included from Duxml

#doc

Attributes included from Saxer

#io

Instance Method Summary collapse

Methods included from Duxml

#load, #log, #save, #validate

Methods included from Saxer

#sax

Instance Method Details

#get_child_patterns(node) ⇒ Array[Duxml::ChildPattern, Duxml::ContentPattern]

Returns one pattern for each child that exists.

Parameters:

  • node (Duxml::Element)

    object whose relationships are to be made into patterns

Returns:

  • (Array[Duxml::ChildPattern, Duxml::ContentPattern])

    one pattern for each child that exists



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/duxml/meta/grammar/pattern_maker.rb', line 57

def get_child_patterns(node)
  i = -1
  node.nodes.collect do |child|
    i += 1
    if child.is_a?(String)
      TextPatternClass.new(node, child, i)
    else
      ChildPatternClass.new(node, child, i)
    end
  end
end

#get_existing_attr_patterns(node) ⇒ Array[Duxml::AttrNamePattern, Duxml::AttrValPattern]

Returns one pattern for each existing attribute.

Parameters:

  • node (Duxml::Element)

    doc whose relationships are to be made into patterns

Returns:



23
24
25
26
27
28
# File 'lib/duxml/meta/grammar/pattern_maker.rb', line 23

def get_existing_attr_patterns(node)
  # check existing attributes

  node.attributes.collect do |k, v|
    [AttrNamePatternClass.new(node, k), AttrValPatternClass.new(node, k)]
  end.flatten
end

#get_null_attr_patterns(node) ⇒ Array[AttrNamePattern]

Returns one pattern for each attribute that should but does not exist.

Parameters:

  • node (Element)

    doc whose relationships are to be made into patterns

Returns:

  • (Array[AttrNamePattern])

    one pattern for each attribute that should but does not exist



32
33
34
35
36
37
38
# File 'lib/duxml/meta/grammar/pattern_maker.rb', line 32

def get_null_attr_patterns(node)
  self.AttrsRuleClass.collect do |attr_rule|
    if attr_rule.required? && node.name == attr_rule.subject
      AttrNamePatternClass.new(node, attr_rule.attr_name) unless node[attr_rule.attr_name]
    end
  end.compact
end

#get_null_child_patterns(node) ⇒ Array[ChildPattern]

Returns one pattern for each child that should be there but isn’t.

Parameters:

  • node (Duxml::Element)

    doc whose relationships are to be made into patterns

Returns:

  • (Array[ChildPattern])

    one pattern for each child that should be there but isn’t



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/duxml/meta/grammar/pattern_maker.rb', line 42

def get_null_child_patterns(node)
  self.ChildrenRuleClass.each do |child_rule|
    if node.name == child_rule.subject
      return child_rule.required_children.collect do |required_child_type|
        unless node.nodes.any? do |n| n.name == required_child_type end
          NullChildPatternClass.new(node, required_child_type, -1)
        end
      end.compact
    end
  end
  []
end

#get_relationships(node) ⇒ Array[Duxml::Pattern]

Returns array of patterns representing every relationship of this XMl node and its members.

Parameters:

  • node (Duxml::Element)

    doc whose relationships are to be made into patterns

Returns:

  • (Array[Duxml::Pattern])

    array of patterns representing every relationship of this XMl node and its members



14
15
16
17
18
19
# File 'lib/duxml/meta/grammar/pattern_maker.rb', line 14

def get_relationships(node)
  [get_child_patterns(node),
   get_null_child_patterns(node),
   get_existing_attr_patterns(node),
  get_null_attr_patterns(node)].flatten
end