Class: Nom::XML::Term
- Inherits:
-
Object
- Object
- Nom::XML::Term
- Defined in:
- lib/nom/xml/term.rb
Direct Known Subclasses
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?(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
16 17 18 19 20 21 22 23 24 25 |
# File 'lib/nom/xml/term.rb', line 16 def initialize parent, name, = {}, *args, &block @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
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
#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)
81 82 83 |
# File 'lib/nom/xml/term.rb', line 81 def flatten [self, terms.map { |k,v| v.flatten }].flatten end |
#full_name ⇒ Object
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
74 75 76 |
# File 'lib/nom/xml/term.rb', line 74 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
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 + ([:path] || name).to_s 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
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
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 |
#terminology ⇒ Nom::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_s ⇒ Object
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 |
#values ⇒ Object
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 |
#xpath ⇒ String
Get the absolute xpath to this node
41 42 43 |
# File 'lib/nom/xml/term.rb', line 41 def xpath [parent_xpath, local_xpath].flatten.compact.join("/") end |