general 1.2.2 → 1.2.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/gtemplate.rb +39 -14
- data/spec/gfile_spec.rb +3 -3
- data/spec/gtemplate_spec.rb +7 -7
- metadata +16 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 477f5e34b1b64b4e0e4741a246208dfdf64ed532
|
4
|
+
data.tar.gz: 100cee4dc93bbf2e150dcddfd066bcf4043c2cc6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e92fc67acd37263609750d28aa79f9d92ad310c167132e1d7d68cd878846919ba3ade7d1736af8bea06fab1df145e38e21d4a62c9014d3646b49ca075367fda5
|
7
|
+
data.tar.gz: e4df59bce6f7190975c9f7ff31737e67979406380e74bad6fbe26bb3a628b39afbffeea886b785c1e18a574731f5af6958f69eb615c3648dac3f932e42f485cf
|
data/lib/gtemplate.rb
CHANGED
@@ -65,28 +65,45 @@ module General
|
|
65
65
|
# Return: a string representation of the string
|
66
66
|
def to_s; @parts.join; end
|
67
67
|
|
68
|
-
#
|
68
|
+
# Applies the given data to the template and returns the generated string
|
69
69
|
#
|
70
|
-
# Parameter: data - the data to be applied (merges with defaults)
|
70
|
+
# Parameter: data - the data to be applied (as a hash. merges with defaults)
|
71
71
|
#
|
72
|
-
# Return: the template with the given data applied
|
72
|
+
# Return: string of the template with the given data applied
|
73
73
|
def apply data={}
|
74
74
|
applied_data = @defaults.merge data.to_hash
|
75
75
|
applied_parts = @parts.clone
|
76
76
|
applied_data.each do |key, value|
|
77
|
-
if @
|
77
|
+
if @places.has_key? key
|
78
78
|
@places[key].each do |place|
|
79
|
-
|
79
|
+
if @array[key]
|
80
|
+
applied_parts[place[:index]] = place[:template].apply_all(value).join(place[:delimeter])
|
81
|
+
else
|
82
|
+
applied_parts[place[:index]] = place[:operation].call(value)
|
83
|
+
end
|
80
84
|
end
|
81
85
|
else
|
82
|
-
@
|
83
|
-
applied_parts[place[:index]] = place[:operation].call(value)
|
84
|
-
end
|
86
|
+
throw "Placeholder is not defined in template: @(#{key})"
|
85
87
|
end
|
86
88
|
end
|
87
89
|
return applied_parts.join
|
88
90
|
end
|
89
91
|
|
92
|
+
# Applies each data structure in the array independently to the template
|
93
|
+
# and returns an array of the generated strings
|
94
|
+
#
|
95
|
+
# Parameter: data_array - the array of data to be applied
|
96
|
+
# (each data hash will be merged with defaults)
|
97
|
+
#
|
98
|
+
# Return: array of strings generated from the template with the given data applied
|
99
|
+
def apply_all data_array
|
100
|
+
string_array = []
|
101
|
+
data_array.each do |data|
|
102
|
+
string_array << apply(data)
|
103
|
+
end
|
104
|
+
return string_array
|
105
|
+
end
|
106
|
+
|
90
107
|
private
|
91
108
|
|
92
109
|
# Returns true if given string has a placeholder
|
@@ -102,15 +119,17 @@ module General
|
|
102
119
|
#
|
103
120
|
# Parameter: string - the string to parse
|
104
121
|
def parse_string string
|
105
|
-
# While
|
122
|
+
# While there is still a placeholder in string
|
106
123
|
while has_placeholder string
|
107
124
|
if ARRAY_PLACEHOLDER =~ string
|
108
125
|
# Split match and add parts
|
109
126
|
match = ARRAY_PLACEHOLDER.match string
|
110
|
-
|
111
|
-
@parts << string[0...match.begin(0)] << name
|
127
|
+
@parts << string[0...match.begin(0)] << match
|
112
128
|
string = string[match.end(0)..-1]
|
113
129
|
|
130
|
+
# Get name
|
131
|
+
name = match[:name].to_sym
|
132
|
+
|
114
133
|
# Get delimeter (if any) and parse array template
|
115
134
|
delimeter = match[:delimeter].nil? ? " " : match[:delimeter]
|
116
135
|
template = GTemplate.new(match[:text])
|
@@ -121,10 +140,13 @@ module General
|
|
121
140
|
elsif PLACEHOLDER =~ string
|
122
141
|
# Split match and add parts
|
123
142
|
match = PLACEHOLDER.match string
|
124
|
-
|
125
|
-
@parts << string[0...match.begin(0)] << name
|
143
|
+
@parts << string[0...match.begin(0)] << match
|
126
144
|
string = string[match.end(0)..-1]
|
127
145
|
|
146
|
+
# Get name
|
147
|
+
name = match[:name].to_sym
|
148
|
+
|
149
|
+
# Get operation and arguments (if any)
|
128
150
|
operation = match[:operation].nil? ? OPERATIONS[:default] : OPERATIONS[match[:operation].to_sym]
|
129
151
|
|
130
152
|
# Push place and default information
|
@@ -149,4 +171,7 @@ module General
|
|
149
171
|
end
|
150
172
|
end
|
151
173
|
end
|
152
|
-
end
|
174
|
+
end
|
175
|
+
|
176
|
+
tem = General::GTemplate.new "There once was a dog named @(name: dog -> capitalize). @(name -> capitalize) earned @(amount -> dollars) last week."
|
177
|
+
puts tem
|
data/spec/gfile_spec.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
require "spec_require"
|
2
2
|
|
3
|
-
describe
|
3
|
+
describe General::GFile do
|
4
4
|
before :all do
|
5
5
|
@filepath = "tmp"
|
6
6
|
@filename = "sample.txt"
|
@@ -12,12 +12,12 @@ describe Generic::GFile do
|
|
12
12
|
end
|
13
13
|
|
14
14
|
before :each do
|
15
|
-
@file =
|
15
|
+
@file = General::GFile.new (@filepath + "/" + @filename + General::GFile::EXTENTION)
|
16
16
|
end
|
17
17
|
|
18
18
|
describe "#new" do
|
19
19
|
it "Creates a new GFile with the given filename" do
|
20
|
-
expect(@file).to be_an_instance_of
|
20
|
+
expect(@file).to be_an_instance_of General::GFile
|
21
21
|
expect(@file.target).to eql (@filepath + "/" + @filename)
|
22
22
|
end
|
23
23
|
end
|
data/spec/gtemplate_spec.rb
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
require "spec_require"
|
2
2
|
|
3
|
-
describe
|
3
|
+
describe General::GTemplate do
|
4
4
|
before :all do
|
5
|
-
@template1 =
|
5
|
+
@template1 = General::GTemplate.new "There once was a man named @(name: Gordon Ramsay). @(name) loved @(food: Cat Food)!"
|
6
6
|
@default_text = "There once was a man named Gordon Ramsay. Gordon Ramsay loved Cat Food!"
|
7
7
|
@all_applied_text = "There once was a man named Joe. Joe loved Joe's Shmoes!"
|
8
8
|
@name_applied_text = "There once was a man named Dog. Dog loved Cat Food!"
|
@@ -11,7 +11,7 @@ describe Generic::GTemplate do
|
|
11
11
|
@name = "Dog"
|
12
12
|
@food = "Denny's Fennies"
|
13
13
|
|
14
|
-
@template2 =
|
14
|
+
@template2 = General::GTemplate.new "@[greetings] Hello, @(name)! How is the @(pet)? @[\n]"
|
15
15
|
@data2 = {greetings: [
|
16
16
|
{name: "Joe", pet: "cat"},
|
17
17
|
{name: "Ben", pet: "dog"},
|
@@ -19,16 +19,16 @@ describe Generic::GTemplate do
|
|
19
19
|
]}
|
20
20
|
@applied_text2 = "Hello, Joe! How is the cat?\nHello, Ben! How is the dog?\nHello, Ken! How is the plant?"
|
21
21
|
|
22
|
-
@template3 =
|
22
|
+
@template3 = General::GTemplate.new "There once was a dog named @(name: dog -> capitalize). @(name -> capitalize) earned @(amount -> dollars) last week."
|
23
23
|
@data3 = {name: "cat", amount: 19999}
|
24
24
|
@applied_text3 = "There once was a dog named Cat. Cat earned $199.99 last week."
|
25
25
|
end
|
26
26
|
|
27
27
|
describe "#new" do
|
28
28
|
it "Creates a new GTemplate with the given template string" do
|
29
|
-
expect(@template1).to be_an_instance_of
|
30
|
-
expect(@template2).to be_an_instance_of
|
31
|
-
expect(@template3).to be_an_instance_of
|
29
|
+
expect(@template1).to be_an_instance_of General::GTemplate
|
30
|
+
expect(@template2).to be_an_instance_of General::GTemplate
|
31
|
+
expect(@template3).to be_an_instance_of General::GTemplate
|
32
32
|
end
|
33
33
|
end
|
34
34
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
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.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Anshul Kharbanda
|
@@ -9,7 +9,21 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
date: 2016-05-26 00:00:00.000000000 Z
|
12
|
-
dependencies:
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rspec
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.4'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3.4'
|
13
27
|
description: "General is a simple templating system in ruby that allows you to create
|
14
28
|
templates from both \t\t\t\t\t\tpure strings and files (with the extension .general),
|
15
29
|
as well as create new strings and files \t\t\t\t\t\twith these created objects.
|