Class: TermUtils::AP::Parameter
- Inherits:
-
Object
- Object
- TermUtils::AP::Parameter
- Defined in:
- lib/term_utils/ap/parameter.rb
Overview
Represents a Parameter.
Instance Attribute Summary collapse
- #articles ⇒ Array<Article>
- #flags ⇒ Array<Flag>
- #id ⇒ Symbol
- #max_occurs ⇒ Integer
- #min_occurs ⇒ Integer
Instance Method Summary collapse
-
#define_article(id = nil, opts = {}, &block) ⇒ Article
Adds a new Article to this one.
-
#define_flag(label, &block) ⇒ Flag
Adds a new Flag to this one.
-
#fetch_articles ⇒ Array<Article>
Fetches all articles.
-
#fetch_flags ⇒ Array<Flag>
Fetches all flags.
-
#finalize!(opts = {}) ⇒ nil
Finalizes this one.
-
#flagged? ⇒ Boolean
Tests whether this one is flagged.
-
#initialize(opts = {}) ⇒ Parameter
constructor
Constructs a new Parameter.
-
#initialize_dup(other) ⇒ Object
For dup method.
-
#multiple_occurs? ⇒ Boolean
Tests whether this one has mutiple occurs.
-
#occur_bounded? ⇒ Boolean
Tests whether the number of occurs is fixed.
Constructor Details
#initialize(opts = {}) ⇒ Parameter
Constructs a new Parameter.
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
#articles ⇒ Array<Article>
32 33 34 |
# File 'lib/term_utils/ap/parameter.rb', line 32 def articles @articles end |
#id ⇒ Symbol
24 25 26 |
# File 'lib/term_utils/ap/parameter.rb', line 24 def id @id end |
#max_occurs ⇒ Integer
28 29 30 |
# File 'lib/term_utils/ap/parameter.rb', line 28 def max_occurs @max_occurs end |
#min_occurs ⇒ 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.
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.
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_articles ⇒ Array<Article>
Fetches all articles.
139 140 141 |
# File 'lib/term_utils/ap/parameter.rb', line 139 def fetch_articles @articles.collect(&:dup) end |
#fetch_flags ⇒ Array<Flag>
Fetches all flags.
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.
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.
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.
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.
83 84 85 |
# File 'lib/term_utils/ap/parameter.rb', line 83 def occur_bounded? (@max_occurs != nil) && @max_occurs.is_a?(Integer) end |