general 1.5.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (59) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +166 -20
  3. data/Rakefile +11 -2
  4. data/exp/expected/applied1.txt +1 -0
  5. data/exp/expected/applied2.txt +3 -0
  6. data/exp/expected/applied3.txt +9 -0
  7. data/exp/expected/default1.txt +1 -0
  8. data/exp/expected/default2.txt +3 -0
  9. data/exp/expected/default3.txt +9 -0
  10. data/exp/out/sample1.txt +1 -0
  11. data/exp/out/sample2.txt +3 -0
  12. data/exp/out/sample3.txt +9 -0
  13. data/exp/templates/sample0.general +7 -0
  14. data/exp/templates/sample1.general +1 -0
  15. data/exp/templates/sample2.general +3 -0
  16. data/exp/templates/sample3.general +4 -0
  17. data/exp/templates/sampleE0.general +7 -0
  18. data/exp/templates/sampleE1.general +3 -0
  19. data/exp/templates/sampleE2.general +3 -0
  20. data/exp/templates/sampleE3.general +7 -0
  21. data/lib/general.rb +3 -3
  22. data/lib/gerror.rb +33 -0
  23. data/lib/goperations.rb +95 -27
  24. data/lib/gpartials/garrayplaceholder.rb +87 -0
  25. data/lib/gpartials/gfullplaceholder.rb +63 -0
  26. data/lib/gpartials/gpartial.rb +11 -1
  27. data/lib/gpartials/gplaceholder.rb +1 -52
  28. data/lib/gpartials/gspecial.rb +81 -0
  29. data/lib/gpartials/gtext.rb +5 -33
  30. data/lib/gprepartials/gextend.rb +42 -0
  31. data/lib/gprepartials/ginclude.rb +47 -0
  32. data/lib/gprepartials/gprepartial.rb +45 -0
  33. data/lib/gprepartials/gpretext.rb +33 -0
  34. data/lib/gprepartials/gyield.rb +33 -0
  35. data/lib/{templates → gtemplates}/gbasetemplate.rb +46 -3
  36. data/lib/gtemplates/gio.rb +143 -0
  37. data/lib/gtemplates/gmeta.rb +106 -0
  38. data/lib/{templates → gtemplates}/gtemplate.rb +10 -30
  39. data/lib/{templates → gtemplates}/gtimeformat.rb +6 -12
  40. data/spec/gdothash_spec.rb +6 -10
  41. data/spec/goperations_spec.rb +112 -82
  42. data/spec/gpartial_garrayplaceholder_spec.rb +165 -0
  43. data/spec/gpartial_gfullplaceholder_spec.rb +82 -0
  44. data/spec/gpartial_gplaceholder_spec.rb +237 -0
  45. data/spec/gpartial_gspecial_spec.rb +88 -0
  46. data/spec/gpartial_gtext_spec.rb +87 -0
  47. data/spec/gtamplate_gtemplate_spec.rb +266 -0
  48. data/spec/gtemplate_gio_spec.rb +147 -0
  49. data/spec/gtemplate_gtimeformat_spec.rb +77 -0
  50. data/spec/spec_require.rb +7 -4
  51. metadata +41 -13
  52. data/exp/future.general +0 -11
  53. data/exp/out.txt +0 -1
  54. data/exp/sample.general +0 -1
  55. data/lib/templates/gio.rb +0 -68
  56. data/spec/garrayplaceholder_spec.rb +0 -163
  57. data/spec/gplaceholder_spec.rb +0 -300
  58. data/spec/gtemplates_spec.rb +0 -480
  59. data/spec/gtext_spec.rb +0 -150
@@ -1,163 +0,0 @@
1
- # General is a templating system in ruby
2
- # Copyright (C) 2016 Anshul Kharbanda
3
- #
4
- # This program is free software: you can redistribute it and/or modify
5
- # it under the terms of the GNU General Public License as published by
6
- # the Free Software Foundation, either version 3 of the License, or
7
- # (at your option) any later version.
8
- #
9
- # This program is distributed in the hope that it will be useful,
10
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
11
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
- # GNU General Public License for more details.
13
- #
14
- # You should have received a copy of the GNU General Public License
15
- # along with this program. If not, see <http://www.gnu.org/licenses/>.
16
-
17
- require_relative "spec_require"
18
-
19
- # Describe General::GArrayPlaceholder
20
- #
21
- # Represents an array placeholder partial in a GTemplate
22
- #
23
- # Author: Anshul Kharbanda
24
- # Created: 7 - 1 - 2016
25
- describe General::GArrayPlaceholder do
26
- before :all do
27
- # -----------DATA-----------
28
-
29
- @arrayname1 = :arg
30
- @hash = {
31
- arg: [
32
- { subarg: "Eeeeyyy" },
33
- { subarg: "Oyyyeee" },
34
- { subarg: "Aaayyyy" }
35
- ]
36
- }
37
-
38
- @text = "Subarg: @(subarg)"
39
- @regex = "Subarg: (?<subarg>.*)"
40
- @delim = "\n"
41
-
42
- # ---------------------------OUTPUT---------------------------
43
-
44
- @out1 = @hash[@arrayname1]
45
- .collect{|e| "Subarg: #{e[:subarg]}"}
46
- .join(General::GArrayPlaceholder::DEFAULT_DELIMETER)
47
- @out2 = @hash[@arrayname1]
48
- .collect{|e| "Subarg: #{e[:subarg]}"}
49
- .join(@delim)
50
-
51
- # ------------------------------------------PARTIALS------------------------------------------
52
-
53
- @partial1 = General::GArrayPlaceholder.new name: @arrayname1, text: @text
54
- @partial2 = General::GArrayPlaceholder.new name: @arrayname1, text: @text, delimeter: @delim
55
-
56
- # -------------------------------------------STRING-------------------------------------------
57
-
58
- @string1 = "@[#{@arrayname1}] #{@text} @[ ]"
59
- @string2 = "@[#{@arrayname1}] #{@text} @[\\n]"
60
-
61
- # -------------------------------------------REGEX--------------------------------------------
62
-
63
- @regex1 = "(?<#{@arrayname1}>(#{@regex}( )?)+)"
64
- @regex2 = "(?<#{@arrayname1}>(#{@regex}(\\n)?)+)"
65
- end
66
-
67
- # Describe General::GArrayPlaceholder::new
68
- #
69
- # Initializes the GPlaceholder with the given match
70
- #
71
- # Parameter: match - the match data from the string being parsed
72
- describe '::new' do
73
- context 'with the given name and text' do
74
- it 'creates a new GArrayPlaceholder with the given name and text' do
75
- expect(@partial1).to be_an_instance_of General::GArrayPlaceholder
76
- end
77
- end
78
-
79
- context 'with the given name, text, and delimeter' do
80
- it 'creates a new GArrayPlaceholder with the given name and text and delimeter' do
81
- expect(@partial1).to be_an_instance_of General::GArrayPlaceholder
82
- end
83
- end
84
- end
85
-
86
- # Describe General::GArrayPlaceholder#apply
87
- #
88
- # Returns the value of the array placeholder in the given data
89
- # formatted by the given GArrayPlaceholder and joined by the given delimeter
90
- #
91
- # Parameter: data - the data being applied
92
- #
93
- # Return: the value of the array placeholder in the given data
94
- # formatted by the given GArrayPlaceholder and joined by the given delimeter
95
- describe '#apply' do
96
- it 'applies the given data array formatted according to the given GArrayPlaceholder and joined by the delimeter' do
97
- expect(@partial1.apply @hash).to eql @out1
98
- expect(@partial2.apply @hash).to eql @out2
99
- end
100
- end
101
-
102
- # Describe General::GArrayPlaceholder#string
103
- #
104
- # Returns the string representation of the GArrayPlaceholder
105
- #
106
- # Parameter: first - true if the GArrayPlaceholder is the first of it's time
107
- #
108
- # Returns: the string representation of the GArrayPlaceholder
109
- describe '#string' do
110
- context 'with no first argument is given' do
111
- it 'returns the string of the GArrayPlaceholder' do
112
- expect(@partial1.string).to eql @string1
113
- expect(@partial2.string).to eql @string2
114
- end
115
- end
116
-
117
- context 'with first argument is given' do
118
- context 'when first is true' do
119
- it 'returns the string of the GArrayPlaceholder' do
120
- expect(@partial1.string true).to eql @string1
121
- expect(@partial2.string true).to eql @string2
122
- end
123
- end
124
-
125
- context 'when first is false' do
126
- it 'returns the string of the GArrayPlaceholder' do
127
- expect(@partial1.string false).to eql @string1
128
- expect(@partial2.string false).to eql @string2
129
- end
130
- end
131
- end
132
- end
133
-
134
- # Describe General::GArrayPlaceholder#regex
135
- #
136
- # Throws TypeError
137
- #
138
- # Parameter: first - true if the placeholder is the first of it's kind
139
- describe '#regex' do
140
- context 'with no first argument is given' do
141
- it 'returns the regex of the GArrayPlaceholder' do
142
- expect { @partial1.regex }.to raise_error TypeError
143
- expect { @partial2.regex }.to raise_error TypeError
144
- end
145
- end
146
-
147
- context 'with first argument is given' do
148
- context 'when first is true' do
149
- it 'returns the regex of the GArrayPlaceholder' do
150
- expect { @partial1.regex true }.to raise_error TypeError
151
- expect { @partial2.regex true }.to raise_error TypeError
152
- end
153
- end
154
-
155
- context 'when first is false' do
156
- it 'returns the regex of the GArrayPlaceholder' do
157
- expect { @partial1.regex false }.to raise_error TypeError
158
- expect { @partial2.regex false }.to raise_error TypeError
159
- end
160
- end
161
- end
162
- end
163
- end
@@ -1,300 +0,0 @@
1
- # General is a templating system in ruby
2
- # Copyright (C) 2016 Anshul Kharbanda
3
- #
4
- # This program is free software: you can redistribute it and/or modify
5
- # it under the terms of the GNU General Public License as published by
6
- # the Free Software Foundation, either version 3 of the License, or
7
- # (at your option) any later version.
8
- #
9
- # This program is distributed in the hope that it will be useful,
10
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
11
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
- # GNU General Public License for more details.
13
- #
14
- # You should have received a copy of the GNU General Public License
15
- # along with this program. If not, see <http://www.gnu.org/licenses/>.
16
-
17
- require_relative "spec_require"
18
-
19
- # Represents a placeholder partial in a GTemplate
20
- #
21
- # Author: Anshul Kharbanda
22
- # Created: 7 - 1 - 2016
23
- describe General::GPlaceholder do
24
- before :all do
25
- # Placename
26
- @placename1 = :plac1
27
- @placename2 = :plac2
28
- @placename3 = :plac3
29
- @placename4 = :plac4
30
- @placename5 = :"plac5.subarg"
31
-
32
- # Hash
33
- @hash = General::GDotHash.new
34
- @hash[@placename1] = "foobar"
35
- @hash[@placename3] = "barbaz"
36
- @hash[@placename4] = "jujar"
37
- @hash[@placename5] = "dudar"
38
-
39
- # Default hash
40
- @defaults = General::GDotHash.new
41
-
42
- # ------------PLACEHOLDER------------
43
- @result1 = @hash[@placename1]
44
- @partial1 = General::GPlaceholder.new [
45
- [:name, @placename1]
46
- ].to_h, @defaults
47
- @string1 = "@(#{@placename1})"
48
- @string_first1 = "@(#{@placename1})"
49
- @regex1 = "\\k<#{@placename1}>"
50
- @regex_first1 = "(?<#{@placename1}>.*)"
51
-
52
- # --------------DEFAULT--------------
53
- @default2 = "foo2"
54
- @result2 = "foo2"
55
- @partial2 = General::GPlaceholder.new [
56
- [:name, @placename2],
57
- [:default, @default2]
58
- ].to_h, @defaults
59
- @string2 = "@(#{@placename2})"
60
- @string_first2 = "@(#{@placename2}:" \
61
- + " #{@default2})"
62
- @regex2 = "\\k<#{@placename2}>"
63
- @regex_first2 = "(?<#{@placename2}>.*)"
64
-
65
- # -------------OPERATION-------------
66
- @operation3 = "capitalize"
67
- @result3 = General::GOperations.send(
68
- @operation3.to_sym,
69
- @hash[@placename3]
70
- )
71
- @partial3 = General::GPlaceholder.new [
72
- [:name, @placename3],
73
- [:operation, @operation3]
74
- ].to_h, @defaults
75
- @string3 = "@(#{@placename3})"
76
- @string_first3 = "@(#{@placename3}" \
77
- + " -> #{@operation3})"
78
- @regex3 = "\\k<#{@placename3}>"
79
- @regex_first3 = "(?<#{@placename3}>.*)"
80
-
81
- # -------------ARGUMENTS-------------
82
- @operation4 = "capitalize"
83
- @arguments4 = ["all"]
84
- @result4 = General::GOperations.send(
85
- @operation4.to_sym,
86
- @hash[@placename4],
87
- *@arguments4
88
- )
89
- @partial4 = General::GPlaceholder.new [
90
- [:name, @placename4],
91
- [:operation, @operation4],
92
- [:arguments, @arguments4.join(" ")]
93
- ].to_h, @defaults
94
- @string4 = "@(#{@placename4})"
95
- @string_first4 = "@(#{@placename4}" \
96
- + " -> #{@operation4} " \
97
- + "#{@arguments4.collect {|s| "\"#{s}\""}.join " "})"
98
- @regex4 = "\\k<#{@placename4}>"
99
- @regex_first4 = "(?<#{@placename4}>.*)"
100
-
101
- # -----------DOT NOTATION------------
102
- keys = @placename5.to_s.split(".").collect(&:to_sym)
103
- @result5 = @hash[keys[0]][keys[1]]
104
- @partial5 = General::GPlaceholder.new [
105
- [:name, @placename5]
106
- ].to_h, @defaults
107
- @string5 = "@(#{@placename5})"
108
- @string_first5 = "@(#{@placename5})"
109
- @regex5 = "\\k<#{@placename5}>"
110
- @regex_first5 = "(?<#{@placename5}>.*)"
111
- end
112
-
113
- # Describe General::GPlaceholder::new
114
- #
115
- # Initializes the GPlaceholder with the given match
116
- #
117
- # Parameter: match - the match data from the string being parsed
118
- describe '::new' do
119
- # -------------------------------------PLACEHOLDER---------------------------------------
120
- context 'with name given' do
121
- it 'creaes a GPlaceholder with the given name' do
122
- expect(@partial1).to be_an_instance_of General::GPlaceholder
123
- expect(@partial1.instance_variable_get :@name).to eql @placename1
124
- expect(@partial1.instance_variable_get :@operation).to be_nil
125
- expect(@partial1.instance_variable_get :@arguments).to be_empty
126
- end
127
- end
128
-
129
- # ---------------------------------------DEFAULT-----------------------------------------
130
- context 'with name and default in match object' do
131
- it 'creates a GPlaceholder with the given name and default' do
132
- expect(@partial2).to be_an_instance_of General::GPlaceholder
133
- expect(@partial2.instance_variable_get :@name).to eql @placename2
134
- expect(@partial2.instance_variable_get :@operation).to be_nil
135
- expect(@partial2.instance_variable_get :@arguments).to be_empty
136
- expect(@defaults[@placename2]).to eql @default2
137
- end
138
- end
139
-
140
- # --------------------------------------OPERATION----------------------------------------
141
- context 'with name, default, and operation in match object' do
142
- it 'creates a GPlaceholder with the given name and operation' do
143
- expect(@partial3).to be_an_instance_of General::GPlaceholder
144
- expect(@partial3.instance_variable_get :@name).to eql @placename3
145
- expect(@partial3.instance_variable_get :@operation).to eql @operation3
146
- expect(@partial3.instance_variable_get :@arguments).to be_empty
147
- end
148
- end
149
-
150
- # --------------------------------------ARGUMENTS----------------------------------------
151
- context 'with name, default, operation, and arguments in match object' do
152
- it 'creates a GPlaceholder with the given name, operation, and arguments' do
153
- expect(@partial4).to be_an_instance_of General::GPlaceholder
154
- expect(@partial4.instance_variable_get :@name).to eql @placename4
155
- expect(@partial4.instance_variable_get :@operation).to eql @operation4
156
- expect(@partial4.instance_variable_get :@arguments).to eql @arguments4
157
- end
158
- end
159
-
160
- # --------------------------------------ARGUMENTS----------------------------------------
161
- context 'with dot notation name' do
162
- it 'creates a GPlaceholder with the given dot notation name' do
163
- expect(@partial5).to be_an_instance_of General::GPlaceholder
164
- expect(@partial5.instance_variable_get :@name).to eql @placename5
165
- expect(@partial5.instance_variable_get :@operation).to be_nil
166
- expect(@partial5.instance_variable_get :@arguments).to be_empty
167
- end
168
- end
169
- end
170
-
171
- # Describe General::GPlaceholder#apply
172
- #
173
- # Returns the value of the placeholder in the given data
174
- # with the given operation performed on it
175
- #
176
- # Parameter: data - the data being applied
177
- #
178
- # Return: the value of the placeholder in the given data
179
- # with the given operation performed on it
180
- describe '#apply' do
181
- # -------------------------------------PLACEHOLDER---------------------------------------
182
- context 'when name is defined in given data' do
183
- it 'returns the value in the data at the name' do
184
- expect(@partial1.apply @hash).to eql @result1
185
- end
186
- end
187
-
188
- # ---------------------------------------DEFAULT-----------------------------------------
189
- context 'when name is not defined in given data, but defined in defaults' do
190
- it 'returns the value in the default at the name' do
191
- expect(@partial2.apply @hash).to eql @result2
192
- end
193
- end
194
-
195
- # --------------------------------------OPERATION----------------------------------------
196
- context 'when an operation is defined' do
197
- it 'returns the value in the data/default at the name with the operation applied' do
198
- expect(@partial3.apply @hash).to eql @result3
199
- end
200
- end
201
-
202
- # --------------------------------------ARGUMENTS----------------------------------------
203
- context 'when an operation and arguments are defined' do
204
- it 'returns the value in the data/default at the name with the operation and arguments applied' do
205
- expect(@partial4.apply @hash).to eql @result4
206
- end
207
- end
208
-
209
- # -------------------------------------DOT NOTATION--------------------------------------
210
- context 'when dot notation name is given' do
211
- it 'returns the value in the data/default at the dot notation name' do
212
- expect(@partial5.apply @hash).to eql @result5
213
- end
214
- end
215
- end
216
-
217
- # Describe General::GTemplate#string
218
- #
219
- # Returns the string representation of the placeholder
220
- #
221
- # Parameter: first - true if the placeholder is the first of its kind in the GTemplate
222
- #
223
- # Return: the string representation of the placeholder
224
- describe '#string' do
225
- # -------------------------------------NO FIRST---------------------------------------
226
- context 'when no first argument specified' do
227
- it 'returns the first string representation of the placeholder' do
228
- expect(@partial1.string).to eql @string_first1
229
- expect(@partial2.string).to eql @string_first2
230
- expect(@partial3.string).to eql @string_first3
231
- expect(@partial4.string).to eql @string_first4
232
- end
233
- end
234
-
235
- # --------------------------------------FIRST-----------------------------------------
236
- context 'when first argument is specified' do
237
- # --------------------------------TRUE--------------------------------
238
- context 'when first is true' do
239
- it 'returns the first string representation of the placeholder' do
240
- expect(@partial1.string true).to eql @string_first1
241
- expect(@partial2.string true).to eql @string_first2
242
- expect(@partial3.string true).to eql @string_first3
243
- expect(@partial4.string true).to eql @string_first4
244
- end
245
- end
246
-
247
- # --------------------------------FALSE-------------------------------
248
- context 'when first is false' do
249
- it 'returns the string representation of the placeholder' do
250
- expect(@partial1.string false).to eql @string1
251
- expect(@partial2.string false).to eql @string2
252
- expect(@partial3.string false).to eql @string3
253
- expect(@partial4.string false).to eql @string4
254
- end
255
- end
256
- end
257
- end
258
-
259
- # Describe General::GTemplate#regex
260
- #
261
- # Returns the string as a regex
262
- #
263
- # Parameter: first - true if the placeholder is the first of its kind in the GTemplate
264
- #
265
- # Returns: the string as a regex
266
- describe '#regex' do
267
- # -------------------------------------NO FIRST---------------------------------------
268
- context 'when no first argument specified' do
269
- it 'returns the first regex representation of the placeholder' do
270
- expect(@partial1.regex).to eql @regex_first1
271
- expect(@partial2.regex).to eql @regex_first2
272
- expect(@partial3.regex).to eql @regex_first3
273
- expect(@partial4.regex).to eql @regex_first4
274
- end
275
- end
276
-
277
- # --------------------------------------FIRST-----------------------------------------
278
- context 'when first argument is specified' do
279
- # --------------------------------TRUE--------------------------------
280
- context 'when first is true' do
281
- it 'returns the first regex representation of the placeholder' do
282
- expect(@partial1.regex true).to eql @regex_first1
283
- expect(@partial2.regex true).to eql @regex_first2
284
- expect(@partial3.regex true).to eql @regex_first3
285
- expect(@partial4.regex true).to eql @regex_first4
286
- end
287
- end
288
-
289
- # --------------------------------FALSE-------------------------------
290
- context 'when first is false' do
291
- it 'returns the regex representation of the placeholder' do
292
- expect(@partial1.regex false).to eql @regex1
293
- expect(@partial2.regex false).to eql @regex2
294
- expect(@partial3.regex false).to eql @regex3
295
- expect(@partial4.regex false).to eql @regex4
296
- end
297
- end
298
- end
299
- end
300
- end