pad_utils 1.1.1 → 1.2.1
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/README.md +53 -6
- data/lib/pad_utils/pad_text.rb +12 -0
- data/lib/pad_utils/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0d994307acebee505edfb95deac3c785681b8595
|
4
|
+
data.tar.gz: 84663dbf244c403d7f20cb105b9756d7f2ccff44
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1e350721bb28b31c559e34ac7ee2e728af50337a5624c5757cb61bfac56a57a66c0f3c4b924d4b61cf17944311bdeaa94e170b3003a55021eca2fbd54c0d697e
|
7
|
+
data.tar.gz: c91c8147eda9bda2bcd6888375a382a78698f1d6dfaff5c1c62cbef94981b75704befe0ad38928e7a95888641069b22ed273075d2d84f6f6981cc029cc762113
|
data/README.md
CHANGED
@@ -2,18 +2,22 @@
|
|
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
|
+
|
5
6
|
## Installation
|
6
7
|
|
7
8
|
It's a Ruby gem. Install it like any other gem!
|
8
9
|
|
9
10
|
`gem install pad_utils`
|
10
11
|
|
12
|
+
|
11
13
|
## Usage
|
12
14
|
|
13
15
|
Just `require 'pad_utils'` within your code to access the following methods.
|
14
16
|
|
17
|
+
|
15
18
|
### Build CLI menus
|
16
19
|
|
20
|
+
|
17
21
|
#### 1. Yes/No menu
|
18
22
|
|
19
23
|
Prompt user with a cli yes/no menu. Returns `true` or `false`.
|
@@ -23,12 +27,14 @@ Prompt user with a cli yes/no menu. Returns `true` or `false`.
|
|
23
27
|
|
24
28
|
`PadUtils.yes_no_menu(question: "Question?", default: "y")`
|
25
29
|
|
30
|
+
|
26
31
|
#### 2. Open question menu
|
27
32
|
|
28
33
|
Prompt user with a cli open question menu. Returns a `string`.
|
29
34
|
|
30
35
|
`PadUtils.question_menu(question)`
|
31
36
|
|
37
|
+
|
32
38
|
#### 3. Multiple choice menu
|
33
39
|
|
34
40
|
Prompt user with a multiple choice menu. Returns a `symbol`.
|
@@ -42,25 +48,36 @@ Prompt user with a multiple choice menu. Returns a `symbol`.
|
|
42
48
|
|
43
49
|
### Work with text
|
44
50
|
|
51
|
+
|
45
52
|
#### 1. Convert a string to a Rubified name
|
46
53
|
|
47
54
|
Convert a string into a proper "rubified" name. For example, 'app_name' will be converted to 'AppName'. Returns a `string`.
|
48
55
|
|
49
56
|
`PadUtils.convert_to_ruby_name(value)`
|
50
57
|
|
51
|
-
|
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
|
52
67
|
|
53
68
|
Sanitize a string by replacing special characters (including spaces) with underscores. Returns a `string`.
|
54
69
|
|
55
70
|
`PadUtils.sanitize(value)`
|
56
71
|
|
57
|
-
|
72
|
+
|
73
|
+
#### 4. Replace a string in a file
|
58
74
|
|
59
75
|
Replace text within a file. `old_text` can either be a `string` or a `regex`. Doesn't return anything, overwrites `file`.
|
60
76
|
|
61
77
|
`PadUtils.replace_in_file(file, old_text, new_text)`
|
62
78
|
|
63
|
-
|
79
|
+
|
80
|
+
#### 5. Insert text before first occurence
|
64
81
|
|
65
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.
|
66
83
|
|
@@ -71,7 +88,8 @@ Insert text in a string or a file before the first occurence of a string. Return
|
|
71
88
|
|
72
89
|
`PadUtils.insert_before_first(original: nil, tag: nil, text: nil, is_file: true)`
|
73
90
|
|
74
|
-
|
91
|
+
|
92
|
+
#### 6. Insert text before last occurence
|
75
93
|
|
76
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.
|
77
95
|
|
@@ -82,7 +100,8 @@ Insert text in a string or a file before the last occurence of a string. Returns
|
|
82
100
|
|
83
101
|
`PadUtils.insert_before_last(original: nil, tag: nil, text: nil, is_file: true)`
|
84
102
|
|
85
|
-
|
103
|
+
|
104
|
+
#### 7. Insert text after first occurence
|
86
105
|
|
87
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.
|
88
107
|
|
@@ -93,7 +112,8 @@ Insert text in a string or a file after the first occurence of a string. Returns
|
|
93
112
|
|
94
113
|
`PadUtils.insert_after_first(original: nil, tag: nil, text: nil, is_file: true)`
|
95
114
|
|
96
|
-
|
115
|
+
|
116
|
+
#### 8. Insert text after last occurence
|
97
117
|
|
98
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.
|
99
119
|
|
@@ -104,28 +124,33 @@ Insert text in a string or a file after the last occurence of a string. Returns
|
|
104
124
|
|
105
125
|
`PadUtils.insert_after_last(original: nil, tag: nil, text: nil, is_file: true)`
|
106
126
|
|
127
|
+
|
107
128
|
### JSON and Hash
|
108
129
|
|
109
130
|
Few methods to convert JSON to deep symbolized hash and back.
|
110
131
|
|
132
|
+
|
111
133
|
#### 1. Symbolize all keys in a hash
|
112
134
|
|
113
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`.
|
114
136
|
|
115
137
|
`PadUtils.deep_symbolize_hash_keys(hash)`
|
116
138
|
|
139
|
+
|
117
140
|
#### 2. Convert a JSON string to a symbolized hash
|
118
141
|
|
119
142
|
Returns a `Hash`.
|
120
143
|
|
121
144
|
`PadUtils.json_to_hash(json)`
|
122
145
|
|
146
|
+
|
123
147
|
#### 3. Load a JSON file and convert it to a symbolized hash
|
124
148
|
|
125
149
|
Returns a `Hash`.
|
126
150
|
|
127
151
|
`PadUtils.json_file_to_hash(json_filename)`
|
128
152
|
|
153
|
+
|
129
154
|
#### 4. Convert a hash to JSON
|
130
155
|
|
131
156
|
*Alias method on `to_json` for consistency.*
|
@@ -134,72 +159,85 @@ Returns a `string`.
|
|
134
159
|
|
135
160
|
`PadUtils.hash_to_json(hash)`
|
136
161
|
|
162
|
+
|
137
163
|
#### 5. Write a hash to a JSON file
|
138
164
|
|
139
165
|
Returns the file content as a `string`.
|
140
166
|
|
141
167
|
`PadUtils..hash_to_json_file(filename, hash)`
|
142
168
|
|
169
|
+
|
143
170
|
### Work with files
|
144
171
|
|
145
172
|
Mostly, these are convenience methods aliasing existing Ruby methods. Implemented for consistency.
|
146
173
|
|
174
|
+
|
147
175
|
#### 1. Delete a file
|
148
176
|
|
149
177
|
Delete a file. If not found, doesn't raise any error.
|
150
178
|
|
151
179
|
`delete_file(file_path)`
|
152
180
|
|
181
|
+
|
153
182
|
#### 2. Does a file exist?
|
154
183
|
|
155
184
|
Returns `true` or `false`.
|
156
185
|
|
157
186
|
`PadUtils.file_exist?(file_path)`
|
158
187
|
|
188
|
+
|
159
189
|
#### 3. Copy a file
|
160
190
|
|
161
191
|
**Will override file if it already exists!**
|
162
192
|
|
163
193
|
`PadUtils.copy_file(file_path, dest_dir)`
|
164
194
|
|
195
|
+
|
165
196
|
#### 4. Move a file
|
166
197
|
|
167
198
|
**Will not throw an error if original file doesn't exist!**
|
168
199
|
|
169
200
|
`PadUtils.move_file(file_path, dest_file)`
|
170
201
|
|
202
|
+
|
171
203
|
#### 5. Copy multiple files
|
172
204
|
|
173
205
|
`PadUtils.copy_files(files_array, dest_dir)`
|
174
206
|
|
207
|
+
|
175
208
|
#### 6. Copy all files
|
176
209
|
|
177
210
|
Copy all files from a directory to another. *Will create destination if it doesn't exist*.
|
178
211
|
|
179
212
|
`PadUtils.copy_all_files(source_dir, dest_dir)`
|
180
213
|
|
214
|
+
|
181
215
|
#### 7. Create a directory
|
182
216
|
|
183
217
|
Create a directory and subdirectories. **Won't complain if it already exists. Won't override content**.
|
184
218
|
|
185
219
|
`PadUtils.create_directory(dir_name)`
|
186
220
|
|
221
|
+
|
187
222
|
#### 8. Delete a directory and its content
|
188
223
|
|
189
224
|
`PadUtils.delete_directory(dir_name)`
|
190
225
|
|
226
|
+
|
191
227
|
#### 9. Read content of a file
|
192
228
|
|
193
229
|
Returns a `string`.
|
194
230
|
|
195
231
|
`PadUtils.get_file_content(filepath)`
|
196
232
|
|
233
|
+
|
197
234
|
#### 10. Write to a file
|
198
235
|
|
199
236
|
Write content to a file. Create it if it doesn't exist. **Overwrites it if it already exists!**.
|
200
237
|
|
201
238
|
`PadUtils.write_to_file(filepath, content)`
|
202
239
|
|
240
|
+
|
203
241
|
#### 11. Append to a file
|
204
242
|
|
205
243
|
Append content to the end of a file. Create it if it doesn't exist.
|
@@ -209,6 +247,7 @@ set the `new_line` option to `false`**.
|
|
209
247
|
|
210
248
|
`PadUtils.append_to_file(filepath, content, new_line = true)`
|
211
249
|
|
250
|
+
|
212
251
|
### Logging
|
213
252
|
|
214
253
|
By default, logs will go to `~/pad_logs/logs.txt`.
|
@@ -220,40 +259,48 @@ To log a message, you call `log` and pass it a `message` string. Optionally, you
|
|
220
259
|
|
221
260
|
`PadUtils.log(message, e = nil)`
|
222
261
|
|
262
|
+
|
223
263
|
### Some time methods
|
224
264
|
|
265
|
+
|
225
266
|
#### 1. Time to string timestamp
|
226
267
|
|
227
268
|
Returns a `string` timestamp with the format `YYYYMMDDHHmmss` from a `Time` object.
|
228
269
|
|
229
270
|
`PadUtils.time_to_stamp(val)`
|
230
271
|
|
272
|
+
|
231
273
|
#### 2. String to time
|
232
274
|
|
233
275
|
Returns a `Time` object from a timestamp with the format `YYYYMMDDHHmmss`
|
234
276
|
|
235
277
|
`PadUtils.stamp_to_time(val)`
|
236
278
|
|
279
|
+
|
237
280
|
#### 3. Time to readable string
|
238
281
|
|
239
282
|
Returns a `string` readable timestamp with format `YYYY-MM-DD HH:mm:ss` from a `Time` object.
|
240
283
|
|
241
284
|
`PadUtils.time_to_readable_stamp(val)`
|
242
285
|
|
286
|
+
|
243
287
|
#### 4. Readable timestamp to time
|
244
288
|
|
245
289
|
Returns a `Time` object from a readable timestamp `string` with format `YYYY-MM-DD HH:mm:ss`
|
246
290
|
|
247
291
|
`PadUtils.readable_stamp_to_time(val)`
|
248
292
|
|
293
|
+
|
249
294
|
## Contribute
|
250
295
|
|
251
296
|
[Get in touch](https://twitter.com/nicoschuele) before submitting a pull request, I don't want to waste your time by rejecting it!
|
252
297
|
|
298
|
+
|
253
299
|
## Testing
|
254
300
|
|
255
301
|
The test suite for PadUtils is currently outside of the gem itself. I may add it if I have time.
|
256
302
|
|
303
|
+
|
257
304
|
## License
|
258
305
|
|
259
306
|
Copyright 2016 - Nico Schuele
|
data/lib/pad_utils/pad_text.rb
CHANGED
@@ -10,6 +10,18 @@ module PadUtils
|
|
10
10
|
end
|
11
11
|
end
|
12
12
|
|
13
|
+
# Convert a CamelCase word to an underscored one, e.g. camel_case
|
14
|
+
# Taken from the Rails Inflector class.
|
15
|
+
def self.underscore(val)
|
16
|
+
word = val.dup
|
17
|
+
word.gsub!(/::/, '/')
|
18
|
+
word.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
|
19
|
+
word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
|
20
|
+
word.tr!("-", "_")
|
21
|
+
word.downcase!
|
22
|
+
word
|
23
|
+
end
|
24
|
+
|
13
25
|
# Convert a string to only alphanumeric and underscores
|
14
26
|
def self.sanitize(value)
|
15
27
|
value.tr('^A-Za-z0-9', '_')
|
data/lib/pad_utils/version.rb
CHANGED