class TermUtils::AP::Parameter

Represents a Parameter.

Attributes

articles[RW]

@return [Array<Article>]

flags[RW]

@return [Array<Flag>]

id[RW]

@return [Symbol]

max_occurs[RW]

@return [Integer]

min_occurs[RW]

@return [Integer]

Public Class Methods

new(opts = {}) click to toggle source

Constructs a new Parameter. @param opts [Hash] @option opts [Symbol] :id @option opts [Integer] :min_occurs Default value is `0`. @option opts [Integer] :max_occurs Default value is `1`.

# File lib/term_utils/ap/parameter.rb, line 39
def initialize(opts = {})
  @id = opts.fetch(:id, nil)
  @min_occurs = opts.fetch(:min_occurs, 0)
  @max_occurs = opts.fetch(:max_occurs, 1)
  @flags = []
  @articles = []
end

Public Instance Methods

define_article(id = nil, opts = {}, &block) click to toggle source

Adds a new Article to this one. @param id [Symbol, nil] @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`. @option opts [Symbol] :type `:integer`, `:string`. @option opts [Symbol] :format @return [Article]

# File lib/term_utils/ap/parameter.rb, line 115
def define_article(id = nil, opts = {}, &block)
  if id
    art = @articles.find { |p| p.id == id }
    if art
      block.call(art) if block
      return art
    end

    opts[:id] = id
  end
  new_article = TermUtils::AP::Article.new(opts)
  @articles << new_article
  block.call(new_article) if block
  new_article
end
define_flag(label, &block) click to toggle source

Adds a new Flag to this one. @param label [String] @return [Flag]

# File lib/term_utils/ap/parameter.rb, line 96
def define_flag(label, &block)
  flavor = (label.length == 2) ? :short : :long
  new_flag = TermUtils::AP::Flag.new(label, flavor)
  raise TermUtils::AP::SyntaxError, 'duplicate flag label' if @flags.include? new_flag

  @flags << new_flag
  block.call(new_flag) if block
  new_flag
end
fetch_articles() click to toggle source

Fetches all articles. @return [Array<Article>]

# File lib/term_utils/ap/parameter.rb, line 139
def fetch_articles
  @articles.collect(&:dup)
end
fetch_flags() click to toggle source

Fetches all flags. @return [Array<Flag>]

# File lib/term_utils/ap/parameter.rb, line 133
def fetch_flags
  @flags.dup
end
finalize!(opts = {}) click to toggle source

Finalizes this one. Internal use. @return [nil] @raise [SyntaxError]

# File lib/term_utils/ap/parameter.rb, line 57
def finalize!(opts = {})
  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)
  raise TermUtils::AP::SyntaxError, 'empty' if @flags.empty? && @articles.empty?

  unless @id
    opts[:anonymous] += 1
    @id = "anonymous#{opts[:anonymous]}".intern
  end
  @flags.each do |f|
    raise TermUtils::AP::SyntaxError, 'duplicate flag label' if opts[:flag_labels].include?(f.to_s)

    opts[:flag_labels] << f.to_s
  end
  @articles.each { |a| a.finalize!(opts) }
  nil
end
flagged?() click to toggle source

Tests whether this one is flagged. @return [Boolean]

# File lib/term_utils/ap/parameter.rb, line 89
def flagged?
  !@flags.empty?
end
initialize_dup(other) click to toggle source

For dup method.

Calls superclass method
# File lib/term_utils/ap/parameter.rb, line 48
def initialize_dup(other)
  super(other)
  @flags = other.flags.map(&:dup) if other.flags
  @articles = other.articles.map(&:dup) if other.articles
end
multiple_occurs?() click to toggle source

Tests whether this one has mutiple occurs. @return [Boolean]

# File lib/term_utils/ap/parameter.rb, line 77
def multiple_occurs?
  (@max_occurs == nil) || (@max_occurs.is_a?(Integer) && (@max_occurs > 1))
end
occur_bounded?() click to toggle source

Tests whether the number of occurs is fixed. @return [Boolean]

# File lib/term_utils/ap/parameter.rb, line 83
def occur_bounded?
  (@max_occurs != nil) && @max_occurs.is_a?(Integer)
end