Class: TermUtils::AP::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/term_utils/ap/result.rb

Overview

Represents an argument parsing Result.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(syntax, element = nil, value = nil) ⇒ Result

Constructs a new Result.

Parameters:

  • syntax (Darn::AP::Syntax)

    Syntax.

  • element (Darn::AP::Element) (defaults to: nil)

    Optional Element.

  • value (Darn::PropertyTreeNode) (defaults to: nil)

    Optional value.



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

#valueTermUtils::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.

Parameters:

  • id (Symbol, Array<Symbol>)

Returns:

  • (Integer)

Raises:



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.

Parameters:

  • id (Symbol, Array<Symbol>, nil)
  • opts (Hash) (defaults to: {})

    `:index`, `:multi`.

Options Hash (opts):

  • :index (Integer)

    The index of a multiple-occurrence level/parameter/article.

  • :multi (Boolean)

    Whether an array of values shall be returned instead of a single value.

Returns:

  • (Object)

Raises:



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.

Parameters:

  • id (Symbol, Array<Symbol>)

Returns:

  • (Boolean)


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.

Parameters:

  • id (Symbol, Array<Symbol>)
  • opts (Hash) (defaults to: {})

    `:index`, `:multi`.

Options Hash (opts):

  • :index (Integer)

    The index of a multiple-occurrence level/parameter/article.

Returns:

Raises:



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