fun_with_templates 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. checksums.yaml +15 -0
  2. data/.document +5 -0
  3. data/CHANGELOG.markdown +6 -0
  4. data/Gemfile +18 -0
  5. data/LICENSE.txt +20 -0
  6. data/README.rdoc +67 -0
  7. data/Rakefile +45 -0
  8. data/VERSION +1 -0
  9. data/lib/fun_with/core_extensions/kernel.rb +18 -0
  10. data/lib/fun_with/templates/directory_builder.rb +36 -0
  11. data/lib/fun_with/templates/filename_var_data.rb +42 -0
  12. data/lib/fun_with/templates/template_evaluator.rb +307 -0
  13. data/lib/fun_with_templates.rb +4 -0
  14. data/test/helper.rb +57 -0
  15. data/test/templates/00/a.rb.template +7 -0
  16. data/test/templates/00/dir/subdir/style.css +4 -0
  17. data/test/templates/00/seqfiles/page%0000i%.html.template +12 -0
  18. data/test/templates/00/seqfiles/page%j%.html.template +12 -0
  19. data/test/templates/00/seqfiles/page_about_%critter_name%.html.template +15 -0
  20. data/test/templates/01/%string.length%/%string%-%string.length%.nontemplate +1 -0
  21. data/test/templates/01/%string.length%/%string%-%string.length%.txt.template +1 -0
  22. data/test/templates/02/%class%.rb.template +6 -0
  23. data/test/templates/03/coordinates.%i%-%j%-%k%.html.template +14 -0
  24. data/test/templates/03/dir%0000i%/dir%0000j%/file%0000k%.py.template +1 -0
  25. data/test/templates/03/index.html.template +22 -0
  26. data/test/templates/epf/book/afterword.markdown +4 -0
  27. data/test/templates/epf/book/chapter-%0000chapter%.markdown.template +4 -0
  28. data/test/templates/epf/book/cover.xhtml.template +13 -0
  29. data/test/templates/epf/book/foreword.markdown +6 -0
  30. data/test/templates/epf/book/images/cover.png +0 -0
  31. data/test/templates/epf/book/stylesheets/stylesheet.css +2 -0
  32. data/test/templates/epf/book/title_page.markdown.template +5 -0
  33. data/test/templates/epf/notes/character.%character.name_for_file%.markdown.template +15 -0
  34. data/test/templates/epf/notes/images/cover.png +0 -0
  35. data/test/templates/epf/notes/stylesheets/stylesheet.css +2 -0
  36. data/test/templates/epf/settings/actions/local_action.rb.example +14 -0
  37. data/test/templates/epf/settings/config.rb.template +52 -0
  38. data/test/templates/epf/settings/htmlizers.rb +83 -0
  39. data/test/templates/epf/settings/wordcount.template +6 -0
  40. data/test/test_directory_builder.rb +24 -0
  41. data/test/test_filename_var_data.rb +51 -0
  42. data/test/test_fun_with_templates.rb +21 -0
  43. data/test/test_parse_filename_vars.rb +25 -0
  44. data/test/test_string_templates.rb +14 -0
  45. data/test/test_write_example_templates.rb +149 -0
  46. metadata +202 -0
@@ -0,0 +1,51 @@
1
+ require 'helper'
2
+
3
+ class TestFilenameVarData < FunWith::Templates::TestCase
4
+ context "testing filename substitution" do
5
+ should "fill in filename" do
6
+ filename = "hello-%name%.html"
7
+ var_data = FunWith::Templates::FilenameVarData.new( :name )
8
+ sub = "barry"
9
+
10
+ filename = var_data.fill_in_path( filename, sub )
11
+
12
+ assert_equal "hello-barry.html", filename
13
+ end
14
+
15
+ should "fill in filename via callable method" do
16
+ filename = "hello-%person.name%.html"
17
+ var_data = FunWith::Templates::FilenameVarData.new( :person, :name )
18
+ mod = Module.new do
19
+ def name
20
+ "barry"
21
+ end
22
+ end
23
+
24
+ sub = Object.new.extend( mod )
25
+
26
+ filename = var_data.fill_in_path( filename, sub )
27
+ assert_equal "hello-barry.html", filename
28
+ end
29
+
30
+ should "fill in filename via hash key" do
31
+ filename = "hello-%person.first_name%_%person.last_name%.html"
32
+ var_data1 = FunWith::Templates::FilenameVarData.new( :person, :first_name )
33
+ var_data2 = FunWith::Templates::FilenameVarData.new( :person, :last_name )
34
+ person = { :first_name => "barry", :last_name => "wendell" }
35
+
36
+ filename = var_data1.fill_in_path( filename, person )
37
+ filename = var_data2.fill_in_path( filename, person )
38
+
39
+ assert_equal "hello-barry_wendell.html", filename
40
+ end
41
+
42
+ should "fill in multiple variables" do
43
+ filename = "coordinates-%i%-%j%-%k%.html"
44
+
45
+ var_data = [ FunWith::Templates::FilenameVarData.new(:i), FunWith::Templates::FilenameVarData.new(:j), FunWith::Templates::FilenameVarData.new(:k) ]
46
+ filename = FunWith::Templates::FilenameVarData.fill_in_path( filename, var_data, {:i => 3, :j => 2, :k => 1} )
47
+
48
+ assert_equal( "coordinates-3-2-1.html", filename )
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,21 @@
1
+ require 'helper'
2
+
3
+ class TestFunWithTemplates < FunWith::Templates::TestCase
4
+ context "basics" do
5
+ should "accept the reality of FunWith::Templates" do
6
+ assert defined?( FunWith ), "FunWith module not defined"
7
+ assert defined?( FunWith::Templates ), "FunWith::Templates not defined."
8
+ end
9
+
10
+ should "properly root FunWith::Templates" do
11
+ assert FunWith::Templates.respond_to?(:root)
12
+ assert_equal FunWith::Templates.root, __FILE__.fwf_filepath.dirname.up
13
+ end
14
+
15
+ should "properly version FunWith::Templates" do
16
+ assert FunWith::Templates.respond_to?( :version )
17
+ assert FunWith::Templates.root( "VERSION" ).file?
18
+ assert_equal FunWith::Templates.root( "VERSION" ).read, FunWith::Templates.version.to_s
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,25 @@
1
+ require 'helper'
2
+
3
+ class TestParseFilenameVars < FunWith::Templates::TestCase
4
+ context "parsing filename vars" do
5
+ setup do
6
+ end
7
+
8
+ should "successfully parse" do
9
+ vars = { :chapter => {:number => 3, :title => "Brokeback Fountain"}}
10
+ @template = FunWith::Templates::TemplateEvaluator.new( nil, vars )
11
+ assert_kind_of FunWith::Templates::TemplateEvaluator, @template
12
+ num, title = @template.parse_filename_vars("chap-%0000chapter.number%-%chapter.title%.html")
13
+ assert_not_nil num
14
+ assert_not_nil title
15
+ assert_equal "0000", num.num_format
16
+ assert_equal :chapter, num.name
17
+ assert_equal :number, num.method_to_call
18
+
19
+ assert_equal :chapter, title.name
20
+ assert_equal :title, title.method_to_call
21
+ assert_nil title.num_format
22
+
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,14 @@
1
+ require 'helper'
2
+
3
+ class TestStringTemplates < FunWith::Templates::TestCase
4
+ context "testing template_handler" do
5
+ should "evaluate erb in a string" do
6
+ sample = "(<%= Time.now %>): <%= @var_is_set %>"
7
+ result = FunWith::Templates::TemplateEvaluator.new( sample, var_is_set: "true" ).result
8
+
9
+ assert_match /true/, result
10
+ time_string = /\((.*)\)/.match(result)[1]
11
+ assert_kind_of Time, Time.parse( time_string )
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,149 @@
1
+ require 'helper'
2
+
3
+ class String
4
+ def epf_underscorize
5
+ self.downcase.gsub(/\s+/,"_").gsub(/[\W]/,"")
6
+ end
7
+ end
8
+
9
+ class TestFunWithTemplates < FunWith::Templates::TestCase
10
+ context "run test templates and validate results" do
11
+ setup do
12
+ @templates_dir = FunWith::Templates.root("test", "templates")
13
+ @templates_dir.glob("*").select(&:directory?).each do |path|
14
+ instance_variable_set( "@template_#{path.basename}", path )
15
+ end
16
+
17
+ assert( @template_00.is_a?( FunWith::Files::FilePath ) )
18
+ assert( @template_01.is_a?( FunWith::Files::FilePath ) )
19
+ assert( @template_02.is_a?( FunWith::Files::FilePath ) )
20
+ assert( @template_03.is_a?( FunWith::Files::FilePath ) )
21
+ assert( @template_epf.is_a?( FunWith::Files::FilePath ) )
22
+
23
+ @dest = nil
24
+ end
25
+
26
+ teardown do
27
+ if @dest.is_a?(FunWith::Files::FilePath) && @dest.directory?
28
+ puts "removing temp directory #{@dest}" if FunWith::Templates::TemplateEvaluator::VERBOSE
29
+ FileUtils.rm_rf( @dest )
30
+ end
31
+ end
32
+
33
+ should "validate results of template00" do
34
+ vars = { :module => "Racer",
35
+ :class => "Car",
36
+ :args => ["size", "color", "speed"],
37
+ :i => (1..20),
38
+ :j => (0..19),
39
+ :critter_name => %w(wombat mollusc squid cat gerbil),
40
+ :critters => {
41
+ "wombat" => { :attributes => ["cunning", "lycanthropean"] },
42
+ "mollusc" => { :attributes => ["edible", "seaworthy"] },
43
+ "squid" => { :attributes => ["multifaceted", "multitentacled"] },
44
+ "cat" => { :attributes => ["glorious", "tailtwitchy"] },
45
+ "gerbil" => { :attributes => ["potato-shaped", "nervous"] }
46
+ }
47
+ }
48
+
49
+ @dest = FunWith::Templates::TemplateEvaluator.write( @template_00, :temp, vars )
50
+ assert @dest.directory?
51
+ assert @dest.join( "dir", "subdir" ).directory?
52
+ a_rb = @dest.join( "a.rb" )
53
+ assert a_rb.file?
54
+ assert_equal 1, a_rb.grep( /size \* color \* speed/ ).length
55
+ style_css = @dest.join( "dir", "subdir", "style.css" )
56
+ assert style_css.file?
57
+ assert_equal 1, style_css.grep( /font-weight/ ).length
58
+ seqdir = @dest.join("seqfiles")
59
+ assert_equal 20, seqdir.glob("page????.html").length
60
+ assert_equal 5, seqdir.glob("page_about_*.html").length
61
+ assert_equal 3, seqdir.join("page_about_mollusc.html").grep(/mollusc/).length
62
+ assert_equal 1, seqdir.join("page_about_cat.html").grep(/tailtwitchy/).length
63
+ assert_equal 1, seqdir.join("page_about_cat.html").grep(/glorious/).length
64
+ assert_file_contents seqdir.join("page_about_cat.html"), /tailtwitchy/
65
+ assert_file_contents seqdir.join("page_about_cat.html"), /glorious/
66
+ end
67
+
68
+ should "validate results of template_01" do
69
+ vars = { :string => %w(1 10 100 1000) }
70
+
71
+ @dest = FunWith::Templates::TemplateEvaluator.write( @template_01, :temp, vars )
72
+
73
+ assert_equal( 12, @dest.glob(:all).length )
74
+
75
+ for str in vars[:string]
76
+ txt = @dest.join( str.length.to_s, "#{str}-#{str.length}.txt" )
77
+ no_template = @dest.join( str.length.to_s, "#{str}-#{str.length}.nontemplate" )
78
+ assert_file txt
79
+ assert_file no_template
80
+ assert_file_contents no_template, /Some strings have/
81
+ assert_file_contents txt, "string #{str} has length #{str.length}"
82
+ end
83
+ end
84
+
85
+ should "build templates/epf" do
86
+ vars = epf_template_vars
87
+ dest = FunWith::Templates::TemplateEvaluator.write( @template_epf, :temp, vars )
88
+
89
+ afterword = dest.join("book", "afterword.markdown")
90
+ assert_file afterword
91
+
92
+
93
+ ch20 = dest.join( "book", "chapter-0020.markdown" )
94
+ assert_file ch20
95
+ assert_equal 1, ch20.grep( /^Chapter 20$/ ).length
96
+
97
+ ch01 = dest.join( "book", "chapter-0001.markdown" )
98
+ assert_file ch01
99
+ assert_equal 1, ch01.grep( /^Chapter 1$/ ).length
100
+
101
+ cover_page = dest.join( "book", "cover.xhtml" )
102
+ assert_file cover_page
103
+ assert_equal 2, cover_page.grep( /Spittleman/ ).length
104
+
105
+ config = dest.join( "settings", "config.rb" )
106
+ assert config.file?
107
+
108
+ assert_equal 1, config.grep( /36ce67/ ).length
109
+
110
+ notes_css = dest.join( "notes", "stylesheets", "stylesheet.css" )
111
+ assert_file_not_empty notes_css
112
+ end
113
+
114
+ should "skip a file in templates/epf without character data" do
115
+ vars = epf_template_vars
116
+ vars[:character] = nil
117
+ dest = FunWith::Templates::TemplateEvaluator.write( @template_epf, :temp, vars )
118
+
119
+ assert_equal 0, dest.join("notes").glob("character.*.markdown").length
120
+ end
121
+
122
+
123
+ should "build template_03, with multiple filename variables" do
124
+ vars = { :i => 0..1, :j => 0..1, :k => 0..1 }
125
+
126
+ dest = FunWith::Templates::TemplateEvaluator.write( @template_03, :temp, vars )
127
+
128
+ entries = dest.glob( "coordinates.*.html" )
129
+
130
+ for entry in entries
131
+ assert_file entry
132
+ end
133
+ assert_equal( 8, dest.glob( "coordinates.*.html" ).length )
134
+
135
+ index = dest.join( "index.html" )
136
+ assert_equal( 8, index.grep(/<li>/).length )
137
+
138
+ for combo in [ [0,0,0], [0,0,1], [0,1,0], [0,1,1], [1,0,0], [1,0,1], [1,1,0], [1,1,1] ]
139
+ fil = dest.join("coordinates.#{combo[0]}-#{combo[1]}-#{combo[2]}.html")
140
+ assert_file fil
141
+ assert_equal( 2, fil.grep( /\(#{combo[0]}, #{combo[1]}, #{combo[2]}\)/ ).length )
142
+
143
+ pyfile = dest.join( "dir000#{combo[0]}", "dir000#{combo[1]}", "file000#{combo[2]}.py" )
144
+ assert_file pyfile
145
+ assert_equal 1, pyfile.grep( /#{combo[0]}-#{combo[1]}-#{combo[2]}/ ).length
146
+ end
147
+ end
148
+ end
149
+ end
metadata ADDED
@@ -0,0 +1,202 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fun_with_templates
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ platform: ruby
6
+ authors:
7
+ - Bryce Anderson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-04-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: fun_with_gems
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '0.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '0.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: fun_with_string_colors
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: shoulda
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '3.5'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '3.5'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rdoc
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '3.12'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '3.12'
69
+ - !ruby/object:Gem::Dependency
70
+ name: bundler
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: '1.5'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ version: '1.5'
83
+ - !ruby/object:Gem::Dependency
84
+ name: jeweler
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ~>
88
+ - !ruby/object:Gem::Version
89
+ version: '2.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ~>
95
+ - !ruby/object:Gem::Version
96
+ version: '2.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: debugger
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ~>
102
+ - !ruby/object:Gem::Version
103
+ version: '1.6'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ~>
109
+ - !ruby/object:Gem::Version
110
+ version: '1.6'
111
+ - !ruby/object:Gem::Dependency
112
+ name: fun_with_testing
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ~>
116
+ - !ruby/object:Gem::Version
117
+ version: '0.0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ~>
123
+ - !ruby/object:Gem::Version
124
+ version: '0.0'
125
+ description: A simple approach to 'fill in the blank' file templates, which may be
126
+ useful for some tasks.
127
+ email: keeputahweird@gmail.com
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files:
131
+ - LICENSE.txt
132
+ - README.rdoc
133
+ files:
134
+ - .document
135
+ - CHANGELOG.markdown
136
+ - Gemfile
137
+ - LICENSE.txt
138
+ - README.rdoc
139
+ - Rakefile
140
+ - VERSION
141
+ - lib/fun_with/core_extensions/kernel.rb
142
+ - lib/fun_with/templates/directory_builder.rb
143
+ - lib/fun_with/templates/filename_var_data.rb
144
+ - lib/fun_with/templates/template_evaluator.rb
145
+ - lib/fun_with_templates.rb
146
+ - test/helper.rb
147
+ - test/templates/00/a.rb.template
148
+ - test/templates/00/dir/subdir/style.css
149
+ - test/templates/00/seqfiles/page%0000i%.html.template
150
+ - test/templates/00/seqfiles/page%j%.html.template
151
+ - test/templates/00/seqfiles/page_about_%critter_name%.html.template
152
+ - test/templates/01/%string.length%/%string%-%string.length%.nontemplate
153
+ - test/templates/01/%string.length%/%string%-%string.length%.txt.template
154
+ - test/templates/02/%class%.rb.template
155
+ - test/templates/03/coordinates.%i%-%j%-%k%.html.template
156
+ - test/templates/03/dir%0000i%/dir%0000j%/file%0000k%.py.template
157
+ - test/templates/03/index.html.template
158
+ - test/templates/epf/book/afterword.markdown
159
+ - test/templates/epf/book/chapter-%0000chapter%.markdown.template
160
+ - test/templates/epf/book/cover.xhtml.template
161
+ - test/templates/epf/book/foreword.markdown
162
+ - test/templates/epf/book/images/cover.png
163
+ - test/templates/epf/book/stylesheets/stylesheet.css
164
+ - test/templates/epf/book/title_page.markdown.template
165
+ - test/templates/epf/notes/character.%character.name_for_file%.markdown.template
166
+ - test/templates/epf/notes/images/cover.png
167
+ - test/templates/epf/notes/stylesheets/stylesheet.css
168
+ - test/templates/epf/settings/actions/local_action.rb.example
169
+ - test/templates/epf/settings/config.rb.template
170
+ - test/templates/epf/settings/htmlizers.rb
171
+ - test/templates/epf/settings/wordcount.template
172
+ - test/test_directory_builder.rb
173
+ - test/test_filename_var_data.rb
174
+ - test/test_fun_with_templates.rb
175
+ - test/test_parse_filename_vars.rb
176
+ - test/test_string_templates.rb
177
+ - test/test_write_example_templates.rb
178
+ homepage: http://github.com/darthschmoo/fun_with_templates
179
+ licenses:
180
+ - MIT
181
+ metadata: {}
182
+ post_install_message:
183
+ rdoc_options: []
184
+ require_paths:
185
+ - lib
186
+ required_ruby_version: !ruby/object:Gem::Requirement
187
+ requirements:
188
+ - - ! '>='
189
+ - !ruby/object:Gem::Version
190
+ version: '0'
191
+ required_rubygems_version: !ruby/object:Gem::Requirement
192
+ requirements:
193
+ - - ! '>='
194
+ - !ruby/object:Gem::Version
195
+ version: '0'
196
+ requirements: []
197
+ rubyforge_project:
198
+ rubygems_version: 2.2.2
199
+ signing_key:
200
+ specification_version: 4
201
+ summary: Templates made stupid
202
+ test_files: []