Class: Nginxtra::ConfigConverter::Token

Inherits:
Object
  • Object
show all
Defined in:
lib/nginxtra/config_converter.rb

Constant Summary collapse

KEYWORDS =
%w(break if return).freeze
TERMINAL_CHARACTERS =
["{", "}", ";"].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value = nil) ⇒ Token

Returns a new instance of Token.



127
128
129
130
131
# File 'lib/nginxtra/config_converter.rb', line 127

def initialize(value = nil)
  @instance = true if value
  @value = value || ""
  @ready = false
end

Instance Attribute Details

#valueObject (readonly)

Returns the value of attribute value.



125
126
127
# File 'lib/nginxtra/config_converter.rb', line 125

def value
  @value
end

Instance Method Details

#<<(c) ⇒ Object



168
169
170
171
172
# File 'lib/nginxtra/config_converter.rb', line 168

def <<(c)
  return space! if c =~ /\s/
  return terminal_character!(c) if TERMINAL_CHARACTERS.include? c
  @value << c
end

#block_end?Boolean

Returns:

  • (Boolean)


157
158
159
# File 'lib/nginxtra/config_converter.rb', line 157

def block_end?
  @value == "}"
end

#block_start?Boolean

Returns:

  • (Boolean)


153
154
155
# File 'lib/nginxtra/config_converter.rb', line 153

def block_start?
  @value == "{"
end

#end?Boolean

Returns:

  • (Boolean)


149
150
151
# File 'lib/nginxtra/config_converter.rb', line 149

def end?
  @value == ";"
end

#if?Boolean

Returns:

  • (Boolean)


133
134
135
# File 'lib/nginxtra/config_converter.rb', line 133

def if?
  @value == "if"
end

#if_end!Object



141
142
143
# File 'lib/nginxtra/config_converter.rb', line 141

def if_end!
  @value.gsub!(/\)$/, "")
end

#if_start!Object



137
138
139
# File 'lib/nginxtra/config_converter.rb', line 137

def if_start!
  @value.gsub!(/^\(/, "")
end

#instanceObject



161
162
163
164
165
166
# File 'lib/nginxtra/config_converter.rb', line 161

def instance
  raise Nginxtra::Error::ConvertFailed, "Whoops!" unless ready?
  token = Nginxtra::ConfigConverter::Token.new @value
  reset!
  token
end

#ready?Boolean

Returns:

  • (Boolean)


174
175
176
# File 'lib/nginxtra/config_converter.rb', line 174

def ready?
  @instance || @ready || terminal_character?
end

#terminal_character?Boolean

Returns:

  • (Boolean)


145
146
147
# File 'lib/nginxtra/config_converter.rb', line 145

def terminal_character?
  TERMINAL_CHARACTERS.include? @value
end

#to_line_startObject



178
179
180
181
182
183
184
# File 'lib/nginxtra/config_converter.rb', line 178

def to_line_start
  if KEYWORDS.include? @value
    "_#{@value}"
  else
    @value
  end
end

#to_sObject



186
187
188
189
190
191
192
# File 'lib/nginxtra/config_converter.rb', line 186

def to_s
  if @value =~ /^\d+$/
    @value
  else
    %("#{@value.gsub("\\") { "\\\\" }}")
  end
end