Class: Parameter

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

Defined Under Namespace

Classes: SyntaxError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeParameter

Returns a new instance of Parameter.



5
6
7
# File 'lib/model/parameter.rb', line 5

def initialize
  @tokens = []
end

Instance Attribute Details

#documentationObject

Returns the value of attribute documentation.



3
4
5
# File 'lib/model/parameter.rb', line 3

def documentation
  @documentation
end

Instance Method Details

#<=>(other) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/model/parameter.rb', line 44

def <=>(other)
  if (self.is_optional? && other.is_optional?) || (self.is_required? && other.is_required?)
    return self.name <=> other.name
  elsif self.is_optional? && other.is_required?
    return 1
  else
    return -1
  end
end

#add(token) ⇒ Object

Raises:



23
24
25
26
27
28
29
30
# File 'lib/model/parameter.rb', line 23

def add(token)
  # A parameter never starts with a newline token, so skip that one
  return if @tokens.empty? && token.type == :NEWLINE
  # Raise a syntax error if the parameter starts with a comma.
  raise SyntaxError, "Syntax error: Expected a parameter definition, found comma on line #{token.line}, column #{token.column}" if @tokens.empty? && token.type == :COMMA

  @tokens << token
end

#columnObject



40
41
42
# File 'lib/model/parameter.rb', line 40

def column
  @tokens.first.column
end

#is_optional?Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/model/parameter.rb', line 9

def is_optional?
  @tokens.any? { |token| token.type == :EQUALS }
end

#is_required?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/model/parameter.rb', line 13

def is_required?
  !is_optional?
end

#lineObject



36
37
38
# File 'lib/model/parameter.rb', line 36

def line
  @tokens.first.line
end

#nameObject



17
18
19
20
21
# File 'lib/model/parameter.rb', line 17

def name
  @tokens.select do |token|
    token.type == :VARIABLE
  end.first.value
end

#tokensObject



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

def tokens
  strip_newlines(@tokens)
end