rordash 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/README.md +629 -2
- data/lib/rordash/file_util.rb +1 -1
- data/lib/rordash/version.rb +1 -1
- data/spec/fixtures/sample.csv +3 -0
- data/spec/fixtures/sample.json +6 -0
- metadata +5 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 22a81aeca1fc92e70e311336496eb836394cc4d653cb1fccf729777ebc58bc5e
|
4
|
+
data.tar.gz: 9d339ce6af83f826f8399e89dc865ae73a2186b9dbd4f910451bd5cf1765df3b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c7588b86ce3c215420b672fdc5da4e089e7697ff0a8bbe0083a4373bb78189096c6000dcb9aa98a3415eca79838fc21fa568592e2d67d9768fdbc700cd5b5a05
|
7
|
+
data.tar.gz: 27d4bc53bca5436b6d6d0d893dffbf79038f73687d82380d2a57d7c6d9ffa8dccebca530b2b0644abbcdb9a48070d144eaf602d6c49f538ea7cb524f8d013eb2
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -20,9 +20,636 @@ Or install it yourself as:
|
|
20
20
|
|
21
21
|
$ gem install rordash
|
22
22
|
|
23
|
-
##
|
23
|
+
## HashUtil
|
24
|
+
|
25
|
+
### .from_string
|
26
|
+
|
27
|
+
> parses a JSON string and convert it to a Ruby hash.
|
28
|
+
|
29
|
+
Example:
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
json_str = '{"name": "John", "age": 30, "city": "New York"}'
|
33
|
+
hash = Rordash::HashUtil.from_string(json_str)
|
34
|
+
puts hash
|
35
|
+
# => { :name=>"John", :age=>30, :city=>"New York" }
|
36
|
+
```
|
37
|
+
|
38
|
+
### .to_str
|
39
|
+
|
40
|
+
> converts Hash/Array into a string
|
41
|
+
|
42
|
+
Example:
|
43
|
+
|
44
|
+
```ruby
|
45
|
+
# Example 1: Serialize a hash to a JSON string
|
46
|
+
hash = { name: "John", age: 30, city: "New York" }
|
47
|
+
json_str = Rordash::HashUtil.to_str(hash)
|
48
|
+
puts json_str
|
49
|
+
# Output: {"name":"John","age":30,"city":"New York"}
|
50
|
+
|
51
|
+
# Example 2: Serialize an array to a JSON string
|
52
|
+
arr = [1, "two", { three: 3 }]
|
53
|
+
json_str = Rordash::HashUtil.to_str(arr)
|
54
|
+
puts json_str
|
55
|
+
# Output: [1,"two",{"three":3}]
|
56
|
+
|
57
|
+
# Example 3: Serialize a non-hash, non-array object to a string
|
58
|
+
obj = 123
|
59
|
+
str = Rordash::HashUtil.to_str(obj)
|
60
|
+
puts str
|
61
|
+
# Output: "123"
|
62
|
+
```
|
63
|
+
|
64
|
+
### .pretty
|
65
|
+
|
66
|
+
> Pretty-prints hash or array contents
|
67
|
+
|
68
|
+
Example:
|
69
|
+
|
70
|
+
```ruby
|
71
|
+
# Example 1: Pretty-print a hash as a JSON string
|
72
|
+
hash = { name: "John", age: 30, city: "New York" }
|
73
|
+
json_str = Rordash::HashUtil.pretty(hash)
|
74
|
+
puts json_str
|
75
|
+
# Output:
|
76
|
+
# {
|
77
|
+
# "name": "John",
|
78
|
+
# "age": 30,
|
79
|
+
# "city": "New York"
|
80
|
+
# }
|
81
|
+
|
82
|
+
# Example 2: Pretty-print an array as a JSON string
|
83
|
+
arr = [1, "two", { three: 3 }]
|
84
|
+
json_str = Rordash::HashUtil.pretty(arr)
|
85
|
+
puts json_str
|
86
|
+
# Output:
|
87
|
+
# [
|
88
|
+
# 1,
|
89
|
+
# "two",
|
90
|
+
# {
|
91
|
+
# "three": 3
|
92
|
+
# }
|
93
|
+
# ]
|
94
|
+
|
95
|
+
# Example 3: Return a non-hash, non-array object as is
|
96
|
+
obj = 123
|
97
|
+
result = Rordash::HashUtil.pretty(obj)
|
98
|
+
puts result
|
99
|
+
# Output: 123
|
100
|
+
```
|
101
|
+
|
102
|
+
### .get
|
103
|
+
> retrieves the value at a given JSON dot notation path
|
104
|
+
|
105
|
+
Example:
|
106
|
+
|
107
|
+
```ruby
|
108
|
+
# Example 1: Get a value from a hash
|
109
|
+
hash = { name: { first: "John", last: "Doe" }, age: 30 }
|
110
|
+
value = Rordash::HashUtil.get(hash, "name.first")
|
111
|
+
puts value
|
112
|
+
# Output: "John"
|
113
|
+
|
114
|
+
# Example 2: Get a value from an array
|
115
|
+
arr = [{ name: "John" }, { name: "Jane" }]
|
116
|
+
value = Rordash::HashUtil.get(arr, "1.name")
|
117
|
+
puts value
|
118
|
+
# Output: "Jane"
|
119
|
+
|
120
|
+
# Example 3: Path not found, return default value
|
121
|
+
hash = { name: { first: "John", last: "Doe" }, age: 30 }
|
122
|
+
value = Rordash::HashUtil.get(hash, "address.city", default: "Unknown")
|
123
|
+
puts value
|
124
|
+
# Output: "Unknown"
|
125
|
+
|
126
|
+
```
|
127
|
+
|
128
|
+
### .get_first_present
|
129
|
+
> returns the first value matching one of `dotted_paths`
|
130
|
+
|
131
|
+
Example:
|
132
|
+
|
133
|
+
```ruby
|
134
|
+
# Example 1: Get first present value from hash
|
135
|
+
hash = { name: { first: "", last: "Doe" }, age: nil, city: "New York" }
|
136
|
+
value = Rordash::HashUtil.get_first_present(hash, ["name.first", "name.last", "age", "city"])
|
137
|
+
puts value
|
138
|
+
# Output: "Doe"
|
139
|
+
|
140
|
+
# Example 2: Get first present value from array of hashes
|
141
|
+
arr = [
|
142
|
+
{ name: "John", age: 30 },
|
143
|
+
{ name: "Jane", age: nil },
|
144
|
+
{ name: "Jim", age: 40 }
|
145
|
+
]
|
146
|
+
value = Rordash::HashUtil.get_first_present(arr, ["1.age", "2.name", "0.age"])
|
147
|
+
puts value
|
148
|
+
# Output: 40
|
149
|
+
|
150
|
+
# Example 3: No present values, return nil
|
151
|
+
hash = { name: "", age: nil, city: "" }
|
152
|
+
value = Rordash::HashUtil.get_first_present(hash, ["name.first", "age", "city"])
|
153
|
+
puts value
|
154
|
+
# Output: nil
|
155
|
+
```
|
156
|
+
|
157
|
+
### .set
|
158
|
+
> .set method to set the value of the key at the specified path in the hash to the provided value.
|
159
|
+
|
160
|
+
Example:
|
161
|
+
|
162
|
+
```ruby
|
163
|
+
# Example 1: Set a value in a hash
|
164
|
+
hash = { name: { first: "John", last: "Doe" }, age: 30 }
|
165
|
+
Rordash::HashUtil.set(hash, "name.first", "Jane")
|
166
|
+
puts hash
|
167
|
+
# Output: { name: { first: "Jane", last: "Doe" }, age: 30 }
|
168
|
+
|
169
|
+
# Example 2: Set a value in a new key
|
170
|
+
hash = { name: { first: "John", last: "Doe" }, age: 30 }
|
171
|
+
Rordash::HashUtil.set(hash, "name.middle", "Allen")
|
172
|
+
puts hash
|
173
|
+
# Output: { name: { first: "John", last: "Doe", middle: "Allen" }, age: 30 }
|
174
|
+
|
175
|
+
```
|
176
|
+
|
177
|
+
### .group_by
|
178
|
+
> groups the elements of the enumerable by the key returned by the grouping function.
|
179
|
+
|
180
|
+
Example:
|
181
|
+
|
182
|
+
```ruby
|
183
|
+
# Example 1: Group an array of hashes by a key
|
184
|
+
people = [
|
185
|
+
{ name: "John", age: 30 },
|
186
|
+
{ name: "Jane", age: 25 },
|
187
|
+
{ name: "Jim", age: 30 },
|
188
|
+
{ name: "Janet", age: 25 }
|
189
|
+
]
|
190
|
+
grouped = Rordash::HashUtil.group_by(people, :age)
|
191
|
+
puts grouped
|
192
|
+
# Output: { 30 => [{ name: "John", age: 30 }, { name: "Jim", age: 30 }],
|
193
|
+
# 25 => [{ name: "Jane", age: 25 }, { name: "Janet", age: 25 }] }
|
194
|
+
|
195
|
+
# Example 2: Group a hash of arrays by a proc
|
196
|
+
people = {
|
197
|
+
adults: [{ name: "John", age: 30 }, { name: "Jim", age: 30 }],
|
198
|
+
children: [{ name: "Jane", age: 5 }, { name: "Janet", age: 8 }]
|
199
|
+
}
|
200
|
+
grouped = Rordash::HashUtil.group_by(people) { |_, v| v.length > 1 ? :multiple : :single }
|
201
|
+
puts grouped
|
202
|
+
# Output: { multiple => [:adults => [{ name: "John", age: 30 }, { name: "Jim", age: 30 }]],
|
203
|
+
# single => [:children => [{ name: "Jane", age: 5 }, { name: "Janet", age: 8 }]] }
|
204
|
+
|
205
|
+
```
|
206
|
+
|
207
|
+
### .dot
|
208
|
+
> Converts a nested hash into a flattened hash
|
209
|
+
|
210
|
+
Example:
|
211
|
+
|
212
|
+
```ruby
|
213
|
+
# Example 1: Flattening a nested hash
|
214
|
+
nested_hash = {
|
215
|
+
user: {
|
216
|
+
name: {
|
217
|
+
first: "John",
|
218
|
+
last: "Doe"
|
219
|
+
},
|
220
|
+
age: 30,
|
221
|
+
interests: ["coding", "reading"]
|
222
|
+
},
|
223
|
+
company: {
|
224
|
+
name: "Acme Inc.",
|
225
|
+
address: {
|
226
|
+
street: "123 Main St",
|
227
|
+
city: "Anytown",
|
228
|
+
state: "CA"
|
229
|
+
}
|
230
|
+
}
|
231
|
+
}
|
232
|
+
flat_hash = Rordash::HashUtil.dot(nested_hash)
|
233
|
+
puts flat_hash
|
234
|
+
# Output: {
|
235
|
+
# "user.name.first" => "John",
|
236
|
+
# "user.name.last" => "Doe",
|
237
|
+
# "user.age" => 30,
|
238
|
+
# "user.interests" => ["coding", "reading"],
|
239
|
+
# "company.name" => "Acme Inc.",
|
240
|
+
# "company.address.street" => "123 Main St",
|
241
|
+
# "company.address.city" => "Anytown",
|
242
|
+
# "company.address.state" => "CA"
|
243
|
+
# }
|
244
|
+
|
245
|
+
# Example 2: Flattening a nested hash and excluding arrays
|
246
|
+
nested_hash = {
|
247
|
+
user: {
|
248
|
+
name: {
|
249
|
+
first: "John",
|
250
|
+
last: "Doe"
|
251
|
+
},
|
252
|
+
age: 30,
|
253
|
+
interests: ["coding", "reading"]
|
254
|
+
},
|
255
|
+
company: {
|
256
|
+
name: "Acme Inc.",
|
257
|
+
address: {
|
258
|
+
street: "123 Main St",
|
259
|
+
city: "Anytown",
|
260
|
+
state: "CA"
|
261
|
+
}
|
262
|
+
}
|
263
|
+
}
|
264
|
+
flat_hash = Rordash::HashUtil.dot(nested_hash, keep_arrays: false)
|
265
|
+
puts flat_hash
|
266
|
+
# Output: {
|
267
|
+
# "user.name.first" => "John",
|
268
|
+
# "user.name.last" => "Doe",
|
269
|
+
# "user.age" => 30,
|
270
|
+
# "company.name" => "Acme Inc.",
|
271
|
+
# "company.address.street" => "123 Main St",
|
272
|
+
# "company.address.city" => "Anytown",
|
273
|
+
# "company.address.state" => "CA"
|
274
|
+
# }
|
275
|
+
|
276
|
+
# Example 3: Flattening a nested hash and transforming values with a block
|
277
|
+
nested_hash = {
|
278
|
+
user: {
|
279
|
+
name: {
|
280
|
+
first: "John",
|
281
|
+
last: "Doe"
|
282
|
+
},
|
283
|
+
age: 30,
|
284
|
+
interests: ["coding", "reading"]
|
285
|
+
}
|
286
|
+
}
|
287
|
+
flat_hash = Rordash::HashUtil.dot(nested_hash) do |key, value|
|
288
|
+
key.start_with?("user") ? value.upcase : value
|
289
|
+
end
|
290
|
+
puts flat_hash
|
291
|
+
# Output: {
|
292
|
+
# "user.name.first" => "JOHN",
|
293
|
+
# ...
|
294
|
+
# }
|
295
|
+
|
296
|
+
```
|
297
|
+
|
298
|
+
### .deep_key?
|
299
|
+
> checks if a given key exists in a hash or nested hash.
|
300
|
+
|
301
|
+
Example:
|
302
|
+
|
303
|
+
```ruby
|
304
|
+
hash = { 'a' => { 'b' => { 'c' => 1 } } }
|
305
|
+
Rordash::HashUtil.deep_key?(hash, 'a.b.c') # returns true
|
306
|
+
Rordash::HashUtil.deep_key?(hash, 'a.b.d') # returns false
|
307
|
+
Rordash::HashUtil.deep_key?(hash, 'a') # returns true
|
308
|
+
Rordash::HashUtil.deep_key?(hash, 'x.y.z') # returns false
|
309
|
+
```
|
310
|
+
|
311
|
+
### .dotted_keys method returns an array of all the dotted keys in a hash or nested hash.
|
312
|
+
|
313
|
+
Example:
|
314
|
+
|
315
|
+
```ruby
|
316
|
+
hash = { 'a' => { 'b' => { 'c' => 1 } } }
|
317
|
+
Rordash::HashUtil.dotted_keys(hash) # returns ['a.b.c']
|
318
|
+
Rordash::HashUtil.dotted_keys(hash, false) # returns ['a', 'a.b', 'a.b.c']
|
319
|
+
```
|
320
|
+
|
321
|
+
### .pick
|
322
|
+
|
323
|
+
> Extracts a subset of a hash based on the given paths
|
324
|
+
|
325
|
+
Example:
|
326
|
+
|
327
|
+
```ruby
|
328
|
+
hash = { a: 1, b: { c: 2, d: { e: 3 } } }
|
329
|
+
Rordash::HashUtil.pick(hash, 'a') # returns { a: 1 }
|
330
|
+
Rordash::HashUtil.pick(hash, 'b.c') # returns { b: { c: 2 } }
|
331
|
+
Rordash::HashUtil.pick(hash, 'b.d.e') # returns { b: { d: { e: 3 } } }
|
332
|
+
Rordash::HashUtil.pick(hash, ['a', 'b.d']) # returns { a: 1, b: { d: { e: 3 } } }
|
333
|
+
```
|
334
|
+
|
335
|
+
### .undot
|
336
|
+
> Converts a dotted hash into a regular hash
|
337
|
+
|
338
|
+
```ruby
|
339
|
+
dotted_hash = {
|
340
|
+
"person.name.first": "John",
|
341
|
+
"person.name.last": "Doe",
|
342
|
+
"person.age": 30
|
343
|
+
}
|
344
|
+
|
345
|
+
regular_hash = Rordash::HashUtil.undot(dotted_hash)
|
346
|
+
|
347
|
+
# Output:
|
348
|
+
# {
|
349
|
+
# "person" => {
|
350
|
+
# "name" => {
|
351
|
+
# "first" => "John",
|
352
|
+
# "last" => "Doe"
|
353
|
+
# },
|
354
|
+
# "age" => 30
|
355
|
+
# }
|
356
|
+
# }
|
357
|
+
|
358
|
+
dotted_hash = {
|
359
|
+
"person.name.first": "John",
|
360
|
+
"person.name.last": "Doe",
|
361
|
+
"person.age": 30
|
362
|
+
}
|
363
|
+
|
364
|
+
# Usage with a block
|
365
|
+
regular_hash = Rordash::HashUtil.undot(dotted_hash) do |key, value|
|
366
|
+
if key == "person.name.first"
|
367
|
+
value.upcase
|
368
|
+
else
|
369
|
+
value
|
370
|
+
end
|
371
|
+
end
|
372
|
+
|
373
|
+
# Output:
|
374
|
+
# {
|
375
|
+
# "person" => {
|
376
|
+
# "name" => {
|
377
|
+
# "first" => "JOHN",
|
378
|
+
# "last" => "Doe"
|
379
|
+
# },
|
380
|
+
# "age" => 30
|
381
|
+
# }
|
382
|
+
# }
|
383
|
+
|
384
|
+
```
|
385
|
+
|
386
|
+
### .deep_compact
|
387
|
+
> Recursively compacts all values from a nested hash
|
388
|
+
|
389
|
+
```ruby
|
390
|
+
hash = { foo: { bar: [1, 2, nil] }, baz: { qux: nil } }
|
391
|
+
result = Rordash::HashUtil.deep_compact(hash) # => { foo: { bar: [1, 2] } }
|
392
|
+
|
393
|
+
# using each_value_proc to remove nils and blank strings
|
394
|
+
result = Rordash::HashUtil.deep_compact(hash) do |k, v|
|
395
|
+
if v.is_a?(String)
|
396
|
+
v.strip!
|
397
|
+
v unless v.empty?
|
398
|
+
else
|
399
|
+
v.compact
|
400
|
+
end
|
401
|
+
end
|
402
|
+
```
|
403
|
+
|
404
|
+
### .reject_blank_values
|
405
|
+
> Rejects key value pairs that are considered blank
|
406
|
+
|
407
|
+
Example:
|
408
|
+
```ruby
|
409
|
+
obj = { foo: '', bar: ' ', baz: nil, qux: [1, 2, '', nil] }
|
410
|
+
result = Rordash::HashUtil.reject_blank_values(obj)
|
411
|
+
puts result.inspect # output: { bar: '', qux: [1, 2] }
|
412
|
+
```
|
413
|
+
|
414
|
+
### .deep_reject_blank_values
|
415
|
+
> Rejects key value pairs that are considered blank from nested hashes
|
416
|
+
|
417
|
+
Example:
|
418
|
+
|
419
|
+
```ruby
|
420
|
+
# Define a hash with nested arrays and values
|
421
|
+
attrs = {
|
422
|
+
name: 'John',
|
423
|
+
age: nil,
|
424
|
+
address: {
|
425
|
+
street: '123 Main St',
|
426
|
+
city: 'Anytown',
|
427
|
+
state: '',
|
428
|
+
zip: ' ',
|
429
|
+
},
|
430
|
+
phones: [
|
431
|
+
{
|
432
|
+
type: 'home',
|
433
|
+
number: '123-456-7890',
|
434
|
+
ext: '',
|
435
|
+
},
|
436
|
+
{
|
437
|
+
type: 'work',
|
438
|
+
number: '',
|
439
|
+
ext: nil,
|
440
|
+
},
|
441
|
+
{
|
442
|
+
type: '',
|
443
|
+
number: '',
|
444
|
+
ext: '',
|
445
|
+
},
|
446
|
+
]
|
447
|
+
}
|
448
|
+
|
449
|
+
# Remove all nil, empty, or blank values recursively
|
450
|
+
clean_attrs = Rordash::HashUtil.deep_reject_blank_values(attrs)
|
451
|
+
|
452
|
+
# Print the cleaned hash
|
453
|
+
puts clean_attrs.inspect
|
454
|
+
|
455
|
+
```
|
456
|
+
|
457
|
+
### .deep_symbolize_keys
|
458
|
+
> Recursively symbolizes all the keys in a nested hash or array.
|
459
|
+
|
460
|
+
Example:
|
461
|
+
|
462
|
+
```ruby
|
463
|
+
hash = { 'foo' => { 'bar' => 'baz' }, 'arr' => [{ 'a' => 1 }, { 'b' => 2 }] }
|
464
|
+
Utils::HashUtil.deep_symbolize_keys(hash)
|
465
|
+
#=> { :foo => { :bar => "baz" }, :arr => [{ :a => 1 }, { :b => 2 }] }
|
466
|
+
|
467
|
+
```
|
468
|
+
|
469
|
+
## NumericUtil
|
470
|
+
|
471
|
+
### .numeric?
|
472
|
+
> returns a boolean indicating whether the value is numeric or not
|
473
|
+
|
474
|
+
Example:
|
475
|
+
|
476
|
+
```ruby
|
477
|
+
numeric?("123") #=> true
|
478
|
+
numeric?("123.45") #=> true
|
479
|
+
numeric?("abc") #=> false
|
480
|
+
numeric?("") #=> false
|
481
|
+
numeric?(nil) #=> false
|
482
|
+
```
|
483
|
+
|
484
|
+
### .convert_unit
|
485
|
+
> Convert a value from one unit to another. Specifically, it's using the `Measured::Length` class to perform conversions
|
486
|
+
|
487
|
+
Example:
|
488
|
+
|
489
|
+
```ruby
|
490
|
+
# convert 10 meters to feet
|
491
|
+
converted_value = convert_unit(10, from_unit: :meters, to_unit: :feet)
|
492
|
+
puts converted_value # output: 32.80839895013123
|
493
|
+
```
|
494
|
+
|
495
|
+
### .convert_unit_sq
|
496
|
+
> Converts an area from one unit to another.
|
497
|
+
|
498
|
+
Example:
|
499
|
+
|
500
|
+
```ruby
|
501
|
+
# Convert an area of 10 square feet to square meters
|
502
|
+
value = 10
|
503
|
+
from_unit = 'ft^2'
|
504
|
+
to_unit = 'm^2'
|
505
|
+
converted_value = convert_unit_sq(value, from_unit: from_unit, to_unit: to_unit)
|
506
|
+
|
507
|
+
puts "#{value} #{from_unit} is equal to #{converted_value} #{to_unit}"
|
508
|
+
# Output: "10 ft^2 is equal to 0.9290304 m^2"
|
509
|
+
```
|
510
|
+
## FileUtil
|
511
|
+
|
512
|
+
### .filename_with_ext_from
|
513
|
+
|
514
|
+
> generates appropriate filename and extension
|
515
|
+
|
516
|
+
Example:
|
517
|
+
|
518
|
+
```ruby
|
519
|
+
filename = "myfile"
|
520
|
+
content_type = "image/png"
|
521
|
+
new_filename = Rordash::FileUtil.filename_with_ext_from(filename: filename, content_type: content_type)
|
522
|
+
puts new_filename # "myfile.png"
|
523
|
+
```
|
524
|
+
|
525
|
+
### .content_type_to_extension
|
526
|
+
|
527
|
+
> maps a content type string to its corresponding file extension.
|
528
|
+
|
529
|
+
Example:
|
530
|
+
|
531
|
+
```ruby
|
532
|
+
content_type = "application/pdf"
|
533
|
+
extension = Rordash::FileUtil.content_type_to_extension(content_type)
|
534
|
+
puts extension # "pdf"
|
535
|
+
```
|
536
|
+
|
537
|
+
### .fixture_file_path
|
538
|
+
> returns the absolute file path for a file under spec/fixtures/files directory.
|
539
|
+
|
540
|
+
Example:
|
541
|
+
|
542
|
+
```ruby
|
543
|
+
file_path = Rordash::FileUtil.fixture_file_path('example.txt')
|
544
|
+
#=> #<Pathname:/Users/<username>/repo/spec/fixtures/files/sample.csv>
|
545
|
+
```
|
546
|
+
|
547
|
+
### .read_fixture_file
|
548
|
+
> Reads the contents of a file located in the project's fixture directory.
|
549
|
+
|
550
|
+
Example:
|
551
|
+
```ruby
|
552
|
+
Rordash::FileUtil.read_fixture_file('sample.csv')
|
553
|
+
#=> "name,date,favorite_color\nJohn Smith,Oct 2 1901,blue\nGemma Jones,Sept 1 2018,silver"
|
554
|
+
```
|
555
|
+
|
556
|
+
### .open_fixture_file
|
557
|
+
|
558
|
+
> returns a File object for a given fixture file.
|
559
|
+
|
560
|
+
Example:
|
561
|
+
|
562
|
+
```ruby
|
563
|
+
file = Rordash::FileUtil.open_fixture_file('sample.csv')
|
564
|
+
#=> <File:/Users/<username>/repo/spec/fixtures/files/sample.csv>
|
565
|
+
|
566
|
+
if file.nil?
|
567
|
+
puts "Fixture file does not exist"
|
568
|
+
else
|
569
|
+
puts "Fixture file contents:"
|
570
|
+
puts file.read
|
571
|
+
file.close
|
572
|
+
end
|
573
|
+
```
|
574
|
+
|
575
|
+
### .read_fixture_file_as_hash
|
576
|
+
|
577
|
+
> reads the contents of a fixture file, specified by its relative path, and returns it as a hash.
|
578
|
+
|
579
|
+
Example:
|
580
|
+
|
581
|
+
```ruby
|
582
|
+
# Given a fixture file `sample.json` with the following content:
|
583
|
+
# [
|
584
|
+
# {
|
585
|
+
# "color": "red",
|
586
|
+
# "value": "#f00"
|
587
|
+
# }
|
588
|
+
# ]
|
589
|
+
|
590
|
+
# We can read it as a hash using the `read_fixture_file_as_hash` method:
|
591
|
+
hash = Rordash::FileUtil.read_fixture_file_as_hash('sample.json')
|
592
|
+
puts hash.inspect
|
593
|
+
# Output: [{:color=>"red", :value=>"#f00"}]
|
594
|
+
```
|
595
|
+
|
596
|
+
### .create_file_blob
|
597
|
+
> Creates an ActiveStorage::Blob record for a file specified by its filename. Useful for creating ActiveStorage::Blob records in a test environment.
|
598
|
+
|
599
|
+
```ruby
|
600
|
+
blob = Rordash::FileUtil.create_file_blob('sample.json')
|
601
|
+
puts blob
|
602
|
+
#=> <ActiveStorage::Blob>
|
603
|
+
```
|
604
|
+
|
605
|
+
### .file_url_for
|
606
|
+
> Generates a URL for a file. It takes a filename as input and returns a string URL.
|
607
|
+
|
608
|
+
Example:
|
609
|
+
|
610
|
+
```ruby
|
611
|
+
url = Rordash::FileUtil.file_url_for(filename)
|
612
|
+
#=> "http://jaskolski.biz/example.txt"
|
613
|
+
```
|
614
|
+
|
615
|
+
### .content_type_from_filename
|
616
|
+
> determines the content type of a file based on its extension.
|
617
|
+
|
618
|
+
Example:
|
619
|
+
|
620
|
+
```ruby
|
621
|
+
content_type = DebugUtil.content_type_from_filename('my_file.jpg')
|
622
|
+
puts content_type # => "image/jpeg"
|
623
|
+
```
|
624
|
+
|
625
|
+
## DebugUtil
|
626
|
+
|
627
|
+
### .calculate_duration
|
628
|
+
|
629
|
+
> `.calculate_duration` calculates the duration of a block of code and logs it to the console.
|
630
|
+
|
631
|
+
```ruby
|
632
|
+
Rordash::DebugUtil.calculate_duration(tag: 'my_code_block') do
|
633
|
+
# your code here
|
634
|
+
end
|
635
|
+
```
|
636
|
+
|
637
|
+
This will log the duration with the specified tag: `tag: my_code_block - total duration - 0 hours 0 minutes and 0.0 seconds`.
|
638
|
+
|
639
|
+
### .wrap_stack_prof
|
640
|
+
|
641
|
+
> `.wrap_stack_prof` runs a code block and profiles its execution with `StackProf`. Useful for identifying performance bottlenecks in your code.
|
642
|
+
|
643
|
+
To use `.wrap_stack_prof`, simply pass a block of code as an argument to the method. For example:
|
644
|
+
|
645
|
+
```ruby
|
646
|
+
Rordash::DebugUtil.wrap_stack_prof(tag: 'my_profile_run', out: 'path/to/output.dump') do
|
647
|
+
# your code here
|
648
|
+
end
|
649
|
+
```
|
650
|
+
|
651
|
+
This will log the duration and output file with the specified tag: `tag: my_profile_run - total duration - 0 hours 0 minutes and 0.0 seconds` and `StackProf` `output file: path/to/output.dump`.
|
24
652
|
|
25
|
-
TODO: Write usage instructions here
|
26
653
|
|
27
654
|
## Development
|
28
655
|
|
data/lib/rordash/file_util.rb
CHANGED
data/lib/rordash/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rordash
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Desmond O'Leary
|
@@ -271,6 +271,8 @@ files:
|
|
271
271
|
- spec/coverage_helper.rb
|
272
272
|
- spec/fixtures/files/sample.csv
|
273
273
|
- spec/fixtures/files/sample.json
|
274
|
+
- spec/fixtures/sample.csv
|
275
|
+
- spec/fixtures/sample.json
|
274
276
|
- spec/rordash/debug_util_spec.rb
|
275
277
|
- spec/rordash/file_util_spec.rb
|
276
278
|
- spec/rordash/hash_util_spec.rb
|
@@ -311,6 +313,8 @@ test_files:
|
|
311
313
|
- spec/coverage_helper.rb
|
312
314
|
- spec/fixtures/files/sample.csv
|
313
315
|
- spec/fixtures/files/sample.json
|
316
|
+
- spec/fixtures/sample.csv
|
317
|
+
- spec/fixtures/sample.json
|
314
318
|
- spec/rordash/debug_util_spec.rb
|
315
319
|
- spec/rordash/file_util_spec.rb
|
316
320
|
- spec/rordash/hash_util_spec.rb
|