pad_utils 1.2.1 → 1.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +3 -278
- data/bin/padutils +1 -0
- data/lib/pad_utils/pad_files.rb +92 -18
- data/lib/pad_utils/pad_json.rb +43 -12
- data/lib/pad_utils/pad_logger.rb +38 -13
- data/lib/pad_utils/pad_menu.rb +28 -11
- data/lib/pad_utils/pad_text.rb +111 -27
- data/lib/pad_utils/pad_time.rb +28 -5
- data/lib/pad_utils/version.rb +2 -1
- data/lib/pad_utils.rb +5 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d62721ccb2c8956e21afbd46701f70c4ed8b16d0
|
4
|
+
data.tar.gz: 880ccaa96f429c75831085cfaa4c06993bb3beb0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 58c650731599b21858389934b0223cfd59780eca5e0206a345b8cf4878ca21a67233e09034c8cb4dc46240d311838d2989ff481ec285231b5fa5eb36a51fb6c2
|
7
|
+
data.tar.gz: f82659ed6c0f39447f064d2e9916c7b7078c145bb7c4d152bd4155d19810c0a0d76d2f3d40430ffce85ce7e261c91ad6310c18a42872b51859d8f96309e61202
|
data/README.md
CHANGED
@@ -2,6 +2,7 @@
|
|
2
2
|
|
3
3
|
PadUtils is a simple gem containing common utilities and shortcuts. It is used in the [Padstone](http://padstone.io) app builder but can be embedded in any other Ruby project.
|
4
4
|
|
5
|
+
It provides methods to work with text, JSON, hashes, time, to build cli menus and more.
|
5
6
|
|
6
7
|
## Installation
|
7
8
|
|
@@ -10,285 +11,9 @@ It's a Ruby gem. Install it like any other gem!
|
|
10
11
|
`gem install pad_utils`
|
11
12
|
|
12
13
|
|
13
|
-
##
|
14
|
+
## Documentation
|
14
15
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
### Build CLI menus
|
19
|
-
|
20
|
-
|
21
|
-
#### 1. Yes/No menu
|
22
|
-
|
23
|
-
Prompt user with a cli yes/no menu. Returns `true` or `false`.
|
24
|
-
|
25
|
-
* `question`: the question to ask
|
26
|
-
* `default`: the default answer
|
27
|
-
|
28
|
-
`PadUtils.yes_no_menu(question: "Question?", default: "y")`
|
29
|
-
|
30
|
-
|
31
|
-
#### 2. Open question menu
|
32
|
-
|
33
|
-
Prompt user with a cli open question menu. Returns a `string`.
|
34
|
-
|
35
|
-
`PadUtils.question_menu(question)`
|
36
|
-
|
37
|
-
|
38
|
-
#### 3. Multiple choice menu
|
39
|
-
|
40
|
-
Prompt user with a multiple choice menu. Returns a `symbol`.
|
41
|
-
|
42
|
-
* `question`: the question to ask
|
43
|
-
* `choices`: hash of choices (e.g. `{b: "blue", r: "red"}`)
|
44
|
-
* `default`: symbol representing the default value. If none provided, last value in choices hash will be used.
|
45
|
-
|
46
|
-
`PadUtils.choice_menu(question: "Question?", choices: {}, default: nil)`
|
47
|
-
|
48
|
-
|
49
|
-
### Work with text
|
50
|
-
|
51
|
-
|
52
|
-
#### 1. Convert a string to a Rubified name
|
53
|
-
|
54
|
-
Convert a string into a proper "rubified" name. For example, 'app_name' will be converted to 'AppName'. Returns a `string`.
|
55
|
-
|
56
|
-
`PadUtils.convert_to_ruby_name(value)`
|
57
|
-
|
58
|
-
|
59
|
-
#### 2. Convert CamelCase to camel_case (underscores)
|
60
|
-
|
61
|
-
Convert a `CamelCase` word to an underscored one, e.g. `camel_case`. Taken from the Rails [Inflector](http://api.rubyonrails.org/classes/ActiveSupport/Inflector.html#method-i-underscore) class. Returns a `string`.
|
62
|
-
|
63
|
-
`PadUtils.underscore(val)`
|
64
|
-
|
65
|
-
|
66
|
-
#### 3. Sanitize a string
|
67
|
-
|
68
|
-
Sanitize a string by replacing special characters (including spaces) with underscores. Returns a `string`.
|
69
|
-
|
70
|
-
`PadUtils.sanitize(value)`
|
71
|
-
|
72
|
-
|
73
|
-
#### 4. Replace a string in a file
|
74
|
-
|
75
|
-
Replace text within a file. `old_text` can either be a `string` or a `regex`. Doesn't return anything, overwrites `file`.
|
76
|
-
|
77
|
-
`PadUtils.replace_in_file(file, old_text, new_text)`
|
78
|
-
|
79
|
-
|
80
|
-
#### 5. Insert text before first occurence
|
81
|
-
|
82
|
-
Insert text in a string or a file before the first occurence of a string. Returns a `string`. If `is_file` is `true`, overwrites `original` file.
|
83
|
-
|
84
|
-
* `original`: the original string or filename
|
85
|
-
* `tag`: occurence of string to find
|
86
|
-
* `text`: string to insert
|
87
|
-
* `is_file`: `true` if `original` is a filename (default), `false` if it's a `string`
|
88
|
-
|
89
|
-
`PadUtils.insert_before_first(original: nil, tag: nil, text: nil, is_file: true)`
|
90
|
-
|
91
|
-
|
92
|
-
#### 6. Insert text before last occurence
|
93
|
-
|
94
|
-
Insert text in a string or a file before the last occurence of a string. Returns a `string`. If `is_file` is `true`, overwrites `original` file.
|
95
|
-
|
96
|
-
* `original`: the original string or filename
|
97
|
-
* `tag`: occurence of string to find
|
98
|
-
* `text`: string to insert
|
99
|
-
* `is_file`: `true` if `original` is a filename (default), `false` if it's a `string`
|
100
|
-
|
101
|
-
`PadUtils.insert_before_last(original: nil, tag: nil, text: nil, is_file: true)`
|
102
|
-
|
103
|
-
|
104
|
-
#### 7. Insert text after first occurence
|
105
|
-
|
106
|
-
Insert text in a string or a file after the first occurence of a string. Returns a `string`. If `is_file` is `true`, overwrites `original` file.
|
107
|
-
|
108
|
-
* `original`: the original string or filename
|
109
|
-
* `tag`: occurence of string to find
|
110
|
-
* `text`: string to insert
|
111
|
-
* `is_file`: `true` if `original` is a filename (default), `false` if it's a `string`
|
112
|
-
|
113
|
-
`PadUtils.insert_after_first(original: nil, tag: nil, text: nil, is_file: true)`
|
114
|
-
|
115
|
-
|
116
|
-
#### 8. Insert text after last occurence
|
117
|
-
|
118
|
-
Insert text in a string or a file after the last occurence of a string. Returns a `string`. If `is_file` is `true`, overwrites `original` file.
|
119
|
-
|
120
|
-
* `original`: the original string or filename
|
121
|
-
* `tag`: occurence of string to find
|
122
|
-
* `text`: string to insert
|
123
|
-
* `is_file`: `true` if `original` is a filename (default), `false` if it's a `string`
|
124
|
-
|
125
|
-
`PadUtils.insert_after_last(original: nil, tag: nil, text: nil, is_file: true)`
|
126
|
-
|
127
|
-
|
128
|
-
### JSON and Hash
|
129
|
-
|
130
|
-
Few methods to convert JSON to deep symbolized hash and back.
|
131
|
-
|
132
|
-
|
133
|
-
#### 1. Symbolize all keys in a hash
|
134
|
-
|
135
|
-
Convert all keys and sub-keys to symbols in a hash. Emulates the `deep_symbolize_keys` found in [Rails](http://apidock.com/rails/Hash/deep_symbolize_keys). Returns a `Hash`.
|
136
|
-
|
137
|
-
`PadUtils.deep_symbolize_hash_keys(hash)`
|
138
|
-
|
139
|
-
|
140
|
-
#### 2. Convert a JSON string to a symbolized hash
|
141
|
-
|
142
|
-
Returns a `Hash`.
|
143
|
-
|
144
|
-
`PadUtils.json_to_hash(json)`
|
145
|
-
|
146
|
-
|
147
|
-
#### 3. Load a JSON file and convert it to a symbolized hash
|
148
|
-
|
149
|
-
Returns a `Hash`.
|
150
|
-
|
151
|
-
`PadUtils.json_file_to_hash(json_filename)`
|
152
|
-
|
153
|
-
|
154
|
-
#### 4. Convert a hash to JSON
|
155
|
-
|
156
|
-
*Alias method on `to_json` for consistency.*
|
157
|
-
|
158
|
-
Returns a `string`.
|
159
|
-
|
160
|
-
`PadUtils.hash_to_json(hash)`
|
161
|
-
|
162
|
-
|
163
|
-
#### 5. Write a hash to a JSON file
|
164
|
-
|
165
|
-
Returns the file content as a `string`.
|
166
|
-
|
167
|
-
`PadUtils..hash_to_json_file(filename, hash)`
|
168
|
-
|
169
|
-
|
170
|
-
### Work with files
|
171
|
-
|
172
|
-
Mostly, these are convenience methods aliasing existing Ruby methods. Implemented for consistency.
|
173
|
-
|
174
|
-
|
175
|
-
#### 1. Delete a file
|
176
|
-
|
177
|
-
Delete a file. If not found, doesn't raise any error.
|
178
|
-
|
179
|
-
`delete_file(file_path)`
|
180
|
-
|
181
|
-
|
182
|
-
#### 2. Does a file exist?
|
183
|
-
|
184
|
-
Returns `true` or `false`.
|
185
|
-
|
186
|
-
`PadUtils.file_exist?(file_path)`
|
187
|
-
|
188
|
-
|
189
|
-
#### 3. Copy a file
|
190
|
-
|
191
|
-
**Will override file if it already exists!**
|
192
|
-
|
193
|
-
`PadUtils.copy_file(file_path, dest_dir)`
|
194
|
-
|
195
|
-
|
196
|
-
#### 4. Move a file
|
197
|
-
|
198
|
-
**Will not throw an error if original file doesn't exist!**
|
199
|
-
|
200
|
-
`PadUtils.move_file(file_path, dest_file)`
|
201
|
-
|
202
|
-
|
203
|
-
#### 5. Copy multiple files
|
204
|
-
|
205
|
-
`PadUtils.copy_files(files_array, dest_dir)`
|
206
|
-
|
207
|
-
|
208
|
-
#### 6. Copy all files
|
209
|
-
|
210
|
-
Copy all files from a directory to another. *Will create destination if it doesn't exist*.
|
211
|
-
|
212
|
-
`PadUtils.copy_all_files(source_dir, dest_dir)`
|
213
|
-
|
214
|
-
|
215
|
-
#### 7. Create a directory
|
216
|
-
|
217
|
-
Create a directory and subdirectories. **Won't complain if it already exists. Won't override content**.
|
218
|
-
|
219
|
-
`PadUtils.create_directory(dir_name)`
|
220
|
-
|
221
|
-
|
222
|
-
#### 8. Delete a directory and its content
|
223
|
-
|
224
|
-
`PadUtils.delete_directory(dir_name)`
|
225
|
-
|
226
|
-
|
227
|
-
#### 9. Read content of a file
|
228
|
-
|
229
|
-
Returns a `string`.
|
230
|
-
|
231
|
-
`PadUtils.get_file_content(filepath)`
|
232
|
-
|
233
|
-
|
234
|
-
#### 10. Write to a file
|
235
|
-
|
236
|
-
Write content to a file. Create it if it doesn't exist. **Overwrites it if it already exists!**.
|
237
|
-
|
238
|
-
`PadUtils.write_to_file(filepath, content)`
|
239
|
-
|
240
|
-
|
241
|
-
#### 11. Append to a file
|
242
|
-
|
243
|
-
Append content to the end of a file. Create it if it doesn't exist.
|
244
|
-
|
245
|
-
**It will write a newline character first. If you don't want that,
|
246
|
-
set the `new_line` option to `false`**.
|
247
|
-
|
248
|
-
`PadUtils.append_to_file(filepath, content, new_line = true)`
|
249
|
-
|
250
|
-
|
251
|
-
### Logging
|
252
|
-
|
253
|
-
By default, logs will go to `~/pad_logs/logs.txt`.
|
254
|
-
|
255
|
-
* Change default path: `PadUtils.log_path = "/new/path/to/logs"`
|
256
|
-
* Change default file name: `PadUtils.log_file = "my_logs.txt"`
|
257
|
-
|
258
|
-
To log a message, you call `log` and pass it a `message` string. Optionally, you can also pass an `Exception` object in the parameters:
|
259
|
-
|
260
|
-
`PadUtils.log(message, e = nil)`
|
261
|
-
|
262
|
-
|
263
|
-
### Some time methods
|
264
|
-
|
265
|
-
|
266
|
-
#### 1. Time to string timestamp
|
267
|
-
|
268
|
-
Returns a `string` timestamp with the format `YYYYMMDDHHmmss` from a `Time` object.
|
269
|
-
|
270
|
-
`PadUtils.time_to_stamp(val)`
|
271
|
-
|
272
|
-
|
273
|
-
#### 2. String to time
|
274
|
-
|
275
|
-
Returns a `Time` object from a timestamp with the format `YYYYMMDDHHmmss`
|
276
|
-
|
277
|
-
`PadUtils.stamp_to_time(val)`
|
278
|
-
|
279
|
-
|
280
|
-
#### 3. Time to readable string
|
281
|
-
|
282
|
-
Returns a `string` readable timestamp with format `YYYY-MM-DD HH:mm:ss` from a `Time` object.
|
283
|
-
|
284
|
-
`PadUtils.time_to_readable_stamp(val)`
|
285
|
-
|
286
|
-
|
287
|
-
#### 4. Readable timestamp to time
|
288
|
-
|
289
|
-
Returns a `Time` object from a readable timestamp `string` with format `YYYY-MM-DD HH:mm:ss`
|
290
|
-
|
291
|
-
`PadUtils.readable_stamp_to_time(val)`
|
16
|
+
PadUtils is fully documented at [padutils.padstone.io](http://padutils.padstone.io/PadUtils.html) using [YARD](http://www.rubydoc.info/gems/yard/).
|
292
17
|
|
293
18
|
|
294
19
|
## Contribute
|
data/bin/padutils
CHANGED
data/lib/pad_utils/pad_files.rb
CHANGED
@@ -6,70 +6,144 @@ module PadUtils
|
|
6
6
|
# Most are wrappers on FileUtils existing methods. Having them here,
|
7
7
|
# a user of PadUtils doesn't have to remember names and parameters.
|
8
8
|
|
9
|
-
#
|
9
|
+
# Deletes a file.
|
10
|
+
#
|
11
|
+
# @param file_path [String] the file path and name
|
12
|
+
# @return [Void] nothing
|
13
|
+
# @example
|
14
|
+
# PadUtils.delete("path/to/file.txt")
|
10
15
|
def self.delete_file(file_path)
|
11
16
|
FileUtils.rm(file_path, force: true)
|
12
17
|
end
|
13
18
|
|
14
|
-
#
|
19
|
+
# Checks if a file exists.
|
20
|
+
#
|
21
|
+
# @param file_path [String] the file path and name
|
22
|
+
# @return [Boolean]
|
23
|
+
# @example
|
24
|
+
# PadUtils.file_exist?("path/to/file.txt")
|
15
25
|
def self.file_exist?(file_path)
|
16
26
|
File.exist?(file_path)
|
17
27
|
end
|
18
28
|
|
19
|
-
#
|
20
|
-
#
|
29
|
+
# Copies a file.
|
30
|
+
#
|
31
|
+
# @param file_path [String] the source file name and path
|
32
|
+
# @param dest_dir [String] the destination directory
|
33
|
+
# @return [Void] nothing
|
34
|
+
# @example
|
35
|
+
# PadUtils.copy_file("test.txt", "path/to/destination/directory")
|
21
36
|
def self.copy_file(file_path, dest_dir)
|
22
37
|
FileUtils.cp(file_path, dest_dir)
|
23
38
|
end
|
24
39
|
|
25
|
-
|
26
|
-
#
|
40
|
+
|
41
|
+
# Moves a file.
|
42
|
+
#
|
43
|
+
# @param file_path [String] the source file path and name
|
44
|
+
# @param dest_file [String] the destination path and name
|
45
|
+
# @return [Void] nothing
|
46
|
+
# @example
|
47
|
+
# PadUtils.move_file("source.txt", "path/to/destination/renamed.txt")
|
27
48
|
def self.move_file(file_path, dest_file)
|
28
49
|
FileUtils.mv(file_path, dest_file, force: true)
|
29
50
|
end
|
30
51
|
|
31
|
-
#
|
52
|
+
# Copies an array of files.
|
53
|
+
#
|
54
|
+
# @param files [Array<String>] the array of files paths and names to copy
|
55
|
+
# @param dest_dir [String] the destination path
|
56
|
+
# @return [Void] nothing
|
57
|
+
# @example
|
58
|
+
# files = ["file1.txt", "path/to/file2.txt", "path/to/files/file3.txt"]
|
59
|
+
# PadUtils.copy_files(files, "path/to/destination")
|
32
60
|
def self.copy_files(files, dest_dir)
|
33
61
|
files.each do |f|
|
34
62
|
copy_file(f, dest_dir)
|
35
63
|
end
|
36
64
|
end
|
37
65
|
|
38
|
-
#
|
39
|
-
#
|
66
|
+
# Copies all files from a directory.
|
67
|
+
#
|
68
|
+
# @note Will create the destination if it doesn't exist.
|
69
|
+
# Files existing on destination but not on source won't be overwritten.
|
70
|
+
#
|
71
|
+
# @param orig_dir [String] the path to source directory
|
72
|
+
# @return [Void] nothing
|
73
|
+
# @example
|
74
|
+
# PadUtils.copy_all_files("path/to/source", "path/to/destination")
|
40
75
|
def self.copy_all_files(orig_dir, dest_dir)
|
41
76
|
FileUtils.copy_entry(orig_dir, dest_dir)
|
42
77
|
end
|
43
78
|
|
44
|
-
#
|
45
|
-
#
|
79
|
+
# Creates a directory.
|
80
|
+
#
|
81
|
+
# @note Won't override directory content if it already exists.
|
82
|
+
#
|
83
|
+
# @param dir_name [String] the directory path and name
|
84
|
+
# @return [Void] nothing
|
85
|
+
# @example
|
86
|
+
# PadUtils.create_directory("path/to/dir")
|
46
87
|
def self.create_directory(dir_name)
|
47
88
|
FileUtils.mkdir_p(dir_name)
|
48
89
|
end
|
49
90
|
|
50
|
-
#
|
51
|
-
#
|
91
|
+
# Deletes a directory and its content
|
92
|
+
#
|
93
|
+
# @param dir_name [String] the directory path and name
|
94
|
+
# @return [Void] nothing
|
95
|
+
# @example
|
96
|
+
# PadUtils.delete_directory("path/to/dir")
|
52
97
|
def self.delete_directory(dir_name)
|
53
98
|
FileUtils.rm_r(dir_name)
|
54
99
|
end
|
55
100
|
|
56
|
-
# Reads content of a file
|
101
|
+
# Reads the whole content of a file into a string.
|
102
|
+
#
|
103
|
+
# Will log errors using {PadUtils.log PadUtils.log}.
|
104
|
+
#
|
105
|
+
# @param filepath [String] the file path and name
|
106
|
+
# @return [String] the content of the file
|
107
|
+
# @example
|
108
|
+
# PadUtils.get_file_content("path/to/file.txt")
|
57
109
|
def self.get_file_content(filepath)
|
58
110
|
File.read(filepath)
|
59
111
|
rescue Exception => e
|
60
112
|
PadUtils.log("Error in get_file_content", e)
|
61
113
|
end
|
62
114
|
|
63
|
-
#
|
115
|
+
# Writes content to a file.
|
116
|
+
#
|
117
|
+
# @note Will create destination file if it doesn't exist.
|
118
|
+
# Will also overwrite the file if it already exists.
|
119
|
+
#
|
120
|
+
# Will log errors using {PadUtils.log PadUtils.log}.
|
121
|
+
#
|
122
|
+
# @param filepath [String] the file path and name
|
123
|
+
# @param content [String] the content to be written
|
124
|
+
# @return [Void] nothing
|
125
|
+
# @example
|
126
|
+
# PadUtils.write_to_file("path/to/file", "Hello\nThis is a test")
|
64
127
|
def self.write_to_file(filepath, content)
|
65
128
|
File.open(filepath, 'w') { |f| f.write(content)}
|
66
129
|
rescue Exception => e
|
67
130
|
PadUtils.log("Error in write_to_file", e)
|
68
131
|
end
|
69
132
|
|
70
|
-
|
71
|
-
#
|
72
|
-
#
|
133
|
+
|
134
|
+
# Appends content to a file.
|
135
|
+
#
|
136
|
+
# @note Will create the destination file if it doesn't exist.
|
137
|
+
# It also prepends a newline character. Set new_line to false to change this.
|
138
|
+
#
|
139
|
+
# Will log errors using {PadUtils.log PadUtils.log}.
|
140
|
+
#
|
141
|
+
# @param filepath [String] the file path and name
|
142
|
+
# @param content [String] the content to append
|
143
|
+
# @param new_line [Boolean] prepends a newline character
|
144
|
+
# @return [Void] nothing
|
145
|
+
# @example
|
146
|
+
# PadUtils.append_to_file("path/to/file.txt", "Append this!", false)
|
73
147
|
def self.append_to_file(filepath, content, new_line = true)
|
74
148
|
content = "\n#{content}" if new_line
|
75
149
|
File.open(filepath, 'a') { |f| f.write("#{content}")}
|
data/lib/pad_utils/pad_json.rb
CHANGED
@@ -2,43 +2,74 @@ require 'json'
|
|
2
2
|
|
3
3
|
module PadUtils
|
4
4
|
|
5
|
-
#
|
6
|
-
#
|
7
|
-
#
|
5
|
+
# Converts keys and sub-keys to symbols in a hash.
|
6
|
+
#
|
7
|
+
# Emulates Rails {http://apidock.com/rails/Hash/deep_symbolize_keys deep_symbolize_keys} method.
|
8
|
+
#
|
9
|
+
# @param hash [Hash] the hash to symbolize
|
10
|
+
# @return [Hash] the symbolized hash
|
11
|
+
# @example
|
12
|
+
# h = {"name": "Nico", "age": 37, "computers": [{"model": "Mac Pro"}, {"model": "MacBook"}]}
|
13
|
+
# PadUtils.deep_symbolize_hash_keys(h)
|
14
|
+
# # => {:name => "Nico", :age => 37, :computers => [{:model => "Mac Pro"}, {:model => "MacBook"}]}
|
8
15
|
def self.deep_symbolize_hash_keys(hash)
|
9
16
|
return hash.collect { |e| deep_symbolize_hash_keys(e) } if hash.is_a?(Array)
|
10
17
|
return hash.inject({}) { |sh,(k,v)| sh[k.to_sym] = deep_symbolize_hash_keys(v); sh } if hash.is_a?(Hash)
|
11
18
|
hash
|
12
19
|
end
|
13
20
|
|
14
|
-
#
|
15
|
-
#
|
21
|
+
# Converts a JSON string to a symbolized hash.
|
22
|
+
#
|
23
|
+
# Calls {PadUtils.deep_symbolize_hash_keys} on a JSON formatted string.
|
24
|
+
#
|
25
|
+
# @param json [String] the JSON string to convert
|
26
|
+
# @return [Hash] the symbolized hash
|
27
|
+
# @example
|
28
|
+
# PadUtils.json_to_hash(a_json_string)
|
16
29
|
def self.json_to_hash(json)
|
17
30
|
h = JSON.parse(json)
|
18
31
|
self.deep_symbolize_hash_keys(h)
|
19
32
|
end
|
20
33
|
|
21
|
-
#
|
22
|
-
#
|
34
|
+
# Converts a JSON file to a symbolized hash.
|
35
|
+
#
|
36
|
+
# Reads a JSON file and calls {PadUtils.deep_symbolize_hash_keys} on its content.
|
37
|
+
#
|
38
|
+
# @param json_file [String] the json file path and name
|
39
|
+
# @return [Hash] the symbolized hash
|
40
|
+
# @example
|
41
|
+
# PadUtils.json_file_to_hash("path/to/file")
|
23
42
|
def self.json_file_to_hash(json_file)
|
24
43
|
jfile = PadUtils.get_file_content(json_file)
|
25
44
|
h = JSON.parse(jfile)
|
26
45
|
self.deep_symbolize_hash_keys(h)
|
27
46
|
end
|
28
47
|
|
29
|
-
# Convert a hash to JSON
|
30
|
-
#
|
48
|
+
# Convert a hash to a JSON string.
|
49
|
+
#
|
50
|
+
# @param hash [Hash] the hash to convert
|
51
|
+
# @return [String] the JSON string
|
52
|
+
# @example
|
53
|
+
# h = {name: "Nico", age: 37}
|
54
|
+
# PadUtils.hash_to_json(h) # => '{"name": "Nico", "age": 37}'
|
31
55
|
def self.hash_to_json(hash)
|
32
56
|
hash.to_json
|
33
57
|
end
|
34
58
|
|
35
|
-
#
|
36
|
-
#
|
59
|
+
# Writes a hash to a JSON file.
|
60
|
+
#
|
61
|
+
# @note Will overwrite the file if it already exists.
|
62
|
+
#
|
63
|
+
# @param filename [String] the file path and name to write to
|
64
|
+
# @param hash [Hash] the hash to convert to JSON and write
|
65
|
+
# @return [String] the JSON string
|
66
|
+
# @example
|
67
|
+
# h = {name: "Nico", age: 37}
|
68
|
+
# PadUtils.hash_to_json_file("path/to/json_file.txt", h) # => '{"name": "Nico", "age": 37}'
|
37
69
|
def self.hash_to_json_file(filename, hash)
|
38
70
|
json = hash.to_json
|
39
71
|
PadUtils.write_to_file(filename, json)
|
40
72
|
json
|
41
73
|
end
|
42
74
|
|
43
|
-
|
44
75
|
end
|
data/lib/pad_utils/pad_logger.rb
CHANGED
@@ -3,29 +3,54 @@ module PadUtils
|
|
3
3
|
# Module variable holding the log path. By default,
|
4
4
|
# logs will go in ~/pad_logs/
|
5
5
|
@@log_path = "#{ENV["HOME"]}/pad_logs"
|
6
|
-
|
6
|
+
|
7
7
|
# Module variable holding the log file. By default,
|
8
8
|
# logs will go in @@log_path/logs.txt
|
9
9
|
@@log_file = "logs.txt"
|
10
|
-
|
11
|
-
#
|
10
|
+
|
11
|
+
# Sets a different log path.
|
12
|
+
#
|
13
|
+
# @param val [String] the path to the directory
|
14
|
+
# @return [String] the log directory path
|
15
|
+
# @example
|
16
|
+
# PadUtils.set_log_path("path/directory") # => 'path/directory'
|
12
17
|
def self.set_log_path(val)
|
13
18
|
@@log_path = val
|
14
19
|
end
|
15
|
-
|
16
|
-
#
|
20
|
+
|
21
|
+
# Sets a different log file.
|
22
|
+
#
|
23
|
+
# @param val [String] the log file name
|
24
|
+
# @return [String] the log file name
|
25
|
+
# @example
|
26
|
+
# PadUtils.set_log_file("my_logs.txt") # => 'my_logs.txt'
|
17
27
|
def self.set_log_file(val)
|
18
28
|
@@log_file = val
|
19
29
|
end
|
20
|
-
|
21
|
-
#
|
30
|
+
|
31
|
+
# Logs a message.
|
32
|
+
#
|
33
|
+
# @note If using the method within a rescue block, pass the exception from
|
34
|
+
# `rescue Exception => e`
|
35
|
+
#
|
36
|
+
# @param message [String] the message to log
|
37
|
+
# @param e [Exception] the raised exception
|
38
|
+
# @return [Void] nothing
|
39
|
+
# @example
|
40
|
+
# def faulty_method
|
41
|
+
# a = 0
|
42
|
+
# b = 3
|
43
|
+
# c = b / a
|
44
|
+
# rescue Exception => e
|
45
|
+
# PadUtils.log("Something bad happened!", e)
|
46
|
+
# end
|
22
47
|
def self.log(message, e = nil)
|
23
48
|
# Create the log directory if it doesn't exist
|
24
49
|
PadUtils.create_directory(@@log_path)
|
25
|
-
|
50
|
+
|
26
51
|
# Add a timestamp to the message
|
27
52
|
message = "#{PadUtils.time_to_stamp(Time.now)}: #{message}"
|
28
|
-
|
53
|
+
|
29
54
|
# If an error is added, add its inner message to the message
|
30
55
|
# as well as the whole stack
|
31
56
|
if e != nil
|
@@ -36,9 +61,9 @@ module PadUtils
|
|
36
61
|
end
|
37
62
|
message = "#{message}\n"
|
38
63
|
end
|
39
|
-
|
64
|
+
|
40
65
|
# Adds the message to the log file
|
41
|
-
PadUtils.append_to_file("#{@@log_path}/#{@@log_file}", message)
|
66
|
+
PadUtils.append_to_file("#{@@log_path}/#{@@log_file}", message)
|
42
67
|
end
|
43
|
-
|
44
|
-
end
|
68
|
+
|
69
|
+
end
|
data/lib/pad_utils/pad_menu.rb
CHANGED
@@ -1,30 +1,48 @@
|
|
1
1
|
module PadUtils
|
2
2
|
|
3
|
-
#
|
4
|
-
#
|
5
|
-
#
|
3
|
+
# Builds a `yes/no` cli menu.
|
4
|
+
#
|
5
|
+
# Will log errors using {PadUtils.log PadUtils.log}.
|
6
|
+
#
|
7
|
+
# @param question [String] the question to ask
|
8
|
+
# @param default [String] the default answer as `"y"` or `"n"` - (*default: `"y"`*)
|
9
|
+
# @return [String] the answer as `"y"` or `"n"`
|
10
|
+
# @example
|
11
|
+
# PadUtils.yes_no_menu("Is it cold outside?", default: "n")
|
6
12
|
def self.yes_no_menu(question: "Question?", default: "y")
|
7
13
|
default_answer = default == "y" ? "(Y/n)" : "(y/N)"
|
8
14
|
STDOUT.print "#{question} #{default_answer}: "
|
9
15
|
answer = STDIN.gets.chomp.strip.downcase
|
10
16
|
answer = default if answer.length < 1
|
11
17
|
answer == "y"
|
12
|
-
|
13
18
|
rescue Exception => e
|
14
19
|
PadUtils.log("Error in yes/no menu", e)
|
15
20
|
end
|
16
21
|
|
17
|
-
#
|
22
|
+
# Builds an open question menu.
|
23
|
+
#
|
24
|
+
# Will add `:` and a space after the question.
|
25
|
+
#
|
26
|
+
# @param question [String] the question to ask
|
27
|
+
# @return [String] the answer to the question
|
28
|
+
# @example
|
29
|
+
# PadUtils.question_menu("How are you?") # => 'All good'
|
18
30
|
def self.question_menu(question)
|
19
31
|
STDOUT.print "#{question}: "
|
20
32
|
STDIN.gets.chomp.strip
|
21
33
|
end
|
22
34
|
|
23
|
-
#
|
24
|
-
#
|
25
|
-
#
|
26
|
-
#
|
27
|
-
#
|
35
|
+
# Builds a multiple choice menu.
|
36
|
+
#
|
37
|
+
# Will log errors using {PadUtils.log PadUtils.log}.
|
38
|
+
#
|
39
|
+
# @param question [String] the question to ask
|
40
|
+
# @param choices [Hash] the possible choices
|
41
|
+
# @param default [Symbol, nil] the default answer hash key. If nil, the last choice will be used.
|
42
|
+
# @return [Symbol] the chosen option
|
43
|
+
# @example
|
44
|
+
# options = {r: "red", b: "blue", g: "green"}
|
45
|
+
# PadUtils.choice_menu(question: "Which color?", choices: options, default: :b)
|
28
46
|
def self.choice_menu(question: "Question?", choices: {}, default: nil)
|
29
47
|
STDOUT.puts
|
30
48
|
STDOUT.puts "- #{question}"
|
@@ -47,7 +65,6 @@ module PadUtils
|
|
47
65
|
else
|
48
66
|
return choices.keys[answer - 1].to_sym
|
49
67
|
end
|
50
|
-
|
51
68
|
rescue Exception => e
|
52
69
|
PadUtils.log("Error in choice menu", e)
|
53
70
|
end
|
data/lib/pad_utils/pad_text.rb
CHANGED
@@ -1,7 +1,15 @@
|
|
1
1
|
module PadUtils
|
2
2
|
|
3
|
-
#
|
4
|
-
#
|
3
|
+
# Converts a string to a Rubified name.
|
4
|
+
#
|
5
|
+
# @deprecated Use {PadUtils.camel_case PadUtils.camel_case} instead as it will
|
6
|
+
# properly sanitize the string as well.
|
7
|
+
#
|
8
|
+
# @param value [String] the string to rubify
|
9
|
+
# @return [String] the rubified string
|
10
|
+
# @example
|
11
|
+
# s = "hello_you"
|
12
|
+
# PadUtils.convert_to_ruby_name(s) # => 'HelloYou'
|
5
13
|
def self.convert_to_ruby_name(value)
|
6
14
|
if value.scan(/\_|\-/).size > 0
|
7
15
|
value.split(/\_|\-/).map(&:capitalize).join
|
@@ -10,8 +18,37 @@ module PadUtils
|
|
10
18
|
end
|
11
19
|
end
|
12
20
|
|
13
|
-
#
|
14
|
-
#
|
21
|
+
# Converts a string to a CamelCase.
|
22
|
+
#
|
23
|
+
# @note The string will first be sanitized using {PadUtils.sanitize PadUtils.sanitize}.
|
24
|
+
#
|
25
|
+
# @param value [String] the string to CamelCase
|
26
|
+
# @return [String] the CamelCased string
|
27
|
+
# @example
|
28
|
+
# s = "hello_you"
|
29
|
+
# s2 = "hello you"
|
30
|
+
# s3 = "hello $you#"
|
31
|
+
# PadUtils.convert_to_ruby_name(s) # => 'HelloYou'
|
32
|
+
# PadUtils.convert_to_ruby_name(s2) # => 'HelloYou'
|
33
|
+
# PadUtils.convert_to_ruby_name(s3) # => 'HelloYou'
|
34
|
+
def self.camel_case(value)
|
35
|
+
value = self.sanitize(value)
|
36
|
+
if value.scan(/\_|\-/).size > 0
|
37
|
+
value.split(/\_|\-/).map(&:capitalize).join
|
38
|
+
else
|
39
|
+
value.slice(0,1).capitalize + value.slice(1..-1)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
# Converts a CamelCase string to an underscore string.
|
44
|
+
#
|
45
|
+
# Taken from the Rails {http://api.rubyonrails.org/classes/ActiveSupport/Inflector.html#method-i-underscore Inflector}
|
46
|
+
# class.
|
47
|
+
#
|
48
|
+
# @param val [String] the CamelCase string to underscore
|
49
|
+
# @return [String] the under_score string
|
50
|
+
# @example
|
51
|
+
# PadUtils.underscore("CamelCase") # => camel_case
|
15
52
|
def self.underscore(val)
|
16
53
|
word = val.dup
|
17
54
|
word.gsub!(/::/, '/')
|
@@ -22,13 +59,28 @@ module PadUtils
|
|
22
59
|
word
|
23
60
|
end
|
24
61
|
|
25
|
-
#
|
62
|
+
# Sanitizes a string.
|
63
|
+
#
|
64
|
+
# Will only allow alphanumeric characters and underscores.
|
65
|
+
#
|
66
|
+
# @param value [String] the string to sanitize
|
67
|
+
# @return [String] the sanitized string
|
68
|
+
# @example
|
69
|
+
# PadUtils.sanitize("Abc Def *34*#yXz") # => 'Abc_Def__34__yXz'
|
26
70
|
def self.sanitize(value)
|
27
71
|
value.tr('^A-Za-z0-9', '_')
|
28
72
|
end
|
29
73
|
|
30
|
-
#
|
31
|
-
#
|
74
|
+
# Replaces text in a file.
|
75
|
+
#
|
76
|
+
# Will log errors using {PadUtils.log PadUtils.log}.
|
77
|
+
#
|
78
|
+
# @param file [String] the file path and name
|
79
|
+
# @param old_text [String, Regexp] the text to find as a string or regex
|
80
|
+
# @param new_text [String] the new text
|
81
|
+
# @return [Void] nothing
|
82
|
+
# @example
|
83
|
+
# PadUtils.replace_in_file("example.txt", /some_text/, "new text")
|
32
84
|
def self.replace_in_file(file, old_text, new_text)
|
33
85
|
text_update = PadUtils.get_file_content(file)
|
34
86
|
text_update = text_update.gsub(old_text, new_text)
|
@@ -38,11 +90,19 @@ module PadUtils
|
|
38
90
|
PadUtils.log("Error replacing #{old_text} in #{file} with #{new_text}", e)
|
39
91
|
end
|
40
92
|
|
41
|
-
#
|
42
|
-
#
|
43
|
-
#
|
44
|
-
#
|
45
|
-
#
|
93
|
+
# Inserts text before the first occurence of a string.
|
94
|
+
#
|
95
|
+
# Can be used on a string or on a file.
|
96
|
+
#
|
97
|
+
# Will log errors using {PadUtils.log PadUtils.log}.
|
98
|
+
#
|
99
|
+
# @param original [String] the original file path and name *or* the original string
|
100
|
+
# @param tag [String] the string to find
|
101
|
+
# @param text [String] the string to insert
|
102
|
+
# @param is_file [Boolean] `true` if `original` is a file, `false` if it is a string
|
103
|
+
# @return [String] the new content
|
104
|
+
# @example
|
105
|
+
# PadUtils.insert_before_first(original: "file.txt", tag: "end", text: "# hello!")
|
46
106
|
def self.insert_before_first(original: nil, tag: nil, text: nil, is_file: true)
|
47
107
|
# The new text will be consolidated in content
|
48
108
|
content = ""
|
@@ -81,11 +141,19 @@ module PadUtils
|
|
81
141
|
PadUtils.log("Error in insert_before_first", e)
|
82
142
|
end
|
83
143
|
|
84
|
-
#
|
85
|
-
#
|
86
|
-
#
|
87
|
-
#
|
88
|
-
#
|
144
|
+
# Inserts text before the last occurence of a string.
|
145
|
+
#
|
146
|
+
# Can be used on a string or on a file.
|
147
|
+
#
|
148
|
+
# Will log errors using {PadUtils.log PadUtils.log}.
|
149
|
+
#
|
150
|
+
# @param original [String] the original file path and name *or* the original string
|
151
|
+
# @param tag [String] the string to find
|
152
|
+
# @param text [String] the string to insert
|
153
|
+
# @param is_file [Boolean] `true` if `original` is a file, `false` if it is a string
|
154
|
+
# @return [String] the new content
|
155
|
+
# @example
|
156
|
+
# PadUtils.insert_before_last(original: "file.txt", tag: "end", text: "# hello!")
|
89
157
|
def self.insert_before_last(original: nil, tag: nil, text: nil, is_file: true)
|
90
158
|
# The new text will be consolidated in content
|
91
159
|
content = ""
|
@@ -114,11 +182,19 @@ module PadUtils
|
|
114
182
|
PadUtils.log("Error in insert_before_last", e)
|
115
183
|
end
|
116
184
|
|
117
|
-
#
|
118
|
-
#
|
119
|
-
#
|
120
|
-
#
|
121
|
-
#
|
185
|
+
# Inserts text after the first occurence of a string.
|
186
|
+
#
|
187
|
+
# Can be used on a string or on a file.
|
188
|
+
#
|
189
|
+
# Will log errors using {PadUtils.log PadUtils.log}.
|
190
|
+
#
|
191
|
+
# @param original [String] the original file path and name *or* the original string
|
192
|
+
# @param tag [String] the string to find
|
193
|
+
# @param text [String] the string to insert
|
194
|
+
# @param is_file [Boolean] `true` if `original` is a file, `false` if it is a string
|
195
|
+
# @return [String] the new content
|
196
|
+
# @example
|
197
|
+
# PadUtils.insert_after_first(original: "file.txt", tag: "end", text: "# hello!")
|
122
198
|
def self.insert_after_first(original: nil, tag: nil, text: nil, is_file: true)
|
123
199
|
# The new text will be consolidated in content
|
124
200
|
content = ""
|
@@ -157,11 +233,19 @@ module PadUtils
|
|
157
233
|
PadUtils.log("Error in insert_after_first", e)
|
158
234
|
end
|
159
235
|
|
160
|
-
#
|
161
|
-
#
|
162
|
-
#
|
163
|
-
#
|
164
|
-
#
|
236
|
+
# Inserts text after the last occurence of a string.
|
237
|
+
#
|
238
|
+
# Can be used on a string or on a file.
|
239
|
+
#
|
240
|
+
# Will log errors using {PadUtils.log PadUtils.log}.
|
241
|
+
#
|
242
|
+
# @param original [String] the original file path and name *or* the original string
|
243
|
+
# @param tag [String] the string to find
|
244
|
+
# @param text [String] the string to insert
|
245
|
+
# @param is_file [Boolean] `true` if `original` is a file, `false` if it is a string
|
246
|
+
# @return [String] the new content
|
247
|
+
# @example
|
248
|
+
# PadUtils.insert_after_last(original: "file.txt", tag: "end", text: "# hello!")
|
165
249
|
def self.insert_after_last(original: nil, tag: nil, text: nil, is_file: true)
|
166
250
|
# The new text will be consolidated in content
|
167
251
|
content = ""
|
data/lib/pad_utils/pad_time.rb
CHANGED
@@ -2,25 +2,48 @@ require "time"
|
|
2
2
|
|
3
3
|
module PadUtils
|
4
4
|
|
5
|
-
#
|
5
|
+
# Returns a string timestamp.
|
6
|
+
#
|
7
|
+
# Format `YYYYMMDDHHmmss` will be used.
|
8
|
+
#
|
9
|
+
# @param val [Time] the Time object to convert
|
10
|
+
# @return [String] the timestamp
|
11
|
+
# @example
|
12
|
+
# PadUtils.time_to_stamp(Time.now) # => 20160227160122
|
6
13
|
def self.time_to_stamp(val)
|
7
14
|
val.strftime("%Y%m%d%H%M%S")
|
8
15
|
end
|
9
16
|
|
10
|
-
#
|
17
|
+
# Returns a Time object from a string timestamp.
|
18
|
+
#
|
19
|
+
# Will log errors using {PadUtils.log PadUtils.log}.
|
20
|
+
#
|
21
|
+
# @param val [String] a string formatted as `YYYYMMDDHHmmss`
|
22
|
+
# @return [Time] the Time object
|
23
|
+
# @example
|
24
|
+
# PadUtils.stamp_to_time("20160227160122")
|
11
25
|
def self.stamp_to_time(val)
|
12
26
|
Time.parse "#{val[0..3]}-#{val[4..5]}-#{val[6..7]} #{val[8..9]}:#{val[10..11]}:#{val[12..13]}"
|
13
27
|
rescue Exception => e
|
14
28
|
PadUtils.log("Error in stamp_to_time", e)
|
15
29
|
end
|
16
30
|
|
17
|
-
#
|
31
|
+
# Returns a readable string timestamp.
|
32
|
+
#
|
33
|
+
# Format `YYYY-MM-DD HH:mm:ss` will be used.
|
34
|
+
#
|
35
|
+
# @param val [Time] the Time object to convert
|
36
|
+
# @return [String] the readable timestamp
|
18
37
|
def self.time_to_readable_stamp(val)
|
19
38
|
val.strftime("%Y-%m-%d %H:%M:%S")
|
20
39
|
end
|
21
40
|
|
22
|
-
#
|
23
|
-
#
|
41
|
+
# Returns a Time object from a readable timestamp.
|
42
|
+
#
|
43
|
+
# Will log errors using {PadUtils.log PadUtils.log}.
|
44
|
+
#
|
45
|
+
# @param val [String] the readable timestamp formatted as `YYYY-MM-DD HH:mm:ss`
|
46
|
+
# @return [Time] the Time object
|
24
47
|
def self.readable_stamp_to_time(val)
|
25
48
|
Time.parse val
|
26
49
|
rescue Exception => e
|
data/lib/pad_utils/version.rb
CHANGED
data/lib/pad_utils.rb
CHANGED
@@ -6,6 +6,11 @@ require_relative "pad_utils/pad_logger"
|
|
6
6
|
require_relative "pad_utils/pad_menu"
|
7
7
|
require_relative "pad_utils/pad_json"
|
8
8
|
|
9
|
+
# Main namespace for PadUtils.
|
10
|
+
#
|
11
|
+
# Each method is implemented as a module method. Prefix them with PadUtils to call them.
|
12
|
+
# @example
|
13
|
+
# PadUtils.some_method(param)
|
9
14
|
module PadUtils
|
10
15
|
# TODO: Add a cli coloring feature
|
11
16
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pad_utils
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nico Schuele
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-02-
|
11
|
+
date: 2016-02-27 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: PadUtils is a simple gem containing common utilities and shortcuts. It
|
14
14
|
is used in the [Padstone](http://padstone.io) app builder but can be embedded in
|
@@ -57,3 +57,4 @@ signing_key:
|
|
57
57
|
specification_version: 4
|
58
58
|
summary: PadUtils is a simple gem containing common utilities and shortcuts
|
59
59
|
test_files: []
|
60
|
+
has_rdoc:
|