Class: Krikri::Parser::Value
- Inherits:
-
Object
- Object
- Krikri::Parser::Value
- Defined in:
- lib/krikri/parser.rb
Overview
A generic parser value.
Interface to a single value node which can access typed data values (e.g. String, DateTime, etc…) parsed from a string, and provides access to child nodes and attributes.
Direct Known Subclasses
Instance Method Summary collapse
-
#[](name_exp) ⇒ Krikri::Parser::ValueArray
Property accessor interface.
-
#attribute?(name) ⇒ Boolean
Queries whether ‘name` is an attribute of this node.
-
#attributes ⇒ Array<Symbol>
abstract
A list of attributes accessible on the node.
-
#child?(name) ⇒ Boolean
Queries whether ‘name` is a subproperty of this node.
-
#children ⇒ Array<Symbol>
abstract
A list of subproperties that can be passed back to #[] to access child nodes.
- #method_missing(name, *args, &block) ⇒ Object
- #respond_to_missing(method) ⇒ Object
-
#value ⇒ <#to_s>
abstract
Typed value for the property.
-
#values? ⇒ Boolean
abstract
True if this node has typed values accessible with #values.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
113 114 115 116 |
# File 'lib/krikri/parser.rb', line 113 def method_missing(name, *args, &block) return attribute(name) if attribute?(name) super end |
Instance Method Details
#[](name_exp) ⇒ Krikri::Parser::ValueArray
Property accessor interface. Passes a name expression (‘name_exp`) to the local implementation of #get_child_nodes.
The given name expression must follow the pattern:
name [| name ...]
The optional “|” is a short-circuit operator that will return the property or element in the document for the first matching part of the phrase.
55 56 57 58 59 60 61 |
# File 'lib/krikri/parser.rb', line 55 def [](name_exp) name_exp.strip.split(/\s*\|\s*/).each do |n| result = get_child_nodes(n) return result unless result.empty? end Krikri::Parser::ValueArray.new([]) end |
#attribute?(name) ⇒ Boolean
Queries whether ‘name` is an attribute of this node
105 106 107 108 109 110 111 |
# File 'lib/krikri/parser.rb', line 105 def attribute?(name) begin attributes.include?(name) rescue NotImplementedError false end end |
#attributes ⇒ Array<Symbol>
Returns a list of attributes accessible on the node.
97 98 99 |
# File 'lib/krikri/parser.rb', line 97 def attributes raise NotImplementedError end |
#child?(name) ⇒ Boolean
Queries whether ‘name` is a subproperty of this node
67 68 69 |
# File 'lib/krikri/parser.rb', line 67 def child?(name) children.include?(name) end |
#children ⇒ Array<Symbol>
Returns a list of subproperties that can be passed back to #[] to access child nodes.
75 76 77 |
# File 'lib/krikri/parser.rb', line 75 def children raise NotImplementedError end |
#respond_to_missing(method) ⇒ Object
118 119 120 |
# File 'lib/krikri/parser.rb', line 118 def respond_to_missing(method, *) attribute?(method) || super end |
#value ⇒ <#to_s>
Returns typed value for the property.
82 83 84 |
# File 'lib/krikri/parser.rb', line 82 def value raise NotImplementedError end |
#values? ⇒ Boolean
Returns true if this node has typed values accessible with #values.
90 91 92 |
# File 'lib/krikri/parser.rb', line 90 def values? raise NotImplementedError end |