lib/term_utils/ap/article.rb in term_utils-0.3.2 vs lib/term_utils/ap/article.rb in term_utils-0.4.0

- old
+ new

@@ -1,9 +1,9 @@ # frozen-string-literal: true + +# Copyright (C) 2020 Thomas Baron # -# Copyright (C) 2019 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. @@ -13,10 +13,11 @@ # 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 <https://www.gnu.org/licenses/>. + module TermUtils module AP # Represents a Article. class Article # @return [Symbol] @@ -27,10 +28,11 @@ attr_accessor :max_occurs # @return [Symbol] `:integer`, `:string` attr_accessor :type # @return [String] `%d`, `%s`. attr_accessor :format + # Constructs a new Article. # @param opts [Hash] # @option opts [Symbol] :id # @option opts [Integer] :min_occurs Default value is `1`. # @option opts [Integer] :max_occurs Default value is `1`. @@ -39,30 +41,34 @@ def initialize(opts = {}) @id = opts.fetch(:id, nil) @min_occurs = opts.fetch(:min_occurs, 1) @max_occurs = opts.fetch(:max_occurs, 1) @type = opts.fetch(:type, :string) - @format = opts.fetch(:format, "%s") + @format = opts.fetch(:format, '%s') end + # Finalizes this one. Internal use. # @return [nil] def finalize!(opts = {}) - raise TermUtils::AP::SyntaxError, "min_occurs must be equal or greater than 0" unless (@min_occurs.is_a? Integer) && (@min_occurs >= 0) - raise TermUtils::AP::SyntaxError, "max_occurs must be equal or greater than min_occurs" unless !occur_bounded? || (@max_occurs >= @min_occurs) + raise TermUtils::AP::SyntaxError, 'min_occurs must be equal or greater than 0' if !@min_occurs.is_a?(Integer) || (@min_occurs < 0) + raise TermUtils::AP::SyntaxError, 'max_occurs must be equal or greater than min_occurs' if occur_bounded? && (@max_occurs < @min_occurs) + unless @id opts[:anonymous] += 1 @id = "anonymous#{opts[:anonymous]}".intern end end + # Tests whether this one has mutiple occurs. # @return [Boolean] def multiple_occurs? - (@max_occurs == nil) || (@max_occurs == :infinity) || ((@max_occurs.is_a? Integer) && (@max_occurs > 1)) + (@max_occurs == nil) || (@max_occurs.is_a?(Integer) && (@max_occurs > 1)) end - # Tests whether the number of occurs are fixed. + + # Tests whether the number of occurs is fixed. # @return [Boolean] def occur_bounded? - (@max_occurs != nil) && (@max_occurs != :infinity) && (@max_occurs.is_a? Integer) + (@max_occurs != nil) && @max_occurs.is_a?(Integer) end end end end