Class: ParameterList

Inherits:
Object
  • Object
show all
Defined in:
lib/model/parameter_list.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tokens, start_index, end_index) ⇒ ParameterList

Returns a new instance of ParameterList.



6
7
8
9
# File 'lib/model/parameter_list.rb', line 6

def initialize(tokens, start_index, end_index)
  @tokens, @start_index, @end_index = tokens, start_index, end_index
  @errors = []
end

Instance Attribute Details

#end_indexObject (readonly)

Returns the value of attribute end_index.



4
5
6
# File 'lib/model/parameter_list.rb', line 4

def end_index
  @end_index
end

#errorsObject (readonly)

Returns the value of attribute errors.



4
5
6
# File 'lib/model/parameter_list.rb', line 4

def errors
  @errors
end

#start_indexObject (readonly)

Returns the value of attribute start_index.



4
5
6
# File 'lib/model/parameter_list.rb', line 4

def start_index
  @start_index
end

Instance Method Details

#each(&block) ⇒ Object



15
16
17
18
19
# File 'lib/model/parameter_list.rb', line 15

def each(&block)
  parameters.each do |parameter|
    yield parameter
  end
end

#optional_parametersObject



21
22
23
# File 'lib/model/parameter_list.rb', line 21

def optional_parameters
  parameters.select(&:is_optional?)
end

#required_parametersObject



25
26
27
# File 'lib/model/parameter_list.rb', line 25

def required_parameters
  parameters.select(&:is_required?)
end

#sortObject



11
12
13
# File 'lib/model/parameter_list.rb', line 11

def sort
  parameters.sort
end

#validateObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/model/parameter_list.rb', line 29

def validate
  optional_parameter_found = false

  parameters.each do |parameter|
    if parameter.is_required? && optional_parameter_found
      errors << {
        :message => "Required parameter #{parameter.name} should be specified before optional parameters",
        :line    => parameter.line,
        :column  => parameter.column
      }
    elsif parameter.is_optional?
      optional_parameter_found = true
    end
  end

  validate_alphabetical_order(required_parameters)
  validate_alphabetical_order(optional_parameters)

  errors.empty?
end