# frozen-string-literal: true
# Copyright (C) 2020 Thomas Baron
#
# This file is part of term_utils.
#
# term_utils is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3 of the License.
#
# term_utils is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with term_utils. If not, see .
module TermUtils
module AP
# Represents an argument parsing Result.
class Result
# @return [Syntax]
attr_reader :parameter
# @return [Array]
attr_reader :results
# @return [Array]
attr_accessor :remaining_arguments
# Constructs a new Result.
# @param syntax [Syntax]
def initialize(syntax)
@syntax = syntax
@results = []
@remaining_arguments = nil
end
# Adds a ParameterResult.
# @param result [ParameterResult]
def add_result(result)
@results << result
end
# Returns the first ParameterResult for a given parameter id.
# @param id [Symbol]
# @return [ParameterResult]
def find_parameter(id)
@results.find { |r| r.param_id == id }
end
# Returns all ParameterResult(s) for a given parameter id.
# @param id [Symbol]
# @return [Array]
def find_parameters(id)
@results.find_all { |r| r.param_id == id }
end
# Walks through this one.
def walk(&block)
walker = TermUtils::AP::Walker.new
block.call(walker)
@results.each do |p|
p.results.each do |a|
walker.notify_article(a)
end
walker.notify_parameter(p)
end
walker.notify_finished(@remaining_arguments)
end
end
# Represents a result for a parameter.
class ParameterResult
# @return [Parameter]
attr_accessor :parameter
# @return [Array]
attr_accessor :results
# Constructs a new ParameterResult.
# @param parent [Result]
# @param parameter [Parameter]
def initialize(parent, parameter)
@parent = parent
@parent.add_result(self)
@parameter = parameter
@results = []
end
# Adds an ArticleResult.
# @param result [ArticleResult]
def add_result(result)
@results << result
end
# @return [Symbol]
def param_id
@parameter.id
end
# Returns the first ArticleResult for a given article id.
# @param id [Symbol]
# @return [ArticleResult]
def find_article(id)
@results.find { |r| r.art_id == id }
end
# Returns all ArticleResult(s) for a given article id.
# @param id [Symbol]
# @return [Array]
def find_articles(id)
@results.find_all { |r| r.art_id == id }
end
# Returns the value of the first ArticleResult.
# @param id [Symbol] Filter of article id.
# @return [Object]
def value(id = nil)
return @results.first.value unless id
find_article(id).value
end
# Returns the value of all ArticleResult(s).
# @param id [Symbol] Filter of article id.
# @return [Array