Class: TermUtils::AP::Parameter

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

Overview

Represents a Parameter.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Parameter

Constructs a new Parameter.

Parameters:

  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :id (Symbol)
  • :min_occurs (Integer)

    Default value is ‘0`.

  • :max_occurs (Integer)

    Default value is ‘1`.



39
40
41
42
43
44
45
# 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

Instance Attribute Details

#articlesArray<Article>

Returns:



32
33
34
# File 'lib/term_utils/ap/parameter.rb', line 32

def articles
  @articles
end

#flagsArray<Flag>

Returns:



30
31
32
# File 'lib/term_utils/ap/parameter.rb', line 30

def flags
  @flags
end

#idSymbol

Returns:

  • (Symbol)


24
25
26
# File 'lib/term_utils/ap/parameter.rb', line 24

def id
  @id
end

#max_occursInteger

Returns:

  • (Integer)


28
29
30
# File 'lib/term_utils/ap/parameter.rb', line 28

def max_occurs
  @max_occurs
end

#min_occursInteger

Returns:

  • (Integer)


26
27
28
# File 'lib/term_utils/ap/parameter.rb', line 26

def min_occurs
  @min_occurs
end

Instance Method Details

#define_article(id = nil, opts = {}, &block) ⇒ Article

Adds a new Article to this one.

Parameters:

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

Options Hash (opts):

  • :id (Symbol)
  • :min_occurs (Integer)

    Default value is ‘1`.

  • :max_occurs (Integer)

    Default value is ‘1`.

  • :type (Symbol)

    ‘:integer`, `:string`.

  • :format (Symbol)

Returns:



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# 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) ⇒ Flag

Adds a new Flag to this one.

Parameters:

  • label (String)

Returns:

Raises:



96
97
98
99
100
101
102
103
104
# 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_articlesArray<Article>

Fetches all articles.

Returns:



139
140
141
# File 'lib/term_utils/ap/parameter.rb', line 139

def fetch_articles
  @articles.collect(&:dup)
end

#fetch_flagsArray<Flag>

Fetches all flags.

Returns:



133
134
135
# File 'lib/term_utils/ap/parameter.rb', line 133

def fetch_flags
  @flags.dup
end

#finalize!(opts = {}) ⇒ nil

Finalizes this one. Internal use.

Returns:

  • (nil)

Raises:



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# 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?Boolean

Tests whether this one is flagged.

Returns:

  • (Boolean)


89
90
91
# File 'lib/term_utils/ap/parameter.rb', line 89

def flagged?
  !@flags.empty?
end

#initialize_dup(other) ⇒ Object

For dup method.



48
49
50
51
52
# 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?Boolean

Tests whether this one has mutiple occurs.

Returns:

  • (Boolean)


77
78
79
# 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?Boolean

Tests whether the number of occurs is fixed.

Returns:

  • (Boolean)


83
84
85
# File 'lib/term_utils/ap/parameter.rb', line 83

def occur_bounded?
  (@max_occurs != nil) && @max_occurs.is_a?(Integer)
end