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
-
#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
#eval_occurs(id) ⇒ Integer
Evaluates the number of occurrences of a given level, parameter or article.
51 52 53 54 55 56 57 58 59 60 |
# File 'lib/term_utils/ap/result.rb', line 51 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.
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/term_utils/ap/result.rb', line 68 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.
41 42 43 44 45 46 47 |
# File 'lib/term_utils/ap/result.rb', line 41 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.
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/term_utils/ap/result.rb', line 119 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 |