Class: Nom::XML::Term

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

Direct Known Subclasses

Terminology

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Create a new Nom::XML::Term

Yields:



16
17
18
19
20
21
22
23
24
25
# File 'lib/nom/xml/term.rb', line 16

def initialize parent, name, options = {}, *args, &block
  @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



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

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)


81
82
83
# File 'lib/nom/xml/term.rb', line 81

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

#full_nameObject



27
28
29
# File 'lib/nom/xml/term.rb', line 27

def full_name
  [parent.full_name, "term:#{name}"].join("/")
end

#key?(term_key) ⇒ Boolean

Does this term have a sub-term called term_name

Returns:

  • (Boolean)


74
75
76
# File 'lib/nom/xml/term.rb', line 74

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)


48
49
50
51
52
53
54
55
# File 'lib/nom/xml/term.rb', line 48

def local_xpath
  xpath = ("#{xmlns}:" unless xmlns.blank? ).to_s + (options[:path] || name).to_s

  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)


60
61
62
# File 'lib/nom/xml/term.rb', line 60

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

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

Returns:

  • (Boolean)


85
86
87
88
89
90
91
92
93
# File 'lib/nom/xml/term.rb', line 85

def respond_to? 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



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

def terminology
  @terminology ||= parent.terminology
end

#to_sObject



105
106
107
# File 'lib/nom/xml/term.rb', line 105

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)



66
67
68
# File 'lib/nom/xml/term.rb', line 66

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

#xpathString

Get the absolute xpath to this node

Returns:

  • (String)


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

def xpath
  [parent_xpath, local_xpath].flatten.compact.join("/")
end