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
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ae2513790cf50098833adabdd311b9bbecc9409d
4
- data.tar.gz: a120cf500bc65f1853c240749ecf2f5a048b144c
3
+ metadata.gz: 8cd60835b374d4c4eae5d1a8b0c38eaea4032189
4
+ data.tar.gz: a37a61c98244f3927208539dcf5da0994ceabc35
5
5
  SHA512:
6
- metadata.gz: 651d42923d42e35fdb6e8ddf4fae1080f668c24bbffd85b6abd70f4bd9f6f7e91862437266625d447c9edebb6ca93b80091eacc910db66ed13e5993468c828e1
7
- data.tar.gz: a283344e7695397591087313ab9f29f54c92bd50653333e49a1467e5744ef8384bda3076aca2ab04ba714e17550dd7e246085d497a5530b110035e167d7fe48d
6
+ metadata.gz: ef5ce9c1ed0d2f1e02418b46e54c2ddea1af940ca7d461f846b423aa19eaa064c2dbb16393c1982caf7293cffdeee635562678472cdef14f38b67bf18ac02bd4
7
+ data.tar.gz: 1850598692f1c631661f912fc86e30a300d1883c23fc575474a817b2ca6e9905f96c43314959c1961d2709d5a3f6690761e2dfecc935e9e5e5a411055234a00c
data/README.md CHANGED
@@ -1,13 +1,28 @@
1
1
  # General
2
2
 
3
- ## Overview
3
+ Hello, I am @(name: gordon ramsay -> capitalize all) and I like @(food: cat food)!
4
4
 
5
5
  General is a simple templating system in ruby that allows you to create templates from both pure strings and files (with the extension .general), as well as create new strings and files with these created objects.
6
6
 
7
+ ## Table of Contents
8
+
9
+ - [General Templates](#general-templates)
10
+ - [Basic Templates](#basic-templates)
11
+ - [Array Templates](#array-templates)
12
+ - [Placeholder Operations](#placeholder-operations)
13
+ - [To-Array Operations](#to-array-operations)
14
+ - [General Time Format](#general-time-format)
15
+ - [General IO](#general-io)
16
+ - [PrePartials](#prepartials)
17
+ - [Including Other Templates](#including-other-templates)
18
+ - [Extending Templates](#extending-templates)
19
+
7
20
  ## General Templates
8
21
 
9
22
  ### Basic Templates
10
23
 
24
+ "Hello, I am @(name: Gordon Ramsay) and I like @(food: cat food)!"
25
+
11
26
  A general template consists of regular text along with placeholders, which are defined as follows: `@(-name-)`. You can also specify default text as well like so: `@(-name-: -default text-)`.
12
27
 
13
28
  Here's an exmple of a general template text: `"Hello, I am @(name: Gordon Ramsay) and I like @(food: cat food)!"`
@@ -45,23 +60,13 @@ array = template.apply_all [{name: "Joe", food: "Joe Schmoes"}, {name: "Jane", f
45
60
  # - Hello, I am Denny and I like Denny's Fennies
46
61
  ```
47
62
 
48
- ### Placeholder Operations
49
-
50
- You can also specify operations to be performed on values passed to placeholders, akin to AngularJS's filters. For example: `@(name -> capitalize)` will capitalize whatever name is inputted before applying it to the text.
51
-
52
- The current placeholder operations are:
53
-
54
- | Operation | Description |
55
- |:-----------|:--------------------------------------------------------|
56
- | capitalize | Capitalizes the first letter of each word in a string |
57
- | uppercase | Makes every letter in a string uppercase |
58
- | lowercase | Makes every letter in a string lowercase |
59
- | dollars | Formats an integer money amount (in cents) to dollars |
60
- | time | Formats an integer time (in seconds) to HH:MM:SS format |
61
-
62
63
  ### Array Templates
63
64
 
64
- You can also make array templates within templates, which will format each value in an array of data according to a general template
65
+ @[people]
66
+ Hello, @(name)! How's the @(pet)?
67
+ @[\n]
68
+
69
+ Array templates will format each value in an array of data according to a general template
65
70
 
66
71
  A general array template is as follows: `@[-name-] -general template for each value- @[]`. You can also specify the delimeter, which will be appended to the end of each element. The delimeter should be added in the end tag, like so: `@[-name-] -general template for each value- @[-delimeter-]`. If no delimeter is given, the default is a space. The start and end tags are invariant with atleast 1 whitespace or newline. So you can also define the template like such:
67
72
 
@@ -99,18 +104,82 @@ string = template.apply crew: [
99
104
  # Michael Caine: Alfred Pennyworth"
100
105
  ```
101
106
 
107
+ ### Placeholder Operations
108
+
109
+ @(name -> capitalize all)
110
+
111
+ You can also specify operations to be performed on values passed to placeholders, akin to AngularJS's filters. For example: `@(name -> capitalize)` will capitalize whatever name is inputted before applying it to the text. Placeholder operations also have arguments, which change behaviour. `@(name -> capitalize first)` will capitalize the first word of name, whereas `@(name -> capitalize all)` will capitalize all words in the name.
112
+
113
+ - capitalize: capitalizes the string
114
+ - Argument 1:
115
+ - 'first': only first word is capitalized
116
+ - 'all': all words are capitalized
117
+ - default: 'first'
118
+ - uppercase: converts a string to uppercase
119
+ - lowercase: converts a string to lowercase
120
+ - money: converts an integer to a monetary amount in the given format
121
+ - Argument 1:
122
+ - 'USD': United States Dollar
123
+ - 'EUR': European Euro
124
+ - default: 'USD'
125
+ - time: converts an integer to a formatted time in the given time format
126
+ - Argument 1:
127
+ - Time format to set to
128
+ - default: '@I:@MM:@SS @A'
129
+
130
+ #### To-Array Operations
131
+
132
+ @[groceries -> split]
133
+ I need to get some @#!
134
+ @[\n].
135
+
136
+ Certain operations can convert data values to arrays to be used by an array template. These can be applied to array placeholders as follows `@[text -> split] -general- @[].` These return pure data values, and they can be accessed by a special placeholder `@#` which applies the entire data value as is.
137
+
138
+ ```ruby
139
+ template = General::GTemplate.new "@[text -> split] -> @# @[\n]"
140
+ string = template.apply text: "Good Afternoon World
141
+ I am Chef Gordon Ramsay"
142
+
143
+ # string now equals
144
+ # "-> Good Afternoon World
145
+ # -> I am Chef Gordon Ramsay"
146
+ ```
147
+
148
+ - split: splits a string based on a given delimeter
149
+ - Argument 1:
150
+ - The delimeter regex to split the string by
151
+ - default: \r?\n
152
+ - splitwords: splits a string at a given number of words
153
+ - Argument 1:
154
+ - The words at which to split the string
155
+ - default: 10
156
+
157
+ ## General Time Format
158
+
159
+ General offers a separate special template called a GTimeFormat used for formatting times. The GTimeFormat syntax is also used by the time operation.
160
+
161
+ Time format placeholders are written as an @ followed by one or more characters of the same letter: `@LL...` (except for `@A`) The letter indicates the part of time information to format and the number of letters indicates the number of zeros.
162
+
163
+ | Letter | Description |
164
+ |:-------|:------------------------|
165
+ | @H... | Hour (0 to 23) |
166
+ | @I... | Time interval (1 to 12) |
167
+ | @M... | Minutes (0 to 59) |
168
+ | @S... | Seconds (0 to 59) |
169
+ | @A | AM/PM |
170
+
102
171
  ## General IO
103
172
 
104
- You can also write to files using io GIO, a general template capable of writing to files. You can create a GIO like a GTemplate:
173
+ You can also write to files using GIO, a general template capable of writing to files. You can create a GIO like a GTemplate:
105
174
 
106
175
  ```ruby
107
176
  gio = General::GIO.new "Hello, I am @(name: Gordon Ramsay) and I like @(food: cat food)!"
108
177
  ```
109
178
 
110
- You can also load a GIO from a file. For example, here's how you create a template file from the file "example.txt.general"
179
+ You can also load a GIO from a file. For example, here's how you create a template file from the file "example.general"
111
180
 
112
181
  ```ruby
113
- gio = General::GFile.load "example.txt.general"
182
+ gio = General::GFile.load "example.general"
114
183
  ```
115
184
 
116
185
  To write to a file, simply call the write method, pass in the file and the data to apply (like in GTemplate#apply):
@@ -128,8 +197,85 @@ gio.write "example.txt", name: "Joe", food: "Joe's Schmoes"
128
197
  To get the original source filename of the GIO, just call `source`
129
198
 
130
199
  ```ruby
131
- gio.source # == "example.txt.general"
200
+ gio.source # == "example.general"
132
201
  ```
133
202
 
203
+ ### PrePartials
204
+
205
+ @@extend basic_layout
206
+ @@include header
207
+
208
+ <p>I am a subtemplate! My name is @(name -> capitalize all)</p>
209
+
210
+ PrePartials are parsed before the general template is parsed, and perform file template manipulation like including or extending another template.
211
+
212
+ #### Including Other Templates
213
+
214
+ File to include
215
+
216
+ @(title)
217
+ @(author)
218
+ @(subject)
219
+ @(date)
220
+
221
+ Including file
222
+
223
+ @@include file
224
+
225
+ @(content)
226
+
227
+ Resulting File
228
+
229
+ @(title)
230
+ @(author)
231
+ @(subject)
232
+ @(date)
233
+
234
+ @(content)
235
+
236
+ To include another template file in your file, you use `@@include [name of template]` (excluding the suffix). This will read the corresponding template file and append the content in place of the `@@include`.
237
+
238
+ NOTE: `@@include` requires a new line or carriage return to be parsed (it takes up one entire line). It also appends a newline at the end of the included template.
239
+
240
+ #### Extending Templates
241
+
242
+ File to Extend
243
+
244
+ <!doctype html>
245
+ <html>
246
+ <title>@(title -> capitalize all)</title>
247
+ <body>
248
+ @@yield
249
+ </body>
250
+ </html>
251
+
252
+ Extending File
253
+
254
+ @@extend file
255
+
256
+ <h1>@(name)'s Profile!</h1>
257
+ <p>Favorite Color: @(color)</p>
258
+ <p>Favorite Sport: @(sport)</p>
259
+
260
+ Resulting File
261
+
262
+ <!doctype html>
263
+ <html>
264
+ <title>@(title -> capitalize all)</title>
265
+ <body>
266
+ <h1>@(name)'s Profile!</h1>
267
+ <p>Favorite Color: @(color)</p>
268
+ <p>Favorite Sport: @(sport)</p>
269
+ </body>
270
+ </html>
271
+
272
+ Extending a file wraps the contents of the file around the current template. This is good when many templates will share a single common layout. Extend prepartials are as follows `@@extend [name of template]` (also excluding extension). The `@@extend` must be defined at the beginning of the template, and there can be only one `@@extend` in any template.
273
+
274
+ The extending file should define a `@@yield` where the file will be extended from. This is by default at the end of the file. Note that if `@@yield` is defined in a template, the template is considered a meta-template and cannot be parsed. Also, like extend, only one `@@yield` statement can be defined in a single file.
275
+
276
+ ## Notes
277
+
278
+ Regex and matching have now been officially depricated, and will be removed in General 2.1.0.
279
+
134
280
  -------------------------------------------------------------------------------------------------------------------------------------
135
281
  Anshul Kharbanda
data/Rakefile CHANGED
@@ -44,9 +44,18 @@ namespace :git do
44
44
  sh "git commit -m #{args[:message].inspect}"
45
45
  end
46
46
 
47
+ desc "Ammend recent commit"
48
+ task :ammend, [:messave] do |task, args|
49
+ sh "git commit --ammend"
50
+ end
51
+
47
52
  desc "Soft git reset"
48
- task :reset do sh "git reset" end
53
+ task :reset do
54
+ sh "git reset"
55
+ end
49
56
 
50
57
  desc "Hard git reset"
51
- task :reset_hard do sh "git reset --hard HEAD" end
58
+ task :hardreset do
59
+ sh "git reset --hard HEAD"
60
+ end
52
61
  end
@@ -0,0 +1 @@
1
+ There once was a chef named Joe, and he loved eating Joe's Schmoes. He went to Kentucky.
@@ -0,0 +1,3 @@
1
+ There once was a chef named Joe, and he loved eating Joe's Schmoes. He went to Kentucky.
2
+
3
+ Joe drove to the store to get some Joe's Schmoes.
@@ -0,0 +1,9 @@
1
+ General Super Layout
2
+
3
+ Open
4
+
5
+ There once was a chef named Joe, and he loved eating Joe's Schmoes. He went to Kentucky.
6
+
7
+ Joe went to china to eat some crickets.
8
+
9
+ Close
@@ -0,0 +1 @@
1
+ There once was a chef named Gordon Ramsay, and he loved eating carrots. He went to Virginia.
@@ -0,0 +1,3 @@
1
+ There once was a chef named Gordon Ramsay, and he loved eating carrots. He went to Virginia.
2
+
3
+ Gordon Ramsay drove to the store to get some carrots.
@@ -0,0 +1,9 @@
1
+ General Super Layout
2
+
3
+ Open
4
+
5
+ There once was a chef named Gordon Ramsay, and he loved eating carrots. He went to Virginia.
6
+
7
+ Gordon Ramsay went to china to eat some crickets.
8
+
9
+ Close
@@ -0,0 +1 @@
1
+ There once was a chef named Joe, and he loved eating Joe's Schmoes. He went to Kentucky.
@@ -0,0 +1,3 @@
1
+ There once was a chef named Joe, and he loved eating Joe's Schmoes. He went to Kentucky.
2
+
3
+ Joe drove to the store to get some Joe's Schmoes.
@@ -0,0 +1,9 @@
1
+ General Super Layout
2
+
3
+ Open
4
+
5
+ There once was a chef named Joe, and he loved eating Joe's Schmoes. He went to Kentucky.
6
+
7
+ Joe went to china to eat some crickets.
8
+
9
+ Close
@@ -0,0 +1,7 @@
1
+ General Super Layout
2
+
3
+ Open
4
+
5
+ @@yield
6
+
7
+ Close
@@ -0,0 +1 @@
1
+ There once was a chef named @(name: gordon ramsay -> capitalize all), and he loved eating @(food: carrots). He went to @(place -> capitalize).
@@ -0,0 +1,3 @@
1
+ @@include sample1
2
+
3
+ @(name -> capitalize all) drove to the store to get some @(food).
@@ -0,0 +1,4 @@
1
+ @@extend sample0
2
+ @@include sample1
3
+
4
+ @(name -> capitalize all) went to china to eat some @(exotic_food: crickets).
@@ -0,0 +1,7 @@
1
+ This should not work
2
+
3
+ @@yield
4
+
5
+ Because it has two yields!
6
+
7
+ @@yield
@@ -0,0 +1,3 @@
1
+ @@extend sampleE0
2
+
3
+ WOA! HOW DID THIS HAPPEN!
@@ -0,0 +1,3 @@
1
+ This one has the extend in the wrong place!
2
+
3
+ @@extend sample0
@@ -0,0 +1,7 @@
1
+ @@extend sample0
2
+
3
+ And this one has TWO Extends!
4
+
5
+ @@extend sampleE0
6
+
7
+ What is haaaapening????
data/lib/general.rb CHANGED
@@ -14,6 +14,6 @@
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 "template/gtemplate"
18
- require_relative "template/gtimeformat"
19
- require_relative "template/gio"
17
+ require_relative "gtemplates/gtemplate"
18
+ require_relative "gtemplates/gtimeformat"
19
+ require_relative "gtemplates/gio"
data/lib/gerror.rb ADDED
@@ -0,0 +1,33 @@
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
+ # Base class for errors in General
23
+ #
24
+ # Author: Anshul Kharbanda
25
+ # Created: 1 - 21 - 2017
26
+ class GError < StandardError; end
27
+
28
+ # A General Operation Error
29
+ #
30
+ # Author: Anshul Kharbanda
31
+ # Created: 1 - 21 - 2017
32
+ class GOperationError < GError; end
33
+ end
data/lib/goperations.rb CHANGED
@@ -14,7 +14,8 @@
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 "templates/gtimeformat"
17
+ require_relative "gtemplates/gtimeformat"
18
+ require_relative "gerror"
18
19
 
19
20
  # General is a templating system in ruby
20
21
  #
@@ -35,6 +36,25 @@ module General
35
36
  # The default time format
36
37
  DEFAULT_TIME = "@I:@MM:@SS @A"
37
38
 
39
+ private
40
+
41
+ # Asserts that the given argument for the given operation exists and is of the given types
42
+ #
43
+ # Parameter: operation - the name of the operation
44
+ # Parameter: argument - the argument being checked
45
+ # Parameter: types - the allowed types for the argument
46
+ #
47
+ # Raises: GOperationError if assertion fails
48
+ def self.assert operation, argument, *types
49
+ raise GOperationError.new "Requied argument not given for operation #{operation}" if argument.nil?
50
+ unless types.any? { |type| argument.is_a? type }
51
+ raise GOperationError.new "Argument #{argument} for operation #{operation} " \
52
+ "must be of type(s): #{types.collect(&:to_s).join(", ")}. Got #{argument.class.name}"
53
+ end
54
+ end
55
+
56
+ public
57
+
38
58
  #-----------------------------------STRING OPERATIONS------------------------------------
39
59
 
40
60
  # Capitalizes every word in the string
@@ -42,12 +62,13 @@ module General
42
62
  # Parameter: string - the string being capitalized
43
63
  #
44
64
  # Return: the capitalized string
45
- def self.capitalize string, what="first"
46
- assert_type string, String
65
+ def self.capitalize string=nil, what='first'
66
+ assert 'capitalize', string, String
67
+
47
68
  case what
48
- when "all" then string.split(' ').collect(&:capitalize).join(' ')
49
- when "first" then string.capitalize
50
- else raise TypeError.new "Undefined second argument for operation capitalize: #{what}"
69
+ when 'all' then string.split(' ').collect(&:capitalize).join(' ')
70
+ when 'first' then string.capitalize
71
+ else raise GOperationError.new "Undefined second argument for operation capitalize: #{what}"
51
72
  end
52
73
  end
53
74
 
@@ -56,9 +77,9 @@ module General
56
77
  # Parameter: string - the string being uppercased
57
78
  #
58
79
  # Return: the uppercased string
59
- def self.uppercase(string)
60
- assert_type string, String
61
- string.upcase
80
+ def self.uppercase string=nil
81
+ assert 'uppercase', string, String
82
+ return string.upcase
62
83
  end
63
84
 
64
85
  # Converts every letter in the string to lowercase
@@ -66,9 +87,9 @@ module General
66
87
  # Parameter: string - the string being lowercased
67
88
  #
68
89
  # Return: the lowercased string
69
- def self.lowercase(string)
70
- assert_type string, String
71
- string.downcase
90
+ def self.lowercase string=nil
91
+ assert 'lowercase', string, String
92
+ return string.downcase
72
93
  end
73
94
 
74
95
  #-----------------------------------INTEGER OPERATIONS------------------------------------
@@ -79,12 +100,13 @@ module General
79
100
  # Parameter: type - the type of money (defaults to USD)
80
101
  #
81
102
  # Return: the formatted money amount
82
- def self.money integer, type="USD"
83
- assert_type integer, Integer
103
+ def self.money integer=nil, type="USD"
104
+ assert 'money', integer, Integer
105
+
84
106
  if MONEY_TYPES[type]
85
- (integer < 0 ? "-" : "") + MONEY_TYPES[type] + (integer * 0.01).abs.to_s
107
+ return (integer < 0 ? "-" : "") + MONEY_TYPES[type] + (integer * 0.01).abs.to_s
86
108
  else
87
- raise TypeError.new("Money type: #{type} is not supported!")
109
+ raise GOperationError.new "Money type: #{type} is not supported!"
88
110
  end
89
111
  end
90
112
 
@@ -94,23 +116,69 @@ module General
94
116
  # Parameter: format - the format being used (defaults to DEFAULT_TIME)
95
117
  #
96
118
  # Return: the time formatted with the given formatter
97
- def self.time integer, format=DEFAULT_TIME
98
- assert_type integer, Integer
99
- General::GTimeFormat.new(format).apply(integer)
119
+ def self.time integer=nil, format=DEFAULT_TIME
120
+ assert 'time', integer, Integer
121
+ return General::GTimeFormat.new(format).apply(integer)
100
122
  end
101
123
 
102
- private
124
+ #---------------------------------TO ARRAY OPERATIONS-----------------------------------
125
+
126
+ # Splits the string by the given delimeter (or by newline if no delimeter is given)
127
+ #
128
+ # Parameter: string - the string to split
129
+ # Parameter: delimeter - the delimeter to split by (defaults to newline)
130
+ #
131
+ # Return: the array containing hashes representing the split string chunks
132
+ def self.split string=nil, delimeter="\r?\n"
133
+ assert 'split', string, String
134
+ return string.split(Regexp.new(delimeter))
135
+ end
103
136
 
104
- # Raises TypeError if the given value is not one of the given types
137
+ # Splits a sequence by a set number of words (defaults to 10)
105
138
  #
106
- # Parameter: value - the value to check
107
- # Parameter: types - the types to check for
139
+ # Parameter: string - the string to split
140
+ # Parameter: words - the number of words to split by (defaults to 10)
108
141
  #
109
- # Raises: TypeError - if the given value is not one of the given types
110
- def self.assert_type value, *types
111
- unless types.any? {|type| value.is_a? type}
112
- raise TypeError.new "Unexpected value type #{value.class.name}. Expected #{types.collect(&:name).join(",")}"
142
+ # Return: an array containing hashes representing the chunks of the string split by words
143
+ def self.splitwords string=nil, words=10
144
+ assert 'splitwords', string, String
145
+
146
+ # Convert words to integer
147
+ words = words.to_i
148
+
149
+ # Regex to match words
150
+ matcher = /\G[\w\',\.\?!\(\)\-\:\;\"\"]+\s*/
151
+
152
+ # Buffers
153
+ to_return = []
154
+ buffer = ""
155
+
156
+ # Initialize loop
157
+ matched = matcher.match string
158
+ index = 0
159
+
160
+ # While matched data exists
161
+ while matched
162
+ # Push line to array and reset if number of words is passed
163
+ if index % words == 0
164
+ to_return << buffer.sub(/\s+$/, "")
165
+ buffer = ""
166
+ end
167
+
168
+ # Append word to buffer
169
+ buffer += matched.to_s
170
+
171
+ # Trim word from string
172
+ string = string[matched.end(0)..-1]
173
+
174
+ # Iterate
175
+ matched = matcher.match string
176
+ index += 1
113
177
  end
178
+
179
+ # Push final line to array and return
180
+ to_return << buffer
181
+ return to_return[1..-1] # Getting rid of the first blank line
114
182
  end
115
183
  end
116
184
  end