Class: TermUtils::AP::Result
- Inherits:
-
Object
- Object
- TermUtils::AP::Result
- Defined in:
- lib/term_utils/ap/result.rb
Overview
Represents an argument parsing Result.
Instance Attribute Summary collapse
Instance Method Summary collapse
-
#collect(id, &block) ⇒ Array<Symbol>
Collects IDs.
-
#eval_occurs(id) ⇒ Integer
Evaluates the number of occurrences of a given level, parameter or article.
-
#fetch_value(id, opts = {}) ⇒ Object
Fetches a value.
-
#initialize(syntax, element = nil, value = nil) ⇒ Result
constructor
Constructs a new Result.
-
#present?(id) ⇒ Boolean
Tests whether a given level/parameter/article is present in the result value.
-
#shift(id, opts = {}) ⇒ TermUtils::AP::Result
Shifts this one.
Constructor Details
#initialize(syntax, element = nil, value = nil) ⇒ Result
Constructs a new Result.
28 29 30 31 32 33 34 35 36 37 |
# File 'lib/term_utils/ap/result.rb', line 28 def initialize(syntax, element = nil, value = nil) @syntax = syntax.dup @element = element ? element.dup : nil if value @value = value.dup @value.key = nil else @value = TermUtils::PropertyTreeNode.new end end |
Instance Attribute Details
#value ⇒ TermUtils::PropertyTreeNode
23 24 25 |
# File 'lib/term_utils/ap/result.rb', line 23 def value @value end |
Instance Method Details
#collect(id, &block) ⇒ Array<Symbol>
Collects IDs.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/term_utils/ap/result.rb', line 41 def collect(id, &block) node = @value if id id = [id] if id.is_a? Symbol node = @value.find_node(id) end res = [] return res unless node && node.child_nodes if block node.child_nodes.each do |n| res << n.key if block.call(n.key) end else node.child_nodes.each do |n| res << n.key end end res end |
#eval_occurs(id) ⇒ Integer
Evaluates the number of occurrences of a given level, parameter or article.
73 74 75 76 77 78 79 80 81 82 |
# File 'lib/term_utils/ap/result.rb', line 73 def eval_occurs(id) id = [id] if id.is_a? Symbol obj = fetch_syntax_object(id) raise TermUtils::AP::NoSuchValueError, "no such syntax object" unless obj node = @value.find_node(id) return 0 unless node return 1 if obj.occur_bounded? && (obj.max_occurs == 1) # Parameter is multiple. node.child_nodes ? node.child_nodes.length : 0 end |
#fetch_value(id, opts = {}) ⇒ Object
Fetches a value.
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/term_utils/ap/result.rb', line 90 def fetch_value(id, opts = {}) index = opts.fetch(:index, nil) multi = opts.fetch(:multi, false) unless id node = @value if node && node.child_nodes && index node = node.child_node(index) end raise TermUtils::AP::NoSuchValueError, "no such value" unless node vals = node.collect_values raise TermUtils::AP::NoSuchValueError, "no such value" if vals.empty? return multi ? vals : vals.first end id = [id] if id.is_a? Symbol obj = fetch_syntax_object(id) raise TermUtils::AP::NoSuchValueError, "no such syntax object" unless obj node = @value.find_node(id) if node && node.child_nodes && index node = node.child_node(index) end raise TermUtils::AP::NoSuchValueError, "no such value" unless node catch :value do if obj.is_a? TermUtils::AP::Parameter raise TermUtils::AP::NoSuchValueError, "parameter has no article" if obj.articles.empty? raise TermUtils::AP::NoSuchValueError, "no such value" if node.leaf? vals = node.collect_values raise TermUtils::AP::NoSuchValueError, "no such value" if vals.empty? if multi throw :value, vals else throw :value, vals.first end elsif obj.is_a? TermUtils::AP::Article # raise TermUtils::AP::NoSuchValueError, "no such value" if node.leaf? vals = node.collect_values raise TermUtils::AP::NoSuchValueError, "no such value" if vals.empty? if multi throw :value, vals else throw :value, vals.first end end raise TermUtils::AP::NoSuchValueError, "wrong id" end end |
#present?(id) ⇒ Boolean
Tests whether a given level/parameter/article is present in the result value.
63 64 65 66 67 68 69 |
# File 'lib/term_utils/ap/result.rb', line 63 def present?(id) if id.is_a? Symbol !!@value.child_node(id) elsif id.is_a? Array @value.node_exists?(id) end end |
#shift(id, opts = {}) ⇒ TermUtils::AP::Result
Shifts this one.
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/term_utils/ap/result.rb', line 141 def shift(id, opts = {}) index = opts.fetch(:index, nil) id = [id] if id.is_a? Symbol obj = fetch_syntax_object(id) raise TermUtils::AP::NoSuchValueError, "no such syntax object" unless obj node = @value.find_node(id) if node && node.child_nodes && index node = node.child_node(index) end raise TermUtils::AP::NoSuchValueError, "no such value" unless node catch :value do if obj.is_a? TermUtils::AP::Level throw :value, TermUtils::AP::Result.new(obj.syntax, nil, node) elsif obj.is_a? TermUtils::AP::Parameter throw :value, TermUtils::AP::Result.new(@syntax, obj, node) elsif obj.is_a? TermUtils::AP::Article throw :value, TermUtils::AP::Result.new(@syntax, obj, node) end raise TermUtils::AP::NoSuchValueError, "wrong id" end end |