Class: Chunker::DataParser

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

Overview

Parser class for __END__ data blocks. Find each __TOKEN__ within the __END__, and put each into a DATA_TOKEN constant within the namespace that included us.

Constant Summary collapse

END_TOKEN =

The mark for a DATA block.

/^__END__\r?\n/
CHUNK_TOKEN =

The mark for a ‘sub’ block.

/^__([A-Z\_0-9]+)__\r?\n/

Instance Method Summary collapse

Constructor Details

#initialize(klass, io) ⇒ DataParser

Constructor: Given a klass and an io to the class file, extract the data blocks and install constants.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/chunker.rb', line 50

def initialize( klass, io )
	io.open if io.closed?
	end_string = io.read.split( END_TOKEN, 2 ).last

	@klass   = klass
	@scanner = StringScanner.new( end_string )
	io.close

	# put each chunk into its own constant
	#
	if @scanner.check_until( CHUNK_TOKEN )
		self.extract_blocks

	# no sub blocks, put the whole mess into DATA_END
	#
	else
		@klass.const_set( :DATA_END, StringIO.new( end_string ) )
	end
end