Class: Nom::XML::Term
- Inherits:
-
Object
- Object
- Nom::XML::Term
- Defined in:
- lib/nom/xml/term.rb
Direct Known Subclasses
Constant Summary collapse
- PATH_SEPARATOR =
'/'
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#parent ⇒ Object
Returns the value of attribute parent.
-
#terms ⇒ Object
readonly
Returns the value of attribute terms.
Instance Method Summary collapse
-
#flatten ⇒ Array
Flatten this term and all sub-terms (recursively).
- #full_name ⇒ Object
-
#initialize(parent, name, options = {}, *args) { ... } ⇒ Term
constructor
Create a new Nom::XML::Term.
-
#key?(term_key) ⇒ Boolean
Does this term have a sub-term called term_name.
-
#local_xpath ⇒ String
Get the relative xpath to this node from its immediate parent’s term.
- #method_missing(method, *args, &block) ⇒ Object
-
#nodes ⇒ Nokogiri::XML::NodeSet
Get the document nodes associated with this term.
- #respond_to_missing?(method, *args, &block) ⇒ Boolean
-
#terminology ⇒ Nom::XML::Terminology
Traverse the tree to figure out what terminology this term belongs to.
- #to_s ⇒ Object
-
#values ⇒ Object
Get the document values associated with the term (after e.g. accessors).
-
#xpath ⇒ String
Get the absolute xpath to this node.
Constructor Details
#initialize(parent, name, options = {}, *args) { ... } ⇒ Term
Create a new Nom::XML::Term
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, = {}, *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 = || {} 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
#name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/nom/xml/term.rb', line 3 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
7 8 9 |
# File 'lib/nom/xml/term.rb', line 7 def @options end |
#parent ⇒ Object
Returns the value of attribute parent.
5 6 7 |
# File 'lib/nom/xml/term.rb', line 5 def parent @parent end |
#terms ⇒ Object (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
#flatten ⇒ Array
Flatten this term and all sub-terms (recursively)
91 92 93 |
# File 'lib/nom/xml/term.rb', line 91 def flatten [self, terms.map { |k,v| v.flatten }].flatten end |
#full_name ⇒ Object
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
84 85 86 |
# File 'lib/nom/xml/term.rb', line 84 def key? term_key terms.key? term_key end |
#local_xpath ⇒ String
Get the relative xpath to this node from its immediate parent’s term
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 << "#{[:path] || name}" xpath << "[#{[:if]}]" if [:if] and [:if].is_a? String xpath << "[not(#{[:unless]})]" if [:unless] and [:unless].is_a? String xpath end |
#nodes ⇒ Nokogiri::XML::NodeSet
Get the document nodes associated with this term
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
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 |
#terminology ⇒ Nom::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_s ⇒ Object
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 |
#values ⇒ Object
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 |
#xpath ⇒ String
Get the absolute xpath to this node
47 48 49 |
# File 'lib/nom/xml/term.rb', line 47 def xpath @xpath ||= [parent_xpath, local_xpath].flatten.compact.join(PATH_SEPARATOR) end |