chunker 0.1.53 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +1 -1
- data/Rakefile +27 -42
- data/lib/chunker.rb +22 -24
- data/spec/chunker_spec.rb +41 -40
- metadata +27 -12
data/LICENSE
CHANGED
data/Rakefile
CHANGED
@@ -1,15 +1,14 @@
|
|
1
1
|
#!/usr/bin/env rake
|
2
2
|
#
|
3
|
-
# Chunker Rakefile
|
4
|
-
#
|
5
3
|
|
6
4
|
require 'rubygems'
|
7
5
|
require 'pathname'
|
8
6
|
|
9
7
|
require 'rake'
|
8
|
+
require 'rspec'
|
9
|
+
require 'rspec/core/rake_task'
|
10
10
|
require 'rake/packagetask'
|
11
11
|
require 'rake/gempackagetask'
|
12
|
-
require 'spec/rake/spectask'
|
13
12
|
require 'rubygems/installer'
|
14
13
|
require 'rubygems/uninstaller'
|
15
14
|
|
@@ -23,10 +22,10 @@ BASEDIR = Pathname.new( __FILE__ ).expand_path.dirname.relative_path_from( Pathn
|
|
23
22
|
TEXT_FILES = %w{ Rakefile README LICENSE }.collect {|f| BASEDIR + f }
|
24
23
|
|
25
24
|
SPECDIR = BASEDIR + 'spec'
|
26
|
-
SPEC_FILES = Pathname.glob( SPECDIR + '**/*_spec.rb' )
|
25
|
+
SPEC_FILES = Pathname.glob( SPECDIR + '**/*_spec.rb' )
|
27
26
|
|
28
27
|
LIBDIR = BASEDIR + 'lib'
|
29
|
-
LIB_FILES = Pathname.glob( LIBDIR + '**/*.rb')
|
28
|
+
LIB_FILES = Pathname.glob( LIBDIR + '**/*.rb')
|
30
29
|
|
31
30
|
RELEASE_FILES = TEXT_FILES + LIB_FILES + SPEC_FILES
|
32
31
|
|
@@ -54,24 +53,33 @@ end
|
|
54
53
|
######################################################################
|
55
54
|
|
56
55
|
PKG_NAME = 'chunker'
|
57
|
-
PKG_VERSION = find_pattern( LIBDIR + 'chunker.rb', /VERSION = ['"](
|
58
|
-
|
59
|
-
PKG_FILE_NAME = "#{PKG_NAME}-#{PKG_VERSION}.#{PKG_REVISION}"
|
56
|
+
PKG_VERSION = find_pattern( LIBDIR + 'chunker.rb', /VERSION = ['"](.+)['"]/ )
|
57
|
+
PKG_FILE_NAME = "#{PKG_NAME}-#{PKG_VERSION}"
|
60
58
|
|
61
59
|
|
62
60
|
######################################################################
|
63
61
|
### T A S K S
|
64
62
|
######################################################################
|
65
63
|
|
66
|
-
task :
|
64
|
+
task :test => 'test:spec'
|
65
|
+
task :default => :test
|
66
|
+
# task :default => [ :test, :package ]
|
67
67
|
|
68
68
|
|
69
|
-
###
|
69
|
+
### Tasks: testing via rspec
|
70
70
|
###
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
71
|
+
namespace :test do
|
72
|
+
desc 'Generate verbose and pretty output'
|
73
|
+
RSpec::Core::RakeTask.new( :spec ) do |task|
|
74
|
+
task.pattern = SPEC_FILES
|
75
|
+
task.rspec_opts = ['-b', '-fd', '-c']
|
76
|
+
end
|
77
|
+
|
78
|
+
desc 'Generate quiet non-colored plain-text output'
|
79
|
+
RSpec::Core::RakeTask.new( :quiet ) do |task|
|
80
|
+
task.pattern = SPEC_FILES
|
81
|
+
task.rspec_opts = ['-f', 'p']
|
82
|
+
end
|
75
83
|
end
|
76
84
|
|
77
85
|
|
@@ -87,46 +95,23 @@ end
|
|
87
95
|
### Task: Create gem from source
|
88
96
|
###
|
89
97
|
gem = Gem::Specification.new do |gem|
|
90
|
-
pkg_build = PKG_REVISION || 0
|
91
|
-
|
92
98
|
gem.summary = "A convenience library for parsing __END__ tokens consistently."
|
93
99
|
gem.name = PKG_NAME
|
94
|
-
gem.version =
|
100
|
+
gem.version = PKG_VERSION
|
95
101
|
gem.author = 'Mahlon E. Smith'
|
96
102
|
gem.email = 'mahlon@martini.nu'
|
97
103
|
gem.homepage = 'http://projects.martini.nu/ruby-modules/wiki/Chunker'
|
98
|
-
gem.rubyforge_project = 'mahlon'
|
99
104
|
gem.has_rdoc = true
|
105
|
+
gem.extra_rdoc_files = ['README']
|
106
|
+
gem.rdoc_options << '--main' << 'README'
|
107
|
+
|
100
108
|
|
101
109
|
gem.files = RELEASE_FILES.
|
102
110
|
collect {|f| f.relative_path_from(BASEDIR).to_s }
|
103
111
|
gem.test_files = SPEC_FILES.
|
104
112
|
collect {|f| f.relative_path_from(BASEDIR).to_s }
|
105
113
|
|
106
|
-
gem.description =
|
107
|
-
Ruby provides an automatic constant called DATA, which is an IO object
|
108
|
-
that references all text in the current file under an __END__ token.
|
109
|
-
|
110
|
-
I find it convenient to use the __END__ area to store all sorts of
|
111
|
-
stuff, rather than have to worry about distributing separate files.
|
112
|
-
|
113
|
-
The DATA constant is determined from whatever ruby believes $0 to be.
|
114
|
-
It doesn't work inside of other required libraries, so you'll see stuff
|
115
|
-
like this all the time:
|
116
|
-
|
117
|
-
END = File.open( __FILE__ ).read.split( /^__END__/, 2 ).last
|
118
|
-
|
119
|
-
It works, but it's more work than I want to do.
|
120
|
-
|
121
|
-
Chunker solves this by parsing __END__ tokens for you, and making it
|
122
|
-
available in the form of a 'DATA_END' constant. It installs this
|
123
|
-
constant into the class that includes Chunker, so you can use it again
|
124
|
-
and again, assuming you use a different file for each class.
|
125
|
-
|
126
|
-
It also automatically parses out other things that look like tokens, so
|
127
|
-
you can easily have multiple, distinct documents all embedded into the
|
128
|
-
__END__ block.
|
129
|
-
EOF
|
114
|
+
gem.description = "Embed arbitrary data and multiple, distinct documents within ruby files."
|
130
115
|
end
|
131
116
|
|
132
117
|
Rake::GemPackageTask.new( gem ) do |pkg|
|
data/lib/chunker.rb
CHANGED
@@ -1,10 +1,14 @@
|
|
1
|
-
|
1
|
+
# vim: set nosta noet ts=4 sw=4:
|
2
|
+
|
3
|
+
require 'strscan'
|
4
|
+
require 'stringio'
|
5
|
+
|
2
6
|
#
|
3
7
|
# Chunker: A convenience library for parsing __END__ tokens consistently.
|
4
8
|
#
|
5
9
|
# == Version
|
6
10
|
#
|
7
|
-
# $Id: chunker.rb
|
11
|
+
# $Id: chunker.rb,v 01a3332bfe0a 2011/01/22 03:41:16 mahlon $
|
8
12
|
#
|
9
13
|
# == Author
|
10
14
|
#
|
@@ -17,20 +21,14 @@
|
|
17
21
|
###
|
18
22
|
module Chunker
|
19
23
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
# SVN Revision
|
24
|
-
#
|
25
|
-
SVNRev = %q$Rev: 53 $
|
24
|
+
# VCS Revision
|
25
|
+
VCSRev = %q$Rev: 01a3332bfe0a $
|
26
26
|
|
27
|
-
#
|
28
|
-
|
29
|
-
SVNId = %q$Id: chunker.rb 53 2008-11-09 00:27:36Z mahlon $
|
27
|
+
# VCS Id
|
28
|
+
VCSId = %q$Id: chunker.rb,v 01a3332bfe0a 2011/01/22 03:41:16 mahlon $
|
30
29
|
|
31
30
|
# Package version
|
32
|
-
|
33
|
-
VERSION = '0.1'
|
31
|
+
VERSION = '1.0.0'
|
34
32
|
|
35
33
|
|
36
34
|
### Parser class for __END__ data blocks.
|
@@ -40,11 +38,9 @@ module Chunker
|
|
40
38
|
class DataParser
|
41
39
|
|
42
40
|
# The mark for a DATA block.
|
43
|
-
#
|
44
41
|
END_TOKEN = /^__END__\r?\n/
|
45
42
|
|
46
43
|
# The mark for a 'sub' block.
|
47
|
-
#
|
48
44
|
CHUNK_TOKEN = /^__([A-Z\_0-9]+)__\r?\n/
|
49
45
|
|
50
46
|
|
@@ -59,11 +55,14 @@ module Chunker
|
|
59
55
|
@scanner = StringScanner.new( end_string )
|
60
56
|
io.close
|
61
57
|
|
58
|
+
# put each chunk into its own constant
|
59
|
+
#
|
62
60
|
if @scanner.check_until( CHUNK_TOKEN )
|
63
|
-
# put each chunk into its own constant
|
64
61
|
self.extract_blocks
|
62
|
+
|
63
|
+
# no sub blocks, put the whole mess into DATA_END
|
64
|
+
#
|
65
65
|
else
|
66
|
-
# no sub blocks, put the whole mess into DATA_END
|
67
66
|
@klass.const_set( :DATA_END, StringIO.new( end_string ) )
|
68
67
|
end
|
69
68
|
end
|
@@ -93,20 +92,20 @@ module Chunker
|
|
93
92
|
else
|
94
93
|
label = @scanner[1]
|
95
94
|
|
95
|
+
# Pull the next token text out of the data, set up the next pass
|
96
|
+
#
|
96
97
|
if data = @scanner.scan_until( CHUNK_TOKEN )
|
97
|
-
|
98
|
-
#
|
99
|
-
data = data[ 0, data.length - @scanner[0].length ]
|
98
|
+
data = data[ 0, data.length - @scanner[0].length ]
|
100
99
|
@scanner.pos = self.next_position
|
100
|
+
|
101
|
+
# No additional blocks
|
102
|
+
#
|
101
103
|
else
|
102
|
-
# No additional blocks
|
103
|
-
#
|
104
104
|
data = @scanner.rest
|
105
105
|
end
|
106
106
|
end
|
107
107
|
|
108
108
|
# Add the IO constant to the class that included me.
|
109
|
-
#
|
110
109
|
@klass.const_set( "DATA_#{label}".to_sym, StringIO.new( data ) )
|
111
110
|
end
|
112
111
|
end
|
@@ -126,7 +125,6 @@ module Chunker
|
|
126
125
|
def self.included( klass )
|
127
126
|
# klass.instance_eval{ __FILE__ } awww, nope.
|
128
127
|
# __FILE__ won't work here, so we find the filename via caller().
|
129
|
-
#
|
130
128
|
io = File.open( caller(1).last.sub(/:.*?$/, ''), 'r' )
|
131
129
|
|
132
130
|
DataParser.new( klass, io )
|
data/spec/chunker_spec.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
|
1
|
+
# vim: set nosta noet ts=4 sw=4 ft=rspec:
|
2
2
|
|
3
3
|
BEGIN {
|
4
4
|
require 'pathname'
|
@@ -8,9 +8,8 @@ BEGIN {
|
|
8
8
|
$LOAD_PATH.unshift( libdir ) unless $LOAD_PATH.include?( libdir )
|
9
9
|
}
|
10
10
|
|
11
|
+
require 'rspec'
|
11
12
|
require 'chunker'
|
12
|
-
require 'rubygems'
|
13
|
-
require 'spec'
|
14
13
|
|
15
14
|
ENDSTUFF = <<ENDSTUFF
|
16
15
|
Stuff within the end block.
|
@@ -65,53 +64,55 @@ __HURGADURGA__
|
|
65
64
|
EO_FILE_TEXT
|
66
65
|
|
67
66
|
|
68
|
-
describe Chunker
|
67
|
+
describe Chunker do
|
69
68
|
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
69
|
+
describe Chunker::DataParser do
|
70
|
+
|
71
|
+
it "doesn't include content above the __END__ token" do
|
72
|
+
klass = Class.new
|
73
|
+
dp = Chunker::DataParser.new( klass, StringIO.new( FILE_TEXT_MULTIPLE ))
|
74
|
+
dp.instance_variable_get( :@scanner ).string.
|
75
|
+
should_not =~ /This is stuff we shouldn't see/
|
76
|
+
end
|
76
77
|
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
78
|
+
it "doesn't contain the __END__ token itself" do
|
79
|
+
klass = Class.new
|
80
|
+
dp = Chunker::DataParser.new( klass, StringIO.new( FILE_TEXT ))
|
81
|
+
dp.instance_variable_get( :@scanner ).string.should_not =~ /^__END__/
|
82
|
+
end
|
81
83
|
end
|
82
|
-
end
|
83
84
|
|
84
85
|
|
85
|
-
|
86
|
+
context 'when included from another class' do
|
86
87
|
|
87
|
-
|
88
|
-
|
89
|
-
|
88
|
+
it "has all content in DATA_END if there are no sub blocks" do
|
89
|
+
File.stub!( :open ).and_return( StringIO.new( FILE_TEXT ))
|
90
|
+
klass = Class.new { include Chunker }
|
90
91
|
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
92
|
+
klass.constants.should_not include( 'DATA_POOP' )
|
93
|
+
klass.constants.should_not include( 'DATA_HURRRRG' )
|
94
|
+
klass.constants.should_not include( 'DATA_HURGADURGA' )
|
95
|
+
klass.constants.should include( 'DATA_END' )
|
96
|
+
end
|
96
97
|
|
97
|
-
|
98
|
-
|
99
|
-
|
98
|
+
it "separates data sub blocks into individual constants" do
|
99
|
+
File.stub!( :open ).and_return( StringIO.new( FILE_TEXT_MULTIPLE ))
|
100
|
+
klass = Class.new { include Chunker }
|
100
101
|
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
102
|
+
klass.constants.should include( 'DATA_END' )
|
103
|
+
klass.constants.should include( 'DATA_POOP' )
|
104
|
+
klass.constants.should include( 'DATA_HURRRRG' )
|
105
|
+
klass.constants.should include( 'DATA_HURGADURGA' )
|
106
|
+
end
|
106
107
|
|
107
|
-
|
108
|
-
|
109
|
-
|
108
|
+
it "has IO constants that contain the data block contents" do
|
109
|
+
File.stub!( :open ).and_return( StringIO.new( FILE_TEXT_MULTIPLE ))
|
110
|
+
klass = Class.new { include Chunker }
|
110
111
|
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
112
|
+
klass.const_get( :DATA_END ).read.chomp.should == ENDSTUFF
|
113
|
+
klass.const_get( :DATA_POOP ).read.chomp.should == POOP
|
114
|
+
klass.const_get( :DATA_HURRRRG ).read.chomp.should == HURRRRG
|
115
|
+
klass.const_get( :DATA_HURGADURGA ).read.chomp.should == HURGADURGA
|
116
|
+
end
|
115
117
|
end
|
116
118
|
end
|
117
|
-
|
metadata
CHANGED
@@ -1,7 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: chunker
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
hash: 23
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
- 0
|
10
|
+
version: 1.0.0
|
5
11
|
platform: ruby
|
6
12
|
authors:
|
7
13
|
- Mahlon E. Smith
|
@@ -9,18 +15,18 @@ autorequire:
|
|
9
15
|
bindir: bin
|
10
16
|
cert_chain: []
|
11
17
|
|
12
|
-
date:
|
18
|
+
date: 2011-01-22 00:00:00 -08:00
|
13
19
|
default_executable:
|
14
20
|
dependencies: []
|
15
21
|
|
16
|
-
description:
|
22
|
+
description: Embed arbitrary data and multiple, distinct documents within ruby files.
|
17
23
|
email: mahlon@martini.nu
|
18
24
|
executables: []
|
19
25
|
|
20
26
|
extensions: []
|
21
27
|
|
22
|
-
extra_rdoc_files:
|
23
|
-
|
28
|
+
extra_rdoc_files:
|
29
|
+
- README
|
24
30
|
files:
|
25
31
|
- Rakefile
|
26
32
|
- README
|
@@ -29,29 +35,38 @@ files:
|
|
29
35
|
- spec/chunker_spec.rb
|
30
36
|
has_rdoc: true
|
31
37
|
homepage: http://projects.martini.nu/ruby-modules/wiki/Chunker
|
32
|
-
|
33
|
-
rdoc_options: []
|
38
|
+
licenses: []
|
34
39
|
|
40
|
+
post_install_message:
|
41
|
+
rdoc_options:
|
42
|
+
- --main
|
43
|
+
- README
|
35
44
|
require_paths:
|
36
45
|
- lib
|
37
46
|
required_ruby_version: !ruby/object:Gem::Requirement
|
47
|
+
none: false
|
38
48
|
requirements:
|
39
49
|
- - ">="
|
40
50
|
- !ruby/object:Gem::Version
|
51
|
+
hash: 3
|
52
|
+
segments:
|
53
|
+
- 0
|
41
54
|
version: "0"
|
42
|
-
version:
|
43
55
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
56
|
+
none: false
|
44
57
|
requirements:
|
45
58
|
- - ">="
|
46
59
|
- !ruby/object:Gem::Version
|
60
|
+
hash: 3
|
61
|
+
segments:
|
62
|
+
- 0
|
47
63
|
version: "0"
|
48
|
-
version:
|
49
64
|
requirements: []
|
50
65
|
|
51
|
-
rubyforge_project:
|
52
|
-
rubygems_version: 1.3.
|
66
|
+
rubyforge_project:
|
67
|
+
rubygems_version: 1.3.7
|
53
68
|
signing_key:
|
54
|
-
specification_version:
|
69
|
+
specification_version: 3
|
55
70
|
summary: A convenience library for parsing __END__ tokens consistently.
|
56
71
|
test_files:
|
57
72
|
- spec/chunker_spec.rb
|