general 1.4.2 → 1.5.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/LICENSE +674 -0
- data/README.md +135 -0
- data/Rakefile +52 -0
- data/exp/future.general +11 -0
- data/exp/out.txt +1 -0
- data/exp/sample.general +1 -0
- data/lib/gdothash.rb +106 -0
- data/lib/general.rb +3 -3
- data/lib/goperations.rb +49 -28
- data/lib/gpartials/gpartial.rb +43 -0
- data/lib/gpartials/gplaceholder.rb +156 -0
- data/lib/gpartials/gtext.rb +93 -0
- data/lib/gpartials/gtimeformatplaceholder.rb +71 -0
- data/lib/templates/gbasetemplate.rb +55 -0
- data/lib/{gio.rb → templates/gio.rb} +1 -2
- data/lib/templates/gtemplate.rb +103 -0
- data/lib/templates/gtimeformat.rb +62 -0
- data/spec/garrayplaceholder_spec.rb +163 -0
- data/spec/gdothash_spec.rb +87 -0
- data/spec/goperations_spec.rb +246 -0
- data/spec/gpartial_spec.rb +54 -0
- data/spec/gplaceholder_spec.rb +300 -0
- data/spec/gtemplates_spec.rb +480 -0
- data/spec/gtext_spec.rb +150 -0
- data/spec/spec_require.rb +25 -3
- metadata +24 -8
- data/lib/gpartials.rb +0 -191
- data/lib/gtemplate.rb +0 -118
- data/lib/gtimeformat.rb +0 -53
- data/spec/gio_spec.rb +0 -64
- data/spec/gtemplate_spec.rb +0 -93
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: 1.
|
4
|
+
version: 1.5.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: 2016-
|
11
|
+
date: 2016-11-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|
@@ -33,14 +33,30 @@ executables: []
|
|
33
33
|
extensions: []
|
34
34
|
extra_rdoc_files: []
|
35
35
|
files:
|
36
|
+
- LICENSE
|
37
|
+
- README.md
|
38
|
+
- Rakefile
|
39
|
+
- exp/future.general
|
40
|
+
- exp/out.txt
|
41
|
+
- exp/sample.general
|
42
|
+
- lib/gdothash.rb
|
36
43
|
- lib/general.rb
|
37
|
-
- lib/gio.rb
|
38
44
|
- lib/goperations.rb
|
39
|
-
- lib/gpartials.rb
|
40
|
-
- lib/
|
41
|
-
- lib/
|
42
|
-
-
|
43
|
-
-
|
45
|
+
- lib/gpartials/gpartial.rb
|
46
|
+
- lib/gpartials/gplaceholder.rb
|
47
|
+
- lib/gpartials/gtext.rb
|
48
|
+
- lib/gpartials/gtimeformatplaceholder.rb
|
49
|
+
- lib/templates/gbasetemplate.rb
|
50
|
+
- lib/templates/gio.rb
|
51
|
+
- lib/templates/gtemplate.rb
|
52
|
+
- lib/templates/gtimeformat.rb
|
53
|
+
- spec/garrayplaceholder_spec.rb
|
54
|
+
- spec/gdothash_spec.rb
|
55
|
+
- spec/goperations_spec.rb
|
56
|
+
- spec/gpartial_spec.rb
|
57
|
+
- spec/gplaceholder_spec.rb
|
58
|
+
- spec/gtemplates_spec.rb
|
59
|
+
- spec/gtext_spec.rb
|
44
60
|
- spec/spec_require.rb
|
45
61
|
homepage: https://andydevs.github.io/general
|
46
62
|
licenses:
|
data/lib/gpartials.rb
DELETED
@@ -1,191 +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
|
-
# General is a templating system in ruby
|
18
|
-
#
|
19
|
-
# Author: Anshul Kharbanda
|
20
|
-
# Created: 3 - 4 - 2016
|
21
|
-
module General
|
22
|
-
private
|
23
|
-
|
24
|
-
# Represents a plain string partial in a GTemplate
|
25
|
-
#
|
26
|
-
# Author: Anshul Kharbanda
|
27
|
-
# Created: 7 - 1 - 2016
|
28
|
-
class GPartialString
|
29
|
-
# Initializes the GPartialString with the given string
|
30
|
-
#
|
31
|
-
# Parameter: string - the string value of the GPartialString
|
32
|
-
def initialize(string, match=nil)
|
33
|
-
@string = match ? string[0...match.begin(0)] : string
|
34
|
-
end
|
35
|
-
|
36
|
-
# Returns the string
|
37
|
-
#
|
38
|
-
# Returns: the string
|
39
|
-
def apply(data); @string; end
|
40
|
-
|
41
|
-
# Returns the string
|
42
|
-
#
|
43
|
-
# Returns: the string
|
44
|
-
def to_s; @string; end
|
45
|
-
end
|
46
|
-
|
47
|
-
# Represents a placeholder partial in a GTemplate
|
48
|
-
#
|
49
|
-
# Author: Anshul Kharbanda
|
50
|
-
# Created: 7 - 1 - 2016
|
51
|
-
class GPlaceholder
|
52
|
-
# Regular expression that matches placeholders
|
53
|
-
REGEX = /@\((?<name>[a-zA-Z]\w*)\s*(\:\s*(?<default>.*?))?\s*(->\s*(?<operation>[\w+\s+]+))?\)/
|
54
|
-
|
55
|
-
# Read name
|
56
|
-
attr :name
|
57
|
-
|
58
|
-
# Initializes the GPlaceholder with the given match
|
59
|
-
#
|
60
|
-
# Parameter: match - the match data from the string being parsed
|
61
|
-
# Parameter: defaults - the hash of default data from the GTemplate
|
62
|
-
def initialize match, defaults
|
63
|
-
@name = match[:name].to_sym
|
64
|
-
@operation = match[:operation]
|
65
|
-
@defaults = defaults
|
66
|
-
end
|
67
|
-
|
68
|
-
# Returns the value of the placeholder in the given data
|
69
|
-
# with the given operation performed on it
|
70
|
-
#
|
71
|
-
# Parameter: data - the data being applied
|
72
|
-
#
|
73
|
-
# Return: the value of the placeholder in the given data
|
74
|
-
# with the given operation performed on it
|
75
|
-
def apply data
|
76
|
-
# Get value from either data or defaults
|
77
|
-
value = data[@name] || @defaults[@name]
|
78
|
-
|
79
|
-
# Return value (operation performed if one is defined)
|
80
|
-
return (@operation ? General::GOperations.send(@operation, value) : value).to_s
|
81
|
-
end
|
82
|
-
|
83
|
-
# Returns the string representation of the placeholder
|
84
|
-
#
|
85
|
-
# Return: the string representation of the placeholder
|
86
|
-
def to_s
|
87
|
-
str = "@(#{@name}"
|
88
|
-
str += ": #{@defaults[@name]}" if @defaults[@name]
|
89
|
-
str += "-> #{@operation}" if @operation
|
90
|
-
return str + ")"
|
91
|
-
end
|
92
|
-
end
|
93
|
-
|
94
|
-
# Represents an array placeholder partial in a GTemplate
|
95
|
-
#
|
96
|
-
# Author: Anshul Kharbanda
|
97
|
-
# Created: 7 - 1 - 2016
|
98
|
-
class GArrayPlaceholder
|
99
|
-
# Regular expression that matches array placeholders
|
100
|
-
REGEX = /@\[(?<name>[a-zA-Z]\w*)\]( +|\n+)?(?<text>.*?)( +|\n+)?@\[(?<delimeter>.+)?\]/m
|
101
|
-
|
102
|
-
# Default delimeter
|
103
|
-
DEFAULT_DELIMETER = " "
|
104
|
-
|
105
|
-
# Read name
|
106
|
-
attr :name
|
107
|
-
|
108
|
-
# Initializes the GPlaceholder with the given match
|
109
|
-
#
|
110
|
-
# Parameter: match - the match data from the string being parsed
|
111
|
-
def initialize match
|
112
|
-
@name = match[:name].to_sym
|
113
|
-
@delimeter = match[:delimeter] || DEFAULT_DELIMETER
|
114
|
-
@template = General::GTemplate.new match[:text]
|
115
|
-
end
|
116
|
-
|
117
|
-
# Returns the value of the array placeholder in the given data
|
118
|
-
# formatted by the given GTemplate and joined by the given delimeter
|
119
|
-
#
|
120
|
-
# Parameter: data - the data being applied
|
121
|
-
#
|
122
|
-
# Return: the value of the array placeholder in the given data
|
123
|
-
# formatted by the given GTemplate and joined by the given delimeter
|
124
|
-
def apply(data); @template.apply_all(data[@name]).join(@delimeter); end
|
125
|
-
|
126
|
-
# Returns the string representation of the array placeholder
|
127
|
-
#
|
128
|
-
# Return: the string representation of the array placeholder
|
129
|
-
def to_s; "@[#{@name}] #{@template.to_s} @[#{@delimeter.inspect}]"; end
|
130
|
-
end
|
131
|
-
|
132
|
-
# Represents an timeformat placeholder partial in a GTimeFormat
|
133
|
-
#
|
134
|
-
# Author: Anshul Kharbanda
|
135
|
-
# Created: 7 - 1 - 2016
|
136
|
-
class GTimeFormatPlaceholder
|
137
|
-
# Regular expression that matches timeformat placeholders
|
138
|
-
REGEX = /@(?<type>[A-Z]+)/
|
139
|
-
|
140
|
-
# Read type
|
141
|
-
attr :type
|
142
|
-
|
143
|
-
# Initializes the GTimeFormatPlaceholder with the given match
|
144
|
-
#
|
145
|
-
# Parameter: match - the match data from the string being parsed
|
146
|
-
def initialize(match); @type = match[:type]; end
|
147
|
-
|
148
|
-
# Returns the value of the timeformat placeholder in the given time value
|
149
|
-
# formatted according to the time format type
|
150
|
-
#
|
151
|
-
# Parameter: value - the time value being applied
|
152
|
-
#
|
153
|
-
# Return: the value of the timeformat placeholder in the given time value
|
154
|
-
# formatted according to the time format type
|
155
|
-
def apply value
|
156
|
-
if value.is_a? Integer
|
157
|
-
map = type_map(value).to_s
|
158
|
-
return is_justify? ? map.rjust(@type.length, '0') : map
|
159
|
-
else
|
160
|
-
raise TypeError.new "Expected Integer, got: #{value.class}"
|
161
|
-
end
|
162
|
-
end
|
163
|
-
|
164
|
-
# Returns true if the timeformat placeholder is a justifiable type
|
165
|
-
#
|
166
|
-
# Return: true if the timeformat placeholder is a justifiable type
|
167
|
-
def is_justify?; "HIMS".include? @type[0]; end
|
168
|
-
|
169
|
-
# Returns the string representation of the timeformat placeholder
|
170
|
-
#
|
171
|
-
# Return: the string representation of the timeformat placeholder
|
172
|
-
def to_s; return "@#{@type}"; end
|
173
|
-
|
174
|
-
private
|
175
|
-
|
176
|
-
# Returns the value modified according to the raw timeformat type
|
177
|
-
#
|
178
|
-
# Parameter: value - the time value being applied
|
179
|
-
#
|
180
|
-
# Return: the value modified according to the raw timeformat type
|
181
|
-
def type_map value
|
182
|
-
case @type[0]
|
183
|
-
when "H" then return (value / 3600)
|
184
|
-
when "I" then return (value / 3600 % 12 + 1)
|
185
|
-
when "M" then return (value % 3600 / 60)
|
186
|
-
when "S" then return (value % 3600 % 60)
|
187
|
-
when "A" then return (value / 3600 > 11 ? 'PM' : 'AM')
|
188
|
-
end
|
189
|
-
end
|
190
|
-
end
|
191
|
-
end
|
data/lib/gtemplate.rb
DELETED
@@ -1,118 +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 "goperations"
|
18
|
-
require_relative "gpartials"
|
19
|
-
|
20
|
-
# General is a templating system in ruby
|
21
|
-
#
|
22
|
-
# Author: Anshul Kharbanda
|
23
|
-
# Created: 3 - 4 - 2016
|
24
|
-
module General
|
25
|
-
# Implements the general templating system for strings
|
26
|
-
#
|
27
|
-
# Author: Anshul Kharbanda
|
28
|
-
# Created: 3 - 4 - 2016
|
29
|
-
class GTemplate
|
30
|
-
# Creates a GTemplate with the given template string
|
31
|
-
#
|
32
|
-
# Parameter: string - the string being converted to a template
|
33
|
-
def initialize string
|
34
|
-
# The string gets split into partials by placeholder and array template
|
35
|
-
@partials = []
|
36
|
-
@defaults = {}
|
37
|
-
|
38
|
-
parse_string string
|
39
|
-
end
|
40
|
-
|
41
|
-
# Returns a string representation of the string
|
42
|
-
#
|
43
|
-
# Return: a string representation of the string
|
44
|
-
def to_s; @partials.collect(&:to_s).join; end
|
45
|
-
|
46
|
-
# Applies the given data to the template and returns the generated string
|
47
|
-
#
|
48
|
-
# Parameter: data - the data to be applied (as a hash. merges with defaults)
|
49
|
-
#
|
50
|
-
# Return: string of the template with the given data applied
|
51
|
-
def apply(data={}); @partials.collect { |partial| partial.apply(data) }.join; end
|
52
|
-
|
53
|
-
# Applies each data structure in the array independently to the template
|
54
|
-
# and returns an array of the generated strings
|
55
|
-
#
|
56
|
-
# Parameter: array - the array of data to be applied
|
57
|
-
# (each data hash will be merged with defaults)
|
58
|
-
#
|
59
|
-
# Return: array of strings generated from the template with the given data applied
|
60
|
-
def apply_all(array); array.collect { |data| apply(data) }; end
|
61
|
-
|
62
|
-
private
|
63
|
-
|
64
|
-
# Returns true if given string has a placeholder
|
65
|
-
#
|
66
|
-
# Parameter: string - the string to check for a placeholder
|
67
|
-
#
|
68
|
-
# Return: true if given string has a placeholder
|
69
|
-
def has_next string
|
70
|
-
next_p = General::GPlaceholder::REGEX =~ string
|
71
|
-
next_a = General::GArrayPlaceholder::REGEX =~ string
|
72
|
-
return !((next_p.nil? || next_p < 0) && (next_a.nil? || next_a < 0))
|
73
|
-
end
|
74
|
-
|
75
|
-
# Returns true if the next placeholder is an aray placeholder
|
76
|
-
#
|
77
|
-
# Parameter: string - the string to check for an array placeholder
|
78
|
-
#
|
79
|
-
# Return: true if the next placeholder is an aray placeholder
|
80
|
-
def next_array_placeholder string
|
81
|
-
next_p = General::GPlaceholder::REGEX =~ string
|
82
|
-
next_a = General::GArrayPlaceholder::REGEX =~ string
|
83
|
-
return !(next_a.nil? || next_p.nil?) && next_a < next_p
|
84
|
-
end
|
85
|
-
|
86
|
-
# Parses the string into General template data
|
87
|
-
#
|
88
|
-
# Parameter: string - the string to parse
|
89
|
-
def parse_string string
|
90
|
-
# While there is still a placeholder or array placeholder in string
|
91
|
-
while has_next string
|
92
|
-
# If next is array placeholder, process array placeholder
|
93
|
-
if next_array_placeholder string
|
94
|
-
# Split match and add partials
|
95
|
-
match = General::GArrayPlaceholder::REGEX.match string
|
96
|
-
@partials << General::GPartialString.new(string, match) \
|
97
|
-
<< General::GArrayPlaceholder.new(match)
|
98
|
-
|
99
|
-
# Else process placeholder
|
100
|
-
else
|
101
|
-
# Split match and add partials
|
102
|
-
match = General::GPlaceholder::REGEX.match string
|
103
|
-
@partials << General::GPartialString.new(string, match) \
|
104
|
-
<< General::GPlaceholder.new(match, @defaults)
|
105
|
-
|
106
|
-
# Push default information
|
107
|
-
@defaults[match[:name].to_sym] ||= match[:default]
|
108
|
-
end
|
109
|
-
|
110
|
-
# Trim string
|
111
|
-
string = string[match.end(0)..-1]
|
112
|
-
end
|
113
|
-
|
114
|
-
# Add end of string
|
115
|
-
@partials << General::GPartialString.new(string)
|
116
|
-
end
|
117
|
-
end
|
118
|
-
end
|
data/lib/gtimeformat.rb
DELETED
@@ -1,53 +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
|
-
require_relative "gpartials"
|
19
|
-
|
20
|
-
# General is a templating system in ruby
|
21
|
-
#
|
22
|
-
# Author: Anshul Kharbanda
|
23
|
-
# Created: 3 - 4 - 2016
|
24
|
-
module General
|
25
|
-
# A special template used for formatting time strings
|
26
|
-
#
|
27
|
-
# Author: Anshul Kharbanda
|
28
|
-
# Created: 7 - 2 - 2016
|
29
|
-
class GTimeFormat < GTemplate
|
30
|
-
private
|
31
|
-
|
32
|
-
# Parses the given string into GTimeFormat partials
|
33
|
-
#
|
34
|
-
# Parameter: string - the string to parse
|
35
|
-
def parse_string string
|
36
|
-
# While a GTimeFormatPlaceholder remains
|
37
|
-
while !(General::GTimeFormatPlaceholder::REGEX =~ string).nil? \
|
38
|
-
&& (General::GTimeFormatPlaceholder::REGEX =~ string) > -1
|
39
|
-
|
40
|
-
# Split match and add partials
|
41
|
-
match = General::GTimeFormatPlaceholder::REGEX.match string
|
42
|
-
@partials << General::GPartialString.new(string, match) \
|
43
|
-
<< General::GTimeFormatPlaceholder.new(match)
|
44
|
-
|
45
|
-
# Trim string
|
46
|
-
string = string[match.end(0)..-1]
|
47
|
-
end
|
48
|
-
|
49
|
-
# Add end partial of string
|
50
|
-
@partials << General::GPartialString.new(string)
|
51
|
-
end
|
52
|
-
end
|
53
|
-
end
|
data/spec/gio_spec.rb
DELETED
@@ -1,64 +0,0 @@
|
|
1
|
-
require "spec_require"
|
2
|
-
|
3
|
-
describe General::GIO do
|
4
|
-
before :all do
|
5
|
-
@gio = General::GIO.load("exp/sample" + General::GIO::EXTENSION)
|
6
|
-
@out = "exp/out.txt"
|
7
|
-
@phony = 3
|
8
|
-
@data = {name: "joe", food: "Joe's Schmoes"}
|
9
|
-
@default_text = "There once was a chef name Gordon Ramsay, and he loved eating carrots."
|
10
|
-
@applied_text = "There once was a chef name Joe, and he loved eating Joe's Schmoes."
|
11
|
-
end
|
12
|
-
|
13
|
-
describe "#new" do
|
14
|
-
it "creates a new GIO with the given template string" do
|
15
|
-
expect(@gio).to be_an_instance_of General::GIO
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
describe "#write" do
|
20
|
-
context "with target and no data" do
|
21
|
-
context 'if target is string' do
|
22
|
-
it "writes the default data to the file with the given filename (the target string)" do
|
23
|
-
@gio.write @out
|
24
|
-
expect(IO.read(@out)).to eql @default_text
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
context 'if target is io' do
|
29
|
-
it "writes the default data to the target io" do
|
30
|
-
File.open(@out, "w+") { |ios| @gio.write ios }
|
31
|
-
expect(IO.read(@out)).to eql @default_text
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
context 'if target is not string or io' do
|
36
|
-
it "raises TypeError" do
|
37
|
-
expect{ @gio.write(@phony) }.to raise_error TypeError
|
38
|
-
end
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
context "with target and given data" do
|
43
|
-
context 'if target is string' do
|
44
|
-
it "writes the given data to the file with the given filename (the target string)" do
|
45
|
-
@gio.write @out, @data
|
46
|
-
expect(IO.read(@out)).to eql @applied_text
|
47
|
-
end
|
48
|
-
end
|
49
|
-
|
50
|
-
context 'if target is io' do
|
51
|
-
it "writes the given data to the target io" do
|
52
|
-
File.open(@out, "w+") { |ios| @gio.write ios, @data }
|
53
|
-
expect(IO.read(@out)).to eql @applied_text
|
54
|
-
end
|
55
|
-
end
|
56
|
-
|
57
|
-
context 'if target is not string or io' do
|
58
|
-
it "raises TypeError" do
|
59
|
-
expect{ @gio.write(@phony, @data) }.to raise_error TypeError
|
60
|
-
end
|
61
|
-
end
|
62
|
-
end
|
63
|
-
end
|
64
|
-
end
|
data/spec/gtemplate_spec.rb
DELETED
@@ -1,93 +0,0 @@
|
|
1
|
-
require "spec_require"
|
2
|
-
|
3
|
-
describe General::GTemplate do
|
4
|
-
before :all do
|
5
|
-
# General template test
|
6
|
-
@template1 = General::GTemplate.new "There once was a man named @(name: Gordon Ramsay). @(name) loved @(food: Cat Food)!"
|
7
|
-
@default_text = "There once was a man named Gordon Ramsay. Gordon Ramsay loved Cat Food!"
|
8
|
-
@all_applied_text = "There once was a man named Joe. Joe loved Joe's Shmoes!"
|
9
|
-
@name_applied_text = "There once was a man named Dog. Dog loved Cat Food!"
|
10
|
-
@food_applied_text = "There once was a man named Gordon Ramsay. Gordon Ramsay loved Denny's Fennies!"
|
11
|
-
@data1 = {name: "Joe", food: "Joe's Shmoes"}
|
12
|
-
@name = "Dog"
|
13
|
-
@food = "Denny's Fennies"
|
14
|
-
|
15
|
-
# General array template test
|
16
|
-
@template2 = General::GTemplate.new "@[greetings] Hello, @(name)! How is the @(pet)? @[\n]"
|
17
|
-
@data2 = {greetings: [
|
18
|
-
{name: "Ben", pet: "dog"},
|
19
|
-
{name: "Jen", pet: "cat"},
|
20
|
-
{name: "Ken", pet: "plant"}
|
21
|
-
]}
|
22
|
-
@applied_text2 = "Hello, Ben! How is the dog?\nHello, Jen! How is the cat?\nHello, Ken! How is the plant?"
|
23
|
-
|
24
|
-
# General array template and placeholders test
|
25
|
-
@template3 = General::GTemplate.new "@(film: The Dark Knight)\nCrew:\n@[crew] \t@(name): @(role) @[\n]\nScore: @(score)/10"
|
26
|
-
@data3 = {
|
27
|
-
film: 'Batman Begins',
|
28
|
-
crew: [
|
29
|
-
{name: 'David S Goyer', role: 'Writer'},
|
30
|
-
{name: 'Chris Nolan', role: 'Director'},
|
31
|
-
{name: 'Wally Pfister', role: 'Director of Photography'},
|
32
|
-
{name: 'Michael Caine', role: 'Alfred Pennyworth'},
|
33
|
-
{name: 'Christian Bale', role: 'Bruce Wayne/Batman'}
|
34
|
-
],
|
35
|
-
score: 10
|
36
|
-
}
|
37
|
-
@applied_text3 = "Batman Begins\nCrew:\n\tDavid S Goyer: Writer\n\tChris Nolan: Director\n\tWally Pfister: Director of Photography" \
|
38
|
-
"\n\tMichael Caine: Alfred Pennyworth\n\tChristian Bale: Bruce Wayne/Batman\nScore: 10/10"
|
39
|
-
|
40
|
-
# General operations test
|
41
|
-
@template4 = General::GTemplate.new "There once was a dog named @(name: dog -> capitalize). @(name -> capitalize) earned @(amount -> dollars) last week."
|
42
|
-
@data4 = {name: "cat", amount: 19999}
|
43
|
-
@applied_text4 = "There once was a dog named Cat. Cat earned $199.99 last week."
|
44
|
-
end
|
45
|
-
|
46
|
-
describe "#new" do
|
47
|
-
it "Creates a new GTemplate with the given template string" do
|
48
|
-
expect(@template1).to be_an_instance_of General::GTemplate
|
49
|
-
expect(@template2).to be_an_instance_of General::GTemplate
|
50
|
-
expect(@template3).to be_an_instance_of General::GTemplate
|
51
|
-
expect(@template4).to be_an_instance_of General::GTemplate
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
|
-
describe "#apply" do
|
56
|
-
context "with no data" do
|
57
|
-
it "returns the template with the default data applied" do
|
58
|
-
expect(@template1.apply).to eql @default_text
|
59
|
-
end
|
60
|
-
end
|
61
|
-
|
62
|
-
context "with partial data" do
|
63
|
-
it "returns the template with the given data applied to corresponding placeholders and default data applied to the rest" do
|
64
|
-
expect(@template1.apply(name: @name)).to eql @name_applied_text
|
65
|
-
expect(@template1.apply(food: @food)).to eql @food_applied_text
|
66
|
-
end
|
67
|
-
end
|
68
|
-
|
69
|
-
context "with all data" do
|
70
|
-
it "returns the template with the given data applied" do
|
71
|
-
expect(@template1.apply(@data1)).to eql @all_applied_text
|
72
|
-
end
|
73
|
-
end
|
74
|
-
|
75
|
-
context "with array template" do
|
76
|
-
it "returns the template with the given array data applied and formatted according to the template" do
|
77
|
-
expect(@template2.apply(@data2)).to eql @applied_text2
|
78
|
-
end
|
79
|
-
end
|
80
|
-
|
81
|
-
context "with array template and regular placeholder" do
|
82
|
-
it "returns the template with the given data applied (including array data) and formatted according to the template" do
|
83
|
-
expect(@template3.apply(@data3)).to eql @applied_text3
|
84
|
-
end
|
85
|
-
end
|
86
|
-
|
87
|
-
context "with placeholder operation" do
|
88
|
-
it "returns the template with the given array data applied and formatted according to the format operations" do
|
89
|
-
expect(@template4.apply(@data4)).to eql @applied_text4
|
90
|
-
end
|
91
|
-
end
|
92
|
-
end
|
93
|
-
end
|