Class: Nom::XML::Term

Inherits:
Object
  • Object
show all
Defined in:
lib/nom/xml/term.rb

Direct Known Subclasses

Terminology

Constant Summary collapse

PATH_SEPARATOR =
'/'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent, name, options = {}, *args) { ... } ⇒ Term

Create a new Nom::XML::Term

Yields:



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/nom/xml/term.rb', line 18

def initialize parent, name, options = {}, *args, &block
  if Nokogiri::XML::Node.instance_methods.include?(name.to_sym)
    raise "Unable to redefine method #{name} on Nokogiri::XML::Node"
  end

  @name = name
  @terms = {}
  @parent = parent
  @options = options || {}

  in_edit_context do
    yield(self) if block_given?
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



105
106
107
108
109
110
111
112
113
# File 'lib/nom/xml/term.rb', line 105

def method_missing method, *args, &block 
  if in_edit_context?
    add_term(method, *args, &block)
  elsif key?(method)
    term(method)
  else
    super
  end
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/nom/xml/term.rb', line 3

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'lib/nom/xml/term.rb', line 7

def options
  @options
end

#parentObject

Returns the value of attribute parent.



5
6
7
# File 'lib/nom/xml/term.rb', line 5

def parent
  @parent
end

#termsObject (readonly)

Returns the value of attribute terms.



4
5
6
# File 'lib/nom/xml/term.rb', line 4

def terms
  @terms
end

Instance Method Details

#flattenArray

Flatten this term and all sub-terms (recursively)

Returns:

  • (Array)


91
92
93
# File 'lib/nom/xml/term.rb', line 91

def flatten
  [self, terms.map { |k,v| v.flatten }].flatten
end

#full_nameObject



33
34
35
# File 'lib/nom/xml/term.rb', line 33

def full_name
  @full_name ||= [parent.full_name, "term:#{name}"].join(PATH_SEPARATOR)
end

#key?(term_key) ⇒ Boolean

Does this term have a sub-term called term_name

Returns:

  • (Boolean)


84
85
86
# File 'lib/nom/xml/term.rb', line 84

def key? term_key
  terms.key? term_key
end

#local_xpathString

Get the relative xpath to this node from its immediate parent’s term

Returns:

  • (String)


54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/nom/xml/term.rb', line 54

def local_xpath
  xpath = if xmlns.nil? || xmlns.empty?
    ""
  else
    xmlns + ":"
  end
  xpath << "#{options[:path] || name}"
  xpath << "[#{options[:if]}]" if options[:if] and options[:if].is_a? String
  xpath << "[not(#{options[:unless]})]" if options[:unless] and options[:unless].is_a? String

  xpath
end

#nodesNokogiri::XML::NodeSet

Get the document nodes associated with this term

Returns:

  • (Nokogiri::XML::NodeSet)


70
71
72
# File 'lib/nom/xml/term.rb', line 70

def nodes
  terminology.document.root.xpath(xpath, terminology.namespaces)
end

#respond_to_missing?(method, *args, &block) ⇒ Boolean

Returns:

  • (Boolean)


95
96
97
98
99
100
101
102
103
# File 'lib/nom/xml/term.rb', line 95

def respond_to_missing? method, *args, &block
  if in_edit_context?
    true
  elsif key? method
    true
  else
    super
  end
end

#terminologyNom::XML::Terminology

Traverse the tree to figure out what terminology this term belongs to



40
41
42
# File 'lib/nom/xml/term.rb', line 40

def terminology
  @terminology ||= parent.terminology
end

#to_sObject



115
116
117
# File 'lib/nom/xml/term.rb', line 115

def to_s
  %Q{#<#{self.class.to_s}:#{object_id} #{full_name} name="#{name}" xpath="#{xpath}">}
end

#valuesObject

Get the document values associated with the term (after e.g. accessors)



76
77
78
# File 'lib/nom/xml/term.rb', line 76

def values
  terminology.document.root.xpath(xpath, terminology.namespaces).values_for_term(self)
end

#xpathString

Get the absolute xpath to this node

Returns:

  • (String)


47
48
49
# File 'lib/nom/xml/term.rb', line 47

def xpath
  @xpath ||= [parent_xpath, local_xpath].flatten.compact.join(PATH_SEPARATOR)
end