Class: TermUtils::AP::Syntax

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

Overview

Represents the argument list syntax. It holds a list of parameters and levels.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSyntax

Constructs a new Syntax.



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

def initialize
  @elements = []
end

Instance Attribute Details

#elementsArray<TermUtils::AP::Element>

Returns:



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

def elements
  @elements
end

Instance Method Details

#define_level(opts = {}, &block) ⇒ TermUtils::AP::Level

Creates and adds a new Level.

Parameters:

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

Options Hash (opts):

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

    Default value is `0`.

  • :max_occurs (Integer)

    Default value is `1`.

Returns:



63
64
65
66
67
68
# File 'lib/term_utils/ap/syntax.rb', line 63

def define_level(opts = {}, &block)
  new_level = TermUtils::AP::Level.new(opts)
  @elements << new_level
  block.call(new_level) if block
  new_level
end

#define_parameter(opts = {}, &block) ⇒ TermUtils::AP::Parameter

Creates and adds 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`.

Returns:



51
52
53
54
55
56
# File 'lib/term_utils/ap/syntax.rb', line 51

def define_parameter(opts = {}, &block)
  new_parameter = TermUtils::AP::Parameter.new(opts)
  @elements << new_parameter
  block.call(new_parameter) if block
  new_parameter
end

#fetch_elementsArray

Fetches all direct flagged parameters and levels.



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/term_utils/ap/syntax.rb', line 73

def fetch_elements
  flagged_elems = {}
  unflagged_params = []
  @elements.each do |e|
    if e.flags.empty?
      # Parameter
      unflagged_params << e
    else
      # Parameter or Level
      e.flags.each do |f|
        flagged_elems[f.to_s] = e
      end
    end
  end
  [flagged_elems, unflagged_params]
end

#fetch_flagged_elementsHash<String, TermUtils::AP::Element>

Fetches all direct flagged parameters and levels.

Returns:



91
92
93
94
95
96
97
98
99
# File 'lib/term_utils/ap/syntax.rb', line 91

def fetch_flagged_elements
  elems = {}
  @elements.each do |e|
    e.flags.each do |f|
      elems[f.to_s] = e
    end
  end
  elems
end

#fetch_unflagged_parametersArray<TermUtils::AP::Parameter>

Fetches all direct unflagged.

Returns:



102
103
104
105
106
107
108
109
# File 'lib/term_utils/ap/syntax.rb', line 102

def fetch_unflagged_parameters
  params = []
  @elements.each do |e|
    next unless e.is_a? TermUtils::AP::Parameter
    params << e if e.flags.empty?
  end
  params
end

#finalize!(opts = {}) ⇒ nil

Finalizes this one. Internal use.

Returns:

  • (nil)


41
42
43
44
# File 'lib/term_utils/ap/syntax.rb', line 41

def finalize!(opts = {})
  opts[:anonymous] = 0 unless opts.has_key? :anonymous
  @elements.each { |e| e.finalize!(opts) }
end

#initialize_dup(other) ⇒ Object

For dup method.



30
31
32
33
34
35
36
37
38
# File 'lib/term_utils/ap/syntax.rb', line 30

def initialize_dup(other)
  if other.elements
    @elements = []
    other.elements.each do |e|
      @elements << e.dup
    end
  end
  super
end