general 1.2.6 → 1.2.7
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/lib/general.rb +16 -0
- data/lib/goperations.rb +95 -0
- data/lib/gpartials.rb +127 -0
- data/lib/gtemplate.rb +36 -95
- data/spec/gfile_spec.rb +2 -3
- data/spec/gtemplate_spec.rb +31 -6
- metadata +8 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a561694d495a4822d409b79ee059bed5a7aa2d70
|
4
|
+
data.tar.gz: 77160aa8d9d951cf91988cd482796c50b564ff72
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9cf196acb87edb9afe1f21ed2dbc4754bef11395999d0776fe48563e6f491328ac347a0ad5a23e24221324abefdbb931de087df3dfcc39b4fe0aa54024c49b52
|
7
|
+
data.tar.gz: d6d2e2bd5869732dc2677f9176d6bd9796e0743d863c11aa097d3245923877e97e2b6a95683be8ab9219d045da70d72d6959d0f46a8639f95517c9b24b44b7b3
|
data/lib/general.rb
CHANGED
@@ -1,2 +1,18 @@
|
|
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
|
+
|
1
17
|
require_relative "gtemplate"
|
2
18
|
require_relative "gfile"
|
data/lib/goperations.rb
ADDED
@@ -0,0 +1,95 @@
|
|
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
|
+
# Implements placeholder operations
|
23
|
+
#
|
24
|
+
# Author: Anshul Kharbanda
|
25
|
+
# Created: 6 - 3 - 2016
|
26
|
+
module GOperations
|
27
|
+
#-----------------------------------STRING OPERATIONS------------------------------------
|
28
|
+
|
29
|
+
# Capitalizes every word in the string
|
30
|
+
#
|
31
|
+
# Parameter: string - the string being capitalized
|
32
|
+
#
|
33
|
+
# Return: the capitalized string
|
34
|
+
def self.capitalize string
|
35
|
+
return string.split(' ').collect(&:capitalize).join(' ')
|
36
|
+
end
|
37
|
+
|
38
|
+
# Converts every letter in the string to uppercase
|
39
|
+
#
|
40
|
+
# Parameter: string - the string being uppercased
|
41
|
+
#
|
42
|
+
# Return: the uppercased string
|
43
|
+
def self.uppercase string
|
44
|
+
return string.upcase
|
45
|
+
end
|
46
|
+
|
47
|
+
# Converts every letter in the string to lowercase
|
48
|
+
#
|
49
|
+
# Parameter: string - the string being lowercased
|
50
|
+
#
|
51
|
+
# Return: the lowercased string
|
52
|
+
def self.lowercase string
|
53
|
+
return string.downcase
|
54
|
+
end
|
55
|
+
|
56
|
+
#-----------------------------------INTEGER OPERATIONS------------------------------------
|
57
|
+
|
58
|
+
# Returns the integer monetary value (in cents) formatted to USD
|
59
|
+
#
|
60
|
+
# Parameter: integer - the integer being formatted (representing the amount in cents)
|
61
|
+
#
|
62
|
+
# Return: the formatted USD amount
|
63
|
+
def self.dollars integer
|
64
|
+
if integer < 0
|
65
|
+
return "-$" + (integer * -0.01).to_s
|
66
|
+
else
|
67
|
+
return '$' + (integer * 0.01).to_s
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
# Returns the integer time value (in seconds) formatted to H:MM:SS
|
72
|
+
#
|
73
|
+
# Parameter: integer - the integer being formatted (representing the time in second)
|
74
|
+
#
|
75
|
+
# Return: the formatted H:MM:SS
|
76
|
+
def self.hourminsec integer
|
77
|
+
return format_time "H:MM:SS", integer
|
78
|
+
end
|
79
|
+
|
80
|
+
private
|
81
|
+
|
82
|
+
def self.format_time formatter, value
|
83
|
+
return formatter
|
84
|
+
.sub(/II/, (value / 3600 % 12 + 1).to_s.rjust(2, '0'))
|
85
|
+
.sub(/HH/, (value / 3600).to_s.rjust(2, '0'))
|
86
|
+
.sub(/MM/, (value % 3600 / 60).to_s.rjust(2, '0'))
|
87
|
+
.sub(/SS/, (value % 3600 % 60).to_s.rjust(2, '0'))
|
88
|
+
.sub(/I/, (value / 3600 % 12 + 1).to_s)
|
89
|
+
.sub(/H/, (value / 3600).to_s)
|
90
|
+
.sub(/M/, (value % 3600 / 60).to_s)
|
91
|
+
.sub(/S/, (value % 3600 % 60).to_s)
|
92
|
+
.sub(/A/, value / 3600 > 12 ? 'PM' : 'AM')
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
data/lib/gpartials.rb
ADDED
@@ -0,0 +1,127 @@
|
|
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 part 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)
|
33
|
+
@string = string
|
34
|
+
end
|
35
|
+
|
36
|
+
# Returns the string
|
37
|
+
#
|
38
|
+
# Returns: the string
|
39
|
+
def apply data
|
40
|
+
@string
|
41
|
+
end
|
42
|
+
|
43
|
+
# Returns the string
|
44
|
+
#
|
45
|
+
# Returns: the string
|
46
|
+
def to_s
|
47
|
+
@string
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
# Represents a placeholder part in a GTemplate
|
52
|
+
#
|
53
|
+
# Author: Anshul Kharbanda
|
54
|
+
# Created: 7 - 1 - 2016
|
55
|
+
class GPlaceholder
|
56
|
+
# Regular expression that matches placeholders
|
57
|
+
REGEX = /@\((?<name>[a-zA-Z]\w*)\s*(\:\s*(?<default>.*?))?\s*(->\s*(?<operation>[a-zA-Z]\w*))?\)/
|
58
|
+
|
59
|
+
# Read name
|
60
|
+
attr :name
|
61
|
+
|
62
|
+
# Initializes the GPlaceholder with the given match
|
63
|
+
#
|
64
|
+
# Parameter: match - the match data from the string being parsed
|
65
|
+
def initialize match
|
66
|
+
@name = match[:name].to_sym
|
67
|
+
@operation = match[:operation]
|
68
|
+
end
|
69
|
+
|
70
|
+
# Returns the value of the placeholder in the given data
|
71
|
+
# with the given operation performed on it
|
72
|
+
#
|
73
|
+
# Parameter: data - the data being applied
|
74
|
+
#
|
75
|
+
# Return: the value of the placeholder in the given data
|
76
|
+
# with the given operation performed on it
|
77
|
+
def apply data
|
78
|
+
if @operation
|
79
|
+
return General::GOperations.send(@operation, data[@name]).to_s
|
80
|
+
else
|
81
|
+
return data[@name].to_s
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
# Returns the string representation of the placeholder
|
86
|
+
#
|
87
|
+
# Return: the string representation of the placeholder
|
88
|
+
def to_s
|
89
|
+
return "@(#{@name} -> #{@operation})"
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
# Represents an array placeholder part in a GTemplate
|
94
|
+
#
|
95
|
+
# Author: Anshul Kharbanda
|
96
|
+
# Created: 7 - 1 - 2016
|
97
|
+
class GArrayPlaceholder
|
98
|
+
# Regular expression that matches array placeholders
|
99
|
+
REGEX = /@\[(?<name>[a-zA-Z]\w*)\]( +|\n+)?(?<text>.*?)( +|\n+)?@\[(?<delimeter>.+)?\]/m
|
100
|
+
|
101
|
+
# Default delimeter
|
102
|
+
DEFAULT_DELIMETER = " "
|
103
|
+
|
104
|
+
# Read name
|
105
|
+
attr :name
|
106
|
+
|
107
|
+
# Initializes the GPlaceholder with the given match
|
108
|
+
#
|
109
|
+
# Parameter: match - the match data from the string being parsed
|
110
|
+
def initialize match
|
111
|
+
@name = match[:name].to_sym
|
112
|
+
@delimeter = match[:delimeter] || DEFAULT_DELIMETER
|
113
|
+
@template = General::GTemplate.new match[:text]
|
114
|
+
end
|
115
|
+
|
116
|
+
# Returns the value of the array placeholder in the given data
|
117
|
+
# formatted by the given GTemplate and joined by the given delimeter
|
118
|
+
#
|
119
|
+
# Parameter: data - the data being applied
|
120
|
+
#
|
121
|
+
# Return: the value of the array placeholder in the given data
|
122
|
+
# formatted by the given GTemplate and joined by the given delimeter
|
123
|
+
def apply data
|
124
|
+
return @template.apply_all(data[@name]).join(@delimeter)
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
data/lib/gtemplate.rb
CHANGED
@@ -14,6 +14,9 @@
|
|
14
14
|
# You should have received a copy of the GNU General Public License
|
15
15
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
16
16
|
|
17
|
+
require_relative "goperations"
|
18
|
+
require_relative "gpartials"
|
19
|
+
|
17
20
|
# General is a templating system in ruby
|
18
21
|
#
|
19
22
|
# Author: Anshul Kharbanda
|
@@ -24,38 +27,13 @@ module General
|
|
24
27
|
# Author: Anshul Kharbanda
|
25
28
|
# Created: 3 - 4 - 2016
|
26
29
|
class GTemplate
|
27
|
-
# Regular expression that matches placeholders
|
28
|
-
PLACEHOLDER = /@\((?<name>[a-zA-Z]\w*)\s*(\:\s*(?<default>.*?))?\s*(->\s*(?<operation>[a-zA-Z]\w*))?\)/
|
29
|
-
|
30
|
-
# Regular expression that matches array placeholders
|
31
|
-
ARRAY_PLACEHOLDER = /@\[(?<name>[a-zA-Z]\w*)\]( |\n)?(?<text>.*?)( |\n)?@\[(?<delimeter>.+)?\]/m
|
32
|
-
|
33
|
-
# Operations that can be called on placeholder values
|
34
|
-
OPERATIONS = {
|
35
|
-
# String operations
|
36
|
-
default: lambda { |string| return string },
|
37
|
-
capitalize: lambda { |string| return string.split(" ")
|
38
|
-
.collect(&:capitalize)
|
39
|
-
.join(" ") },
|
40
|
-
uppercase: lambda { |string| return string.uppercase },
|
41
|
-
lowercase: lambda { |string| return string.lowercase },
|
42
|
-
|
43
|
-
# Integer operations
|
44
|
-
dollars: lambda { |integer| return "$" + (integer * 0.01).to_s },
|
45
|
-
hourminsec: lambda { |integer| return (integer / 3600).to_s \
|
46
|
-
+ ":" + (integer % 3600 / 60).to_s \
|
47
|
-
+ ":" + (integer % 3600 % 60).to_s }
|
48
|
-
}
|
49
|
-
|
50
30
|
# Creates a GTemplate with the given template string
|
51
31
|
#
|
52
32
|
# Parameter: string - the string being converted to a template
|
53
33
|
def initialize string
|
54
|
-
|
55
|
-
@
|
56
|
-
@defaults
|
57
|
-
@operation = {}
|
58
|
-
@array = Hash.new(false)
|
34
|
+
# The string gets split into parts by placeholder and array template
|
35
|
+
@parts = []
|
36
|
+
@defaults = {}
|
59
37
|
|
60
38
|
parse_string string
|
61
39
|
end
|
@@ -63,7 +41,7 @@ module General
|
|
63
41
|
# Returns a string representation of the string
|
64
42
|
#
|
65
43
|
# Return: a string representation of the string
|
66
|
-
def to_s; @parts.join; end
|
44
|
+
def to_s; @parts.collect(&:to_s).join; end
|
67
45
|
|
68
46
|
# Applies the given data to the template and returns the generated string
|
69
47
|
#
|
@@ -71,22 +49,11 @@ module General
|
|
71
49
|
#
|
72
50
|
# Return: string of the template with the given data applied
|
73
51
|
def apply data={}
|
52
|
+
# Create applied data and parts
|
74
53
|
applied_data = @defaults.merge data.to_hash
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
@places[key].each do |place|
|
79
|
-
if @array[key]
|
80
|
-
applied_parts[place[:index]] = place[:template].apply_all(value).join(place[:delimeter])
|
81
|
-
else
|
82
|
-
applied_parts[place[:index]] = OPERATIONS[place[:operation]].call(value)
|
83
|
-
end
|
84
|
-
end
|
85
|
-
else
|
86
|
-
throw "Placeholder is not defined in template: @(#{key})"
|
87
|
-
end
|
88
|
-
end
|
89
|
-
return applied_parts.join
|
54
|
+
|
55
|
+
# Apply each part
|
56
|
+
return @parts.inject("") { |string, part| string += part.apply(applied_data) }
|
90
57
|
end
|
91
58
|
|
92
59
|
# Applies each data structure in the array independently to the template
|
@@ -97,11 +64,7 @@ module General
|
|
97
64
|
#
|
98
65
|
# Return: array of strings generated from the template with the given data applied
|
99
66
|
def apply_all data_array
|
100
|
-
|
101
|
-
data_array.each do |data|
|
102
|
-
string_array << apply(data)
|
103
|
-
end
|
104
|
-
return string_array
|
67
|
+
return data_array.collect { |data| apply(data) }
|
105
68
|
end
|
106
69
|
|
107
70
|
private
|
@@ -112,7 +75,9 @@ module General
|
|
112
75
|
#
|
113
76
|
# Return: true if given string has a placeholder
|
114
77
|
def has_placeholder string
|
115
|
-
|
78
|
+
placeholder = General::GPlaceholder::REGEX =~ string
|
79
|
+
array_placeholder = General::GArrayPlaceholder::REGEX =~ string
|
80
|
+
return !placeholder.nil? && placeholder > -1 || !array_placeholder.nil? && array_placeholder > -1
|
116
81
|
end
|
117
82
|
|
118
83
|
# Parses the string into General template data
|
@@ -122,67 +87,43 @@ module General
|
|
122
87
|
# While there is still a placeholder in string
|
123
88
|
while has_placeholder string
|
124
89
|
# Find locations of next placeholder and next array placeholder
|
125
|
-
next_p =
|
126
|
-
next_a =
|
90
|
+
next_p = General::GPlaceholder::REGEX =~ string
|
91
|
+
next_a = General::GArrayPlaceholder::REGEX =~ string
|
127
92
|
|
128
93
|
# If found at least one
|
129
94
|
if !(next_a.nil? && next_p.nil?)
|
130
95
|
|
131
96
|
# If found only array placeholder (or if found array placeholder before placeholder)
|
132
97
|
# Process placeholder
|
133
|
-
if
|
134
|
-
# Split match and add
|
135
|
-
match =
|
136
|
-
@parts << string[0...match.begin(0)]
|
137
|
-
string = string[match.end(0)..-1]
|
138
|
-
|
139
|
-
# Get name
|
140
|
-
name = match[:name].to_sym
|
141
|
-
|
142
|
-
# Get delimeter (if any) and parse array template
|
143
|
-
delimeter = match[:delimeter].nil? ? " " : match[:delimeter]
|
144
|
-
template = GTemplate.new(match[:text])
|
98
|
+
if !(next_a.nil? || next_p.nil?) && next_a < next_p || next_p.nil?
|
99
|
+
# Split match and add part
|
100
|
+
match = General::GArrayPlaceholder::REGEX.match string
|
101
|
+
@parts << General::GPartialString.new(string[0...match.begin(0)])
|
145
102
|
|
146
|
-
# Push
|
147
|
-
|
148
|
-
@array[name] = true
|
103
|
+
# Push array template
|
104
|
+
@parts << General::GArrayPlaceholder.new(match)
|
149
105
|
|
150
|
-
# If found only
|
106
|
+
# If found only placeholder (or if found placeholder before array placeholder)
|
151
107
|
# Process placeholder
|
152
|
-
elsif
|
153
|
-
# Split match and add
|
154
|
-
match =
|
155
|
-
@parts << string[0...match.begin(0)]
|
156
|
-
string = string[match.end(0)..-1]
|
157
|
-
|
158
|
-
# Get name
|
159
|
-
name = match[:name].to_sym
|
108
|
+
elsif !(next_a.nil? || next_p.nil?) && next_p < next_a || next_a.nil?
|
109
|
+
# Split match and add previous part
|
110
|
+
match = General::GPlaceholder::REGEX.match string
|
111
|
+
@parts << General::GPartialString.new(string[0...match.begin(0)])
|
160
112
|
|
161
|
-
#
|
162
|
-
|
113
|
+
# Add placeholder
|
114
|
+
@parts << General::GPlaceholder.new(match)
|
163
115
|
|
164
|
-
# Push
|
165
|
-
|
166
|
-
@defaults[name] = match[:default] unless @defaults.has_key? name
|
167
|
-
|
116
|
+
# Push default information
|
117
|
+
@defaults[match[:name].to_sym] ||= match[:default]
|
168
118
|
end
|
119
|
+
|
120
|
+
# Trim string
|
121
|
+
string = string[match.end(0)..-1]
|
169
122
|
end
|
170
123
|
end
|
171
124
|
|
172
125
|
# Add end of string
|
173
|
-
@parts << string
|
174
|
-
end
|
175
|
-
|
176
|
-
# Adds the given place for the placeholder of the given name
|
177
|
-
#
|
178
|
-
# Parameter: name - the name of the placeholder add a place to
|
179
|
-
# Parameter: place - the place information to add
|
180
|
-
def push_place name, place
|
181
|
-
if @places.has_key? name
|
182
|
-
@places[name] << place
|
183
|
-
else
|
184
|
-
@places[name] = [place]
|
185
|
-
end
|
126
|
+
@parts << General::GPartialString.new(string)
|
186
127
|
end
|
187
128
|
end
|
188
129
|
end
|
data/spec/gfile_spec.rb
CHANGED
@@ -2,10 +2,10 @@ require "spec_require"
|
|
2
2
|
|
3
3
|
describe General::GFile do
|
4
4
|
before :all do
|
5
|
-
@filepath = "
|
5
|
+
@filepath = "exp"
|
6
6
|
@filename = "sample.txt"
|
7
7
|
@new_filename = "supersample.txt"
|
8
|
-
@new_filepath = "
|
8
|
+
@new_filepath = "exp/superexp"
|
9
9
|
@data = {name: "joe", food: "Joe's Schmoes"}
|
10
10
|
@default_text = "There once was a chef name Gordon Ramsay, and he loved eating carrots."
|
11
11
|
@applied_text = "There once was a chef name Joe, and he loved eating Joe's Schmoes."
|
@@ -18,7 +18,6 @@ describe General::GFile do
|
|
18
18
|
describe "#new" do
|
19
19
|
it "Creates a new GFile with the given filename" do
|
20
20
|
expect(@file).to be_an_instance_of General::GFile
|
21
|
-
expect(@file.target).to eql (@filepath + "/" + @filename)
|
22
21
|
end
|
23
22
|
end
|
24
23
|
|
data/spec/gtemplate_spec.rb
CHANGED
@@ -2,6 +2,7 @@ require "spec_require"
|
|
2
2
|
|
3
3
|
describe General::GTemplate do
|
4
4
|
before :all do
|
5
|
+
# General template test
|
5
6
|
@template1 = General::GTemplate.new "There once was a man named @(name: Gordon Ramsay). @(name) loved @(food: Cat Food)!"
|
6
7
|
@default_text = "There once was a man named Gordon Ramsay. Gordon Ramsay loved Cat Food!"
|
7
8
|
@all_applied_text = "There once was a man named Joe. Joe loved Joe's Shmoes!"
|
@@ -11,17 +12,34 @@ describe General::GTemplate do
|
|
11
12
|
@name = "Dog"
|
12
13
|
@food = "Denny's Fennies"
|
13
14
|
|
15
|
+
# General array template test
|
14
16
|
@template2 = General::GTemplate.new "@[greetings] Hello, @(name)! How is the @(pet)? @[\n]"
|
15
17
|
@data2 = {greetings: [
|
16
|
-
{name: "Joe", pet: "cat"},
|
17
18
|
{name: "Ben", pet: "dog"},
|
19
|
+
{name: "Jen", pet: "cat"},
|
18
20
|
{name: "Ken", pet: "plant"}
|
19
21
|
]}
|
20
|
-
@applied_text2 = "Hello,
|
22
|
+
@applied_text2 = "Hello, Ben! How is the dog?\nHello, Jen! How is the cat?\nHello, Ken! How is the plant?"
|
21
23
|
|
22
|
-
@template3 = General::GTemplate.new "
|
23
|
-
@data3 = {
|
24
|
-
|
24
|
+
@template3 = General::GTemplate.new "@(film: The Dark Knight)\nCrew:\n@[crew] \t@(name): @(role) @[\n]\nScore: @(score)/10"
|
25
|
+
@data3 = {
|
26
|
+
film: 'Batman Begins',
|
27
|
+
crew: [
|
28
|
+
{name: 'David S Goyer', role: 'Writer'},
|
29
|
+
{name: 'Chris Nolan', role: 'Director'},
|
30
|
+
{name: 'Wally Pfister', role: 'Director of Photography'},
|
31
|
+
{name: 'Michael Caine', role: 'Alfred Pennyworth'},
|
32
|
+
{name: 'Christian Bale', role: 'Bruce Wayne/Batman'}
|
33
|
+
],
|
34
|
+
score: 10
|
35
|
+
}
|
36
|
+
@applied_text3 = "Batman Begins\nCrew:\n\tDavid S Goyer: Writer\n\tChris Nolan: Director\n\tWally Pfister: Director of Photography" \
|
37
|
+
"\n\tMichael Caine: Alfred Pennyworth\n\tChristian Bale: Bruce Wayne/Batman\nScore: 10/10"
|
38
|
+
|
39
|
+
# General operations test
|
40
|
+
@template4 = General::GTemplate.new "There once was a dog named @(name: dog -> capitalize). @(name -> capitalize) earned @(amount -> dollars) last week."
|
41
|
+
@data4 = {name: "cat", amount: 19999}
|
42
|
+
@applied_text4 = "There once was a dog named Cat. Cat earned $199.99 last week."
|
25
43
|
end
|
26
44
|
|
27
45
|
describe "#new" do
|
@@ -29,6 +47,7 @@ describe General::GTemplate do
|
|
29
47
|
expect(@template1).to be_an_instance_of General::GTemplate
|
30
48
|
expect(@template2).to be_an_instance_of General::GTemplate
|
31
49
|
expect(@template3).to be_an_instance_of General::GTemplate
|
50
|
+
expect(@template4).to be_an_instance_of General::GTemplate
|
32
51
|
end
|
33
52
|
end
|
34
53
|
|
@@ -63,9 +82,15 @@ describe General::GTemplate do
|
|
63
82
|
end
|
64
83
|
end
|
65
84
|
|
85
|
+
context "With array template and regular placeholder" do
|
86
|
+
it "Returns the template with the given data applied (including array data) and formatted according to the template" do
|
87
|
+
expect(@template3.apply(@data3)).to eql @applied_text3
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
66
91
|
context "With placeholder operation" do
|
67
92
|
it "Returns the template with the given array data applied and formatted according to the format operations" do
|
68
|
-
expect(@
|
93
|
+
expect(@template4.apply(@data4)).to eql @applied_text4
|
69
94
|
end
|
70
95
|
end
|
71
96
|
end
|
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.2.
|
4
|
+
version: 1.2.7
|
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-07-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|
@@ -24,10 +24,10 @@ dependencies:
|
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '3.4'
|
27
|
-
description:
|
28
|
-
templates from
|
29
|
-
as
|
30
|
-
|
27
|
+
description: General is a simple templating system in ruby that allows you to create
|
28
|
+
templates from bothpure strings and files (with the extension .general), as well
|
29
|
+
as create new strings and fileswith these created objects. For more information,
|
30
|
+
visit the homepage
|
31
31
|
email: akanshul97@gmail.com
|
32
32
|
executables: []
|
33
33
|
extensions: []
|
@@ -35,6 +35,8 @@ extra_rdoc_files: []
|
|
35
35
|
files:
|
36
36
|
- lib/general.rb
|
37
37
|
- lib/gfile.rb
|
38
|
+
- lib/goperations.rb
|
39
|
+
- lib/gpartials.rb
|
38
40
|
- lib/gtemplate.rb
|
39
41
|
- spec/gfile_spec.rb
|
40
42
|
- spec/gtemplate_spec.rb
|