Class: Regenerate::ParsedRegenerateCommentLine

Inherits:
Object
  • Object
show all
Defined in:
lib/regenerate/web-page.rb

Overview

An object which matches the regex used to identify Regenerate comment line commands (Note, however, a parsed line may match the regex, but if it doesn’t have at least one of a comment start or a comment end and at least one of a section start or a section end, it will be assumed that it is a line which was not intended to be parsed as a Regenerate command.)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(line, match) ⇒ ParsedRegenerateCommentLine

Returns a new instance of ParsedRegenerateCommentLine.



258
259
260
261
262
263
264
265
266
267
268
# File 'lib/regenerate/web-page.rb', line 258

def initialize(line, match)
  @hasCommentStart = match[1] != ""
  @sectionStart = match[2] != ""
  @isInstanceVar = match[4] != ""
  @name = match[3]
  @value = match[6]
  @sectionEnd = match[7] != ""
  @hasCommentEnd = match[8] != ""
  @line = line
  @isEmptySection = @sectionStart && @sectionEnd
end

Instance Attribute Details

#hasCommentEndObject (readonly)

Does this command include the end of an HTML comment?



251
252
253
# File 'lib/regenerate/web-page.rb', line 251

def hasCommentEnd
  @hasCommentEnd
end

#hasCommentStartObject (readonly)

Does this command include the start of an HTML comment?



250
251
252
# File 'lib/regenerate/web-page.rb', line 250

def hasCommentStart
  @hasCommentStart
end

#isEmptySectionObject (readonly)

Does this represent an empty section, because it starts and ends the same section?



254
255
256
# File 'lib/regenerate/web-page.rb', line 254

def isEmptySection
  @isEmptySection
end

#isInstanceVarObject (readonly)

Is there an associated page object instance variable?



249
250
251
# File 'lib/regenerate/web-page.rb', line 249

def isInstanceVar
  @isInstanceVar
end

#lineObject (readonly)

The full text line matched against



248
249
250
# File 'lib/regenerate/web-page.rb', line 248

def line
  @line
end

#nameObject (readonly)

The instance variable name (@something) or special command name (“ruby” or “class”)



255
256
257
# File 'lib/regenerate/web-page.rb', line 255

def name
  @name
end

#sectionEndObject (readonly)

Does this command include a section start indicator, i.e. “]” ?



253
254
255
# File 'lib/regenerate/web-page.rb', line 253

def sectionEnd
  @sectionEnd
end

#sectionStartObject (readonly)

Does this command include a section start indicator, i.e. “[” ?



252
253
254
# File 'lib/regenerate/web-page.rb', line 252

def sectionStart
  @sectionStart
end

#valueObject (readonly)

The optional value associated with a special command



256
257
258
# File 'lib/regenerate/web-page.rb', line 256

def value
  @value
end

Instance Method Details

#checkIsValidObject

only call this method if isRegenerateCommentLine returns true - in other words, if it looks like it was intended to be a Regenerate comment line command, check that it is valid.



297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
# File 'lib/regenerate/web-page.rb', line 297

def checkIsValid
  # The "name" value has to be an instance variable name or "ruby" or "class"
  if !@isInstanceVar and !["ruby", "class"].include?(@name)
    raiseParseException("Unknown section name #{@name.inspect}")
  end
  # An empty section has to be a self-contained comment line
  if @isEmptySection and (!@hasCommentStart && !@hasCommentEnd)
    raiseParseException("Empty section, but is not a closed comment")
  end
  # If it's not a section start, it has to be a section end, so there has to be a comment end
  if !@sectionStart && !@hasCommentEnd
    raiseParseException("End of section in comment start")
  end
  # If it's not a section end, it has to be a section start, so there has to be a comment start
  if !@sectionEnd && !@hasCommentStart
    raiseParseException("Start of section in comment end")
  end
  # Empty Ruby page components aren't allowed.
  if (@sectionStart && @sectionEnd) && isRuby
    raiseParseException("Empty ruby section")
  end
end

#instanceVarNameObject

The name of the associated instance variable (assuming there is one)



286
287
288
# File 'lib/regenerate/web-page.rb', line 286

def instanceVarName
  return @name
end

#isRegenerateCommentLineObject

Is this line recognised as a Regenerate comment line command?



276
277
278
# File 'lib/regenerate/web-page.rb', line 276

def isRegenerateCommentLine
  return (@hasCommentStart || @hasCommentEnd) && (@sectionStart || @sectionEnd)
end

#isRubyObject

Does this command start a Ruby page component (because it has special command name “ruby”)?



281
282
283
# File 'lib/regenerate/web-page.rb', line 281

def isRuby
  !@isInstanceVar && @name == "ruby"
end

#raiseParseException(message) ⇒ Object

Raise a parse exception due to an error within this command line

Raises:



291
292
293
# File 'lib/regenerate/web-page.rb', line 291

def raiseParseException(message)
  raise ParseException.new("Error parsing line #{@line.inspect}: #{message}")
end

#to_sObject

Reconstruct a line which would re-parse the same (but possibly with reduced whitespace)



271
272
273
# File 'lib/regenerate/web-page.rb', line 271

def to_s
  "#{@hasCommentStart ? "<!-- ":""}#{@sectionStart ? "[ ":""}#{@isInstanceVar ? "@ ":""}#{@name.inspect}#{@value ? " "+@value:""}#{@sectionEnd ? " ]":""}#{@hasCommentEnd ? " -->":""}"
end