general 1.5.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +166 -20
- data/Rakefile +11 -2
- data/exp/expected/applied1.txt +1 -0
- data/exp/expected/applied2.txt +3 -0
- data/exp/expected/applied3.txt +9 -0
- data/exp/expected/default1.txt +1 -0
- data/exp/expected/default2.txt +3 -0
- data/exp/expected/default3.txt +9 -0
- data/exp/out/sample1.txt +1 -0
- data/exp/out/sample2.txt +3 -0
- data/exp/out/sample3.txt +9 -0
- data/exp/templates/sample0.general +7 -0
- data/exp/templates/sample1.general +1 -0
- data/exp/templates/sample2.general +3 -0
- data/exp/templates/sample3.general +4 -0
- data/exp/templates/sampleE0.general +7 -0
- data/exp/templates/sampleE1.general +3 -0
- data/exp/templates/sampleE2.general +3 -0
- data/exp/templates/sampleE3.general +7 -0
- data/lib/general.rb +3 -3
- data/lib/gerror.rb +33 -0
- data/lib/goperations.rb +95 -27
- data/lib/gpartials/garrayplaceholder.rb +87 -0
- data/lib/gpartials/gfullplaceholder.rb +63 -0
- data/lib/gpartials/gpartial.rb +11 -1
- data/lib/gpartials/gplaceholder.rb +1 -52
- data/lib/gpartials/gspecial.rb +81 -0
- data/lib/gpartials/gtext.rb +5 -33
- data/lib/gprepartials/gextend.rb +42 -0
- data/lib/gprepartials/ginclude.rb +47 -0
- data/lib/gprepartials/gprepartial.rb +45 -0
- data/lib/gprepartials/gpretext.rb +33 -0
- data/lib/gprepartials/gyield.rb +33 -0
- data/lib/{templates → gtemplates}/gbasetemplate.rb +46 -3
- data/lib/gtemplates/gio.rb +143 -0
- data/lib/gtemplates/gmeta.rb +106 -0
- data/lib/{templates → gtemplates}/gtemplate.rb +10 -30
- data/lib/{templates → gtemplates}/gtimeformat.rb +6 -12
- data/spec/gdothash_spec.rb +6 -10
- data/spec/goperations_spec.rb +112 -82
- data/spec/gpartial_garrayplaceholder_spec.rb +165 -0
- data/spec/gpartial_gfullplaceholder_spec.rb +82 -0
- data/spec/gpartial_gplaceholder_spec.rb +237 -0
- data/spec/gpartial_gspecial_spec.rb +88 -0
- data/spec/gpartial_gtext_spec.rb +87 -0
- data/spec/gtamplate_gtemplate_spec.rb +266 -0
- data/spec/gtemplate_gio_spec.rb +147 -0
- data/spec/gtemplate_gtimeformat_spec.rb +77 -0
- data/spec/spec_require.rb +7 -4
- metadata +41 -13
- data/exp/future.general +0 -11
- data/exp/out.txt +0 -1
- data/exp/sample.general +0 -1
- data/lib/templates/gio.rb +0 -68
- data/spec/garrayplaceholder_spec.rb +0 -163
- data/spec/gplaceholder_spec.rb +0 -300
- data/spec/gtemplates_spec.rb +0 -480
- data/spec/gtext_spec.rb +0 -150
@@ -0,0 +1,266 @@
|
|
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 "spec_require"
|
18
|
+
|
19
|
+
# Describe General::GTemplate
|
20
|
+
#
|
21
|
+
# Implements the general templating system for strings
|
22
|
+
#
|
23
|
+
# Author: Anshul Kharbanda
|
24
|
+
# Created: 3 - 4 - 2016
|
25
|
+
describe General::GTemplate do
|
26
|
+
# Before all
|
27
|
+
before :all do
|
28
|
+
class Person
|
29
|
+
def initialize nam, food
|
30
|
+
@name = nam
|
31
|
+
@food = food
|
32
|
+
end
|
33
|
+
def generalized
|
34
|
+
{name: @name,
|
35
|
+
food: @food}
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
@template1 = General::GTemplate.new "There once was a man named @(name: Gordon Ramsay). @(name) loved @(food: Cat Food)!"
|
40
|
+
@template2 = General::GTemplate.new "@(user)@at;@(domain)"
|
41
|
+
@template3 = General::GTemplate.new "@[greetings] Hello, @(name)! How is the @(pet)? @[\n]"
|
42
|
+
@template4 = General::GTemplate.new "@(film: The Dark Knight)\nCrew:\n@[crew] \t@(name): @(role) @[\n]\nScore: @(score)/10"
|
43
|
+
@template5 = General::GTemplate.new "There once was a dog named @(name: dog -> capitalize). @(name -> capitalize) earned @(amount -> money) last week."
|
44
|
+
@template6 = General::GTemplate.new "There once was a cat named @(name -> capitalize all)."
|
45
|
+
@template7 = General::GTemplate.new "The time is @(time -> time). It may also be formatted as @(time -> time '@SS <- @MM <- @HH')"
|
46
|
+
@template8 = General::GTemplate.new "The name's @(name.last)... @(name.first) @(name.last)."
|
47
|
+
@template9 = General::GTemplate.new "My favorite color is @#!"
|
48
|
+
@template10 = General::GTemplate.new "@[list -> split] Need @#! @[\n]"
|
49
|
+
end
|
50
|
+
|
51
|
+
# Describe General::GTemplate::new
|
52
|
+
#
|
53
|
+
# Creates a GTemplate with the given template string
|
54
|
+
#
|
55
|
+
# Parameter: string - the string being converted to a template
|
56
|
+
describe "::new" do
|
57
|
+
it "creates a new GTemplate with the given template string" do
|
58
|
+
[@template1, @template2, @template3, @template4, @template5,
|
59
|
+
@template6, @template7, @template8, @template9, @template10]
|
60
|
+
.each{ |template| expect(template).to be_an_instance_of General::GTemplate }
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
# Describe General::GTemplate#apply
|
65
|
+
#
|
66
|
+
# Applies the given data to the template and returns the generated string
|
67
|
+
#
|
68
|
+
# Parameter: data - the data to be applied (as a hash. merges with defaults)
|
69
|
+
#
|
70
|
+
# Return: string of the template with the given data applied
|
71
|
+
describe "#apply" do
|
72
|
+
# ---------------------------------------------DATA----------------------------------------------
|
73
|
+
before :all do
|
74
|
+
# -------------------------------------BASIC PLACEHOLDER-------------------------------------
|
75
|
+
@data1 = {name: "Joe", food: "Joe's Shmoes"}
|
76
|
+
@name = "Dog"
|
77
|
+
@food = "Denny's Fennies"
|
78
|
+
|
79
|
+
@default_text = "There once was a man named Gordon Ramsay. Gordon Ramsay loved Cat Food!"
|
80
|
+
@name_text = "There once was a man named Dog. Dog loved Cat Food!"
|
81
|
+
@food_text = "There once was a man named Gordon Ramsay. Gordon Ramsay loved Denny's Fennies!"
|
82
|
+
@all_text = "There once was a man named Joe. Joe loved Joe's Shmoes!"
|
83
|
+
|
84
|
+
# ------------------------------------SPECIAL CHARACTERS------------------------------------
|
85
|
+
@data2 = { user: "hillary", domain: "clinton.org" }
|
86
|
+
@text2 = "hillary@clinton.org"
|
87
|
+
|
88
|
+
# --------------------------------------ARRAY TEMPLATE--------------------------------------
|
89
|
+
@data3 = {
|
90
|
+
greetings: [
|
91
|
+
{name: "Ben", pet: "dog"},
|
92
|
+
{name: "Jen", pet: "cat"},
|
93
|
+
{name: "Ken", pet: "plant"}
|
94
|
+
]
|
95
|
+
}
|
96
|
+
@text3 = "Hello, Ben! How is the dog?\nHello, Jen! How is the cat?\nHello, Ken! How is the plant?"
|
97
|
+
|
98
|
+
@data4 = {
|
99
|
+
film: 'Batman Begins',
|
100
|
+
crew: [
|
101
|
+
{name: 'David S. Goyer', role: 'Writer'},
|
102
|
+
{name: 'Chris Nolan', role: 'Director'},
|
103
|
+
{name: 'Wally Pfister', role: 'Director of Photography'},
|
104
|
+
{name: 'Michael Caine', role: 'Alfred Pennyworth'},
|
105
|
+
{name: 'Christian Bale', role: 'Bruce Wayne/Batman'}
|
106
|
+
],
|
107
|
+
score: 10
|
108
|
+
}
|
109
|
+
@text4 = "Batman Begins\nCrew:\n\tDavid S. Goyer: Writer\n\tChris Nolan: Director\n\tWally Pfister: Director of Photography" \
|
110
|
+
"\n\tMichael Caine: Alfred Pennyworth\n\tChristian Bale: Bruce Wayne/Batman\nScore: 10/10"
|
111
|
+
|
112
|
+
# ---------------------------------------OPERATIONS-----------------------------------------
|
113
|
+
@data5 = {name: "cat", amount: 19999}
|
114
|
+
@text5 = "There once was a dog named Cat. Cat earned $199.99 last week."
|
115
|
+
|
116
|
+
@data6 = {name: "joe schmoe"}
|
117
|
+
@text6 = "There once was a cat named Joe Schmoe."
|
118
|
+
|
119
|
+
hrs = 3; min = 14; sec = 12; pm = true
|
120
|
+
@data7 = {
|
121
|
+
time: ( ((pm ? 11 : 0) + hrs)*3600 + min*60 + sec )
|
122
|
+
}
|
123
|
+
|
124
|
+
@text7 = "The time is #{hrs}:#{min.to_s.rjust(2,'0')}:#{sec.to_s.rjust(2,'0')} #{pm ? 'PM' : 'AM'}. " \
|
125
|
+
"It may also be formatted as #{sec.to_s.rjust(2,'0')} <- #{min.to_s.rjust(2,'0')} <- #{((pm ? 11 : 0) + hrs).to_s.rjust(2,'0')}"
|
126
|
+
|
127
|
+
# --------------------------------------DOT NOTATION----------------------------------------
|
128
|
+
@data9 = General::GDotHash.new name: {first: "Gordon", last: "Ramsay"}
|
129
|
+
@text9 = "The name's Ramsay... Gordon Ramsay."
|
130
|
+
|
131
|
+
# ------------------------------------FULL PLACEHOLDER--------------------------------------
|
132
|
+
@data10 = "Gilbert"
|
133
|
+
@text10 = "My favorite color is Gilbert!"
|
134
|
+
|
135
|
+
# -----------------------------------TO-ARRAY OPERATION-------------------------------------
|
136
|
+
@data11 = {list: "Butter\nMilk\nEggs"}
|
137
|
+
@text11 = "Need Butter!\nNeed Milk!\nNeed Eggs!"
|
138
|
+
|
139
|
+
# ----------------------------------GENERALIZED INTERFACE-----------------------------------
|
140
|
+
@data12 = Person.new "Joe", "Joe's Schmoes"
|
141
|
+
@text12 = "There once was a man named Joe. Joe loved Joe's Schmoes!"
|
142
|
+
end
|
143
|
+
|
144
|
+
# ---------------------------------------------TEST----------------------------------------------
|
145
|
+
|
146
|
+
it "returns the template with data/defaults applied to corresponding placeholders" do
|
147
|
+
expect(@template1.apply).to eql @default_text
|
148
|
+
expect(@template1.apply name: @name).to eql @name_text
|
149
|
+
expect(@template1.apply food: @food).to eql @food_text
|
150
|
+
expect(@template1.apply @data1).to eql @all_text
|
151
|
+
end
|
152
|
+
|
153
|
+
it 'formats special @; characters appropriately' do
|
154
|
+
expect(@template2.apply @data2).to eql @text2
|
155
|
+
end
|
156
|
+
|
157
|
+
it "formatts array data according to array templates" do
|
158
|
+
expect(@template3.apply @data3).to eql @text3
|
159
|
+
expect(@template4.apply @data4).to eql @text4
|
160
|
+
end
|
161
|
+
|
162
|
+
it 'formats data according to given placeholder operations and arguments (if given)' do
|
163
|
+
expect(@template5.apply @data5).to eql @text5
|
164
|
+
expect(@template6.apply @data6).to eql @text6
|
165
|
+
expect(@template7.apply @data7).to eql @text7
|
166
|
+
end
|
167
|
+
|
168
|
+
it "applies data appropriately to dot notation placeholders" do
|
169
|
+
expect(@template8.apply @data9).to eql @text9
|
170
|
+
end
|
171
|
+
|
172
|
+
it "applies full data to full placeholders" do
|
173
|
+
expect(@template9.apply @data10).to eql @text10
|
174
|
+
end
|
175
|
+
|
176
|
+
it "formats array data generated by to-array operations according to array template" do
|
177
|
+
expect(@template10.apply @data11).to eql @text11
|
178
|
+
end
|
179
|
+
|
180
|
+
it "returns the template with the generalized data from object applied appropriately" do
|
181
|
+
expect(@template1.apply @data12).to eql @text12
|
182
|
+
end
|
183
|
+
end
|
184
|
+
|
185
|
+
# Describe General::GTemplate#apply_all
|
186
|
+
#
|
187
|
+
# Applies each data structure in the array independently to the template
|
188
|
+
# and returns an array of the generated strings
|
189
|
+
#
|
190
|
+
# Parameter: array - the array of data to be applied
|
191
|
+
# (each data hash will be merged with defaults)
|
192
|
+
#
|
193
|
+
# Return: array of strings generated from the template with the given
|
194
|
+
# data applied
|
195
|
+
describe '#apply_all' do
|
196
|
+
before :all do
|
197
|
+
@data5 = [
|
198
|
+
{name: "Joe", food: "Joe's Schmoes"},
|
199
|
+
{name: "Jane", food: "Jane's Danes"},
|
200
|
+
{name: "Denny", food: "Denny's Fennies"}
|
201
|
+
]
|
202
|
+
|
203
|
+
@text5 = [
|
204
|
+
"There once was a man named Joe. Joe loved Joe's Schmoes!",
|
205
|
+
"There once was a man named Jane. Jane loved Jane's Danes!",
|
206
|
+
"There once was a man named Denny. Denny loved Denny's Fennies!"
|
207
|
+
]
|
208
|
+
end
|
209
|
+
|
210
|
+
it 'applies all the values in the data array individually' do
|
211
|
+
expect(@template1.apply_all @data5).to eql @text5
|
212
|
+
end
|
213
|
+
end
|
214
|
+
|
215
|
+
# Describe General::GTemplate#regex (depricated)
|
216
|
+
#
|
217
|
+
# Returns the string as a regex
|
218
|
+
#
|
219
|
+
# Parameter: sub - true if the template is part of array template
|
220
|
+
#
|
221
|
+
# Returns: the string as a regex
|
222
|
+
describe '#regex (depricated)' do
|
223
|
+
before :all do
|
224
|
+
@regex1 = /\AThere once was a man named (?<name>.*)\. \k<name> loved (?<food>.*)!\z/
|
225
|
+
@sub_regex1 = "There once was a man named (?<name>.*)\\. \\k<name> loved (?<food>.*)!"
|
226
|
+
end
|
227
|
+
|
228
|
+
it 'returns the regex created from the GTemplate if template is not part of array template' do
|
229
|
+
expect(@template1.regex).to eql @regex1
|
230
|
+
end
|
231
|
+
|
232
|
+
it 'returns the sub regex string created from the GTemplate if template is part of array template' do
|
233
|
+
expect(@template1.regex true).to eql @sub_regex1
|
234
|
+
end
|
235
|
+
end
|
236
|
+
|
237
|
+
# Describe General::GTemplate#match (depricated)
|
238
|
+
#
|
239
|
+
# Matches the given string against the template and returns the
|
240
|
+
# collected information. Returns nil if the given string does
|
241
|
+
# not match.
|
242
|
+
#
|
243
|
+
# If a block is given, it will be run with the generated hash
|
244
|
+
# if the string matches. Alias for:
|
245
|
+
#
|
246
|
+
# if m = template.match(string)
|
247
|
+
# # Run block
|
248
|
+
# end
|
249
|
+
#
|
250
|
+
# Parameter: string the string to match
|
251
|
+
#
|
252
|
+
# Return: Information matched from the string or nil
|
253
|
+
describe '#match (depricated)' do
|
254
|
+
it 'returns the data generated from the string when the given string matches the template regex' do
|
255
|
+
expect(@template1.match @all_text).to eql @data1
|
256
|
+
end
|
257
|
+
|
258
|
+
it 'returns nil when the given string does not match the template regex' do
|
259
|
+
expect(@template1.match "").to be_nil
|
260
|
+
end
|
261
|
+
|
262
|
+
it 'passes the hash to a block if given' do
|
263
|
+
expect{ @template1.match(@all_text) { |hash| expect(hash).to eql @data1 } }.not_to raise_error
|
264
|
+
end
|
265
|
+
end
|
266
|
+
end
|
@@ -0,0 +1,147 @@
|
|
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::GIO
|
20
|
+
#
|
21
|
+
# Implements the general IO writer template
|
22
|
+
#
|
23
|
+
# Author: Anshul Kharbanda
|
24
|
+
# Created: 3 - 4 - 2016
|
25
|
+
describe General::GIO do
|
26
|
+
before :all do
|
27
|
+
@default_data = {place: "virginia"}
|
28
|
+
@applied_data = {name: "joe", food: "Joe's Schmoes", place: "kentucky"}
|
29
|
+
|
30
|
+
@gio1 = General::GIO.load("exp/templates/sample1" + General::GIO::EXTENSION)
|
31
|
+
@gio2 = General::GIO.load("exp/templates/sample2" + General::GIO::EXTENSION)
|
32
|
+
@gio3 = General::GIO.load("exp/templates/sample3" + General::GIO::EXTENSION)
|
33
|
+
|
34
|
+
@out1 = "exp/out/sample1.txt"
|
35
|
+
@out2 = "exp/out/sample2.txt"
|
36
|
+
@out3 = "exp/out/sample3.txt"
|
37
|
+
@outF = 3
|
38
|
+
|
39
|
+
@default_text1 = IO.read "exp/expected/default1.txt"
|
40
|
+
@default_text2 = IO.read "exp/expected/default2.txt"
|
41
|
+
@default_text3 = IO.read "exp/expected/default3.txt"
|
42
|
+
|
43
|
+
@applied_text1 = IO.read "exp/expected/applied1.txt"
|
44
|
+
@applied_text2 = IO.read "exp/expected/applied2.txt"
|
45
|
+
@applied_text3 = IO.read "exp/expected/applied3.txt"
|
46
|
+
end
|
47
|
+
|
48
|
+
# Describe General::GIO::load
|
49
|
+
#
|
50
|
+
# Loads a GIO from a file with the given path
|
51
|
+
#
|
52
|
+
# Parameter: path - the path of the file to load
|
53
|
+
#
|
54
|
+
# Return: GIO loaded from the file
|
55
|
+
describe "::load" do
|
56
|
+
it "creates a new GIO with the given template string" do
|
57
|
+
expect(@gio1).to be_an_instance_of General::GIO
|
58
|
+
expect(@gio2).to be_an_instance_of General::GIO
|
59
|
+
expect(@gio3).to be_an_instance_of General::GIO
|
60
|
+
end
|
61
|
+
|
62
|
+
it "cannot load GMeta file (with @@yield defined)" do
|
63
|
+
expect { General::GIO.load("exp/templates/sample0"+General::GIO::EXTENSION) }.to raise_error General::GError
|
64
|
+
end
|
65
|
+
|
66
|
+
it "cannot load GMeta file with multiple @@yield statements" do
|
67
|
+
expect { General::GIO.load("exp/templates/sampleE1"+General::GIO::EXTENSION) }.to raise_error General::GError
|
68
|
+
end
|
69
|
+
|
70
|
+
it "cannot load GIO in which @@extend statement is not at the beginning of the template" do
|
71
|
+
expect { General::GIO.load("exp/templates/sampleE2"+General::GIO::EXTENSION) }.to raise_error General::GError
|
72
|
+
end
|
73
|
+
|
74
|
+
it "cannot load GIO with multiple @@extend statements" do
|
75
|
+
expect { General::GIO.load("exp/templates/sampleE3"+General::GIO::EXTENSION) }.to raise_error General::GError
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
# Describe General::GIO#write
|
80
|
+
#
|
81
|
+
# Writes the template with the given data applied to the target stream
|
82
|
+
#
|
83
|
+
# Parameter: ios - if String, is the name of the file to write to
|
84
|
+
# if IO, is the stream to write to
|
85
|
+
# Parameter: data - the data to be applied (merges with defaults)
|
86
|
+
describe "#write" do
|
87
|
+
# -------------------------------------------DEFAULT-------------------------------------------
|
88
|
+
|
89
|
+
context "with target and default data" do
|
90
|
+
it "writes the default data to the file with the given filename" do
|
91
|
+
@gio1.write @out1, @default_data
|
92
|
+
@gio2.write @out2, @default_data
|
93
|
+
@gio3.write @out3, @default_data
|
94
|
+
|
95
|
+
expect(IO.read @out1).to eql @default_text1
|
96
|
+
expect(IO.read @out2).to eql @default_text2
|
97
|
+
expect(IO.read @out3).to eql @default_text3
|
98
|
+
end
|
99
|
+
|
100
|
+
it "writes the default data to the given file io" do
|
101
|
+
File.open(@out1, "w+") { |ios| @gio1.write ios, @default_data }
|
102
|
+
File.open(@out2, "w+") { |ios| @gio2.write ios, @default_data }
|
103
|
+
File.open(@out3, "w+") { |ios| @gio3.write ios, @default_data }
|
104
|
+
|
105
|
+
expect(IO.read @out1).to eql @default_text1
|
106
|
+
expect(IO.read @out2).to eql @default_text2
|
107
|
+
expect(IO.read @out3).to eql @default_text3
|
108
|
+
end
|
109
|
+
|
110
|
+
it "raises TypeError if target is not string or io" do
|
111
|
+
expect{ @gio1.write @outF, @default_data }.to raise_error TypeError
|
112
|
+
expect{ @gio2.write @outF, @default_data }.to raise_error TypeError
|
113
|
+
expect{ @gio3.write @outF, @default_data }.to raise_error TypeError
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
# --------------------------------------------DATA---------------------------------------------
|
118
|
+
|
119
|
+
context "with target and given data" do
|
120
|
+
it "writes the given data to the file with the given filename" do
|
121
|
+
@gio1.write @out1, @applied_data
|
122
|
+
@gio2.write @out2, @applied_data
|
123
|
+
@gio3.write @out3, @applied_data
|
124
|
+
|
125
|
+
expect(IO.read @out1).to eql @applied_text1
|
126
|
+
expect(IO.read @out2).to eql @applied_text2
|
127
|
+
expect(IO.read @out3).to eql @applied_text3
|
128
|
+
end
|
129
|
+
|
130
|
+
it "writes the given data to the given file io" do
|
131
|
+
File.open(@out1, "w+") { |ios| @gio1.write ios, @applied_data }
|
132
|
+
File.open(@out2, "w+") { |ios| @gio2.write ios, @applied_data }
|
133
|
+
File.open(@out3, "w+") { |ios| @gio3.write ios, @applied_data }
|
134
|
+
|
135
|
+
expect(IO.read @out1).to eql @applied_text1
|
136
|
+
expect(IO.read @out2).to eql @applied_text2
|
137
|
+
expect(IO.read @out3).to eql @applied_text3
|
138
|
+
end
|
139
|
+
|
140
|
+
it "raises TypeError if target is not string or io" do
|
141
|
+
expect{ @gio1.write @outF, @applied_data }.to raise_error TypeError
|
142
|
+
expect{ @gio2.write @outF, @applied_data }.to raise_error TypeError
|
143
|
+
expect{ @gio3.write @outF, @applied_data }.to raise_error TypeError
|
144
|
+
end
|
145
|
+
end
|
146
|
+
end
|
147
|
+
end
|
@@ -0,0 +1,77 @@
|
|
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::GTimeFormat
|
20
|
+
#
|
21
|
+
# A special template used for formatting time strings
|
22
|
+
#
|
23
|
+
# Author: Anshul Kharbanda
|
24
|
+
# Created: 7 - 2 - 2016
|
25
|
+
describe General::GTimeFormat do
|
26
|
+
before :all do
|
27
|
+
# Time value
|
28
|
+
@time = (11 + 3)*3600 + 42*60 + 9
|
29
|
+
|
30
|
+
# Phony value
|
31
|
+
@phony = {data: "Time, Yo!"}
|
32
|
+
|
33
|
+
# Formats
|
34
|
+
@format1 = General::GTimeFormat.new "@HH:@MM:@SS"
|
35
|
+
@format2 = General::GTimeFormat.new "@I:@MM:@S @A"
|
36
|
+
@format3 = General::GTimeFormat.new "@SS - @M - @H (@I @A)"
|
37
|
+
|
38
|
+
# Applied values
|
39
|
+
@applied1 = "14:42:09"
|
40
|
+
@applied2 = "3:42:9 PM"
|
41
|
+
@applied3 = "09 - 42 - 14 (3 PM)"
|
42
|
+
end
|
43
|
+
|
44
|
+
# Describe General::GTimeFormat::new
|
45
|
+
#
|
46
|
+
# Creates the GTimeFormat with the given string
|
47
|
+
#
|
48
|
+
# Parameter: string - the template string
|
49
|
+
describe "::new" do
|
50
|
+
it "creates a new GTimeFormat with the given format string" do
|
51
|
+
expect(@format1).to be_an_instance_of General::GTimeFormat
|
52
|
+
expect(@format2).to be_an_instance_of General::GTimeFormat
|
53
|
+
expect(@format3).to be_an_instance_of General::GTimeFormat
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
# Describe General::GTimeFormat#apply
|
58
|
+
#
|
59
|
+
# Applies the given integer value to the template and returns the generated string
|
60
|
+
#
|
61
|
+
# Parameter: value - the value to be applied (as a hash. merges with defaults)
|
62
|
+
#
|
63
|
+
# Return: string of the template with the given value applied
|
64
|
+
describe "#apply" do
|
65
|
+
it "returns the given integer time value value formatted according to the time format" do
|
66
|
+
expect(@format1.apply(@time)).to eql @applied1
|
67
|
+
expect(@format2.apply(@time)).to eql @applied2
|
68
|
+
expect(@format3.apply(@time)).to eql @applied3
|
69
|
+
end
|
70
|
+
|
71
|
+
it "raises TypeError if value given is not integer" do
|
72
|
+
expect{ @format1.apply(@phony) }.to raise_error TypeError
|
73
|
+
expect{ @format2.apply(@phony) }.to raise_error TypeError
|
74
|
+
expect{ @format3.apply(@phony) }.to raise_error TypeError
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
data/spec/spec_require.rb
CHANGED
@@ -16,10 +16,13 @@
|
|
16
16
|
|
17
17
|
require_relative "../lib/gdothash"
|
18
18
|
require_relative "../lib/goperations"
|
19
|
+
require_relative "../lib/gerror"
|
19
20
|
require_relative "../lib/gpartials/gpartial"
|
20
21
|
require_relative "../lib/gpartials/gtext"
|
21
22
|
require_relative "../lib/gpartials/gplaceholder"
|
22
|
-
require_relative "../lib/gpartials/
|
23
|
-
require_relative "../lib/
|
24
|
-
require_relative "../lib/
|
25
|
-
require_relative "../lib/
|
23
|
+
require_relative "../lib/gpartials/garrayplaceholder"
|
24
|
+
require_relative "../lib/gpartials/gfullplaceholder"
|
25
|
+
require_relative "../lib/gpartials/gtimeformatplaceholder"
|
26
|
+
require_relative "../lib/gtemplates/gtemplate"
|
27
|
+
require_relative "../lib/gtemplates/gtimeformat"
|
28
|
+
require_relative "../lib/gtemplates/gio"
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: general
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Anshul Kharbanda
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-02-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|
@@ -36,27 +36,55 @@ files:
|
|
36
36
|
- LICENSE
|
37
37
|
- README.md
|
38
38
|
- Rakefile
|
39
|
-
- exp/
|
40
|
-
- exp/
|
41
|
-
- exp/
|
39
|
+
- exp/expected/applied1.txt
|
40
|
+
- exp/expected/applied2.txt
|
41
|
+
- exp/expected/applied3.txt
|
42
|
+
- exp/expected/default1.txt
|
43
|
+
- exp/expected/default2.txt
|
44
|
+
- exp/expected/default3.txt
|
45
|
+
- exp/out/sample1.txt
|
46
|
+
- exp/out/sample2.txt
|
47
|
+
- exp/out/sample3.txt
|
48
|
+
- exp/templates/sample0.general
|
49
|
+
- exp/templates/sample1.general
|
50
|
+
- exp/templates/sample2.general
|
51
|
+
- exp/templates/sample3.general
|
52
|
+
- exp/templates/sampleE0.general
|
53
|
+
- exp/templates/sampleE1.general
|
54
|
+
- exp/templates/sampleE2.general
|
55
|
+
- exp/templates/sampleE3.general
|
42
56
|
- lib/gdothash.rb
|
43
57
|
- lib/general.rb
|
58
|
+
- lib/gerror.rb
|
44
59
|
- lib/goperations.rb
|
60
|
+
- lib/gpartials/garrayplaceholder.rb
|
61
|
+
- lib/gpartials/gfullplaceholder.rb
|
45
62
|
- lib/gpartials/gpartial.rb
|
46
63
|
- lib/gpartials/gplaceholder.rb
|
64
|
+
- lib/gpartials/gspecial.rb
|
47
65
|
- lib/gpartials/gtext.rb
|
48
66
|
- lib/gpartials/gtimeformatplaceholder.rb
|
49
|
-
- lib/
|
50
|
-
- lib/
|
51
|
-
- lib/
|
52
|
-
- lib/
|
53
|
-
-
|
67
|
+
- lib/gprepartials/gextend.rb
|
68
|
+
- lib/gprepartials/ginclude.rb
|
69
|
+
- lib/gprepartials/gprepartial.rb
|
70
|
+
- lib/gprepartials/gpretext.rb
|
71
|
+
- lib/gprepartials/gyield.rb
|
72
|
+
- lib/gtemplates/gbasetemplate.rb
|
73
|
+
- lib/gtemplates/gio.rb
|
74
|
+
- lib/gtemplates/gmeta.rb
|
75
|
+
- lib/gtemplates/gtemplate.rb
|
76
|
+
- lib/gtemplates/gtimeformat.rb
|
54
77
|
- spec/gdothash_spec.rb
|
55
78
|
- spec/goperations_spec.rb
|
79
|
+
- spec/gpartial_garrayplaceholder_spec.rb
|
80
|
+
- spec/gpartial_gfullplaceholder_spec.rb
|
81
|
+
- spec/gpartial_gplaceholder_spec.rb
|
82
|
+
- spec/gpartial_gspecial_spec.rb
|
83
|
+
- spec/gpartial_gtext_spec.rb
|
56
84
|
- spec/gpartial_spec.rb
|
57
|
-
- spec/
|
58
|
-
- spec/
|
59
|
-
- spec/
|
85
|
+
- spec/gtamplate_gtemplate_spec.rb
|
86
|
+
- spec/gtemplate_gio_spec.rb
|
87
|
+
- spec/gtemplate_gtimeformat_spec.rb
|
60
88
|
- spec/spec_require.rb
|
61
89
|
homepage: https://andydevs.github.io/general
|
62
90
|
licenses:
|
data/exp/future.general
DELETED
data/exp/out.txt
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
There once was a chef name Joe, and he loved eating Joe's Schmoes. He went to Kentucky.
|
data/exp/sample.general
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
There once was a chef name @(name: gordon ramsay -> capitalize all), and he loved eating @(food: carrots). He went to @(place -> capitalize).
|
data/lib/templates/gio.rb
DELETED
@@ -1,68 +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 "gtemplate"
|
18
|
-
|
19
|
-
# General is a templating system in ruby
|
20
|
-
#
|
21
|
-
# Author: Anshul Kharbanda
|
22
|
-
# Created: 3 - 4 - 2016
|
23
|
-
module General
|
24
|
-
# Implements the general IO writer template
|
25
|
-
#
|
26
|
-
# Author: Anshul Kharbanda
|
27
|
-
# Created: 3 - 4 - 2016
|
28
|
-
class GIO < GTemplate
|
29
|
-
# The general file extension
|
30
|
-
EXTENSION = ".general"
|
31
|
-
|
32
|
-
# Public read source
|
33
|
-
attr :source
|
34
|
-
|
35
|
-
# Loads a GIO from a file with the given path
|
36
|
-
#
|
37
|
-
# Parameter: path - the path of the file to load
|
38
|
-
#
|
39
|
-
# Return: GIO loaded from the file
|
40
|
-
def self.load path
|
41
|
-
self.new IO.read(path), path
|
42
|
-
end
|
43
|
-
|
44
|
-
# Creates a GIO with the given template string and source filename
|
45
|
-
#
|
46
|
-
# Parameter: string - the string being converted to a template
|
47
|
-
# Parameter: source - the name of the source file of the template
|
48
|
-
def initialize string, source=nil
|
49
|
-
super string
|
50
|
-
@source = source
|
51
|
-
end
|
52
|
-
|
53
|
-
# Writes the template with the given data applied to the target stream
|
54
|
-
#
|
55
|
-
# Parameter: ios - if String, is the name of the file to write to
|
56
|
-
# if IO, is the stream to write to
|
57
|
-
# Parameter: data - the data to be applied (merges with defaults)
|
58
|
-
def write ios, data={}
|
59
|
-
if ios.is_a? String
|
60
|
-
IO.write ios, apply(data)
|
61
|
-
elsif ios.is_a? IO
|
62
|
-
ios.write apply(data)
|
63
|
-
else
|
64
|
-
raise TypeError.new "Expected IO or String, got: #{ios.class}"
|
65
|
-
end
|
66
|
-
end
|
67
|
-
end
|
68
|
-
end
|