rordash 0.1.0 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e4ff085f2f49437e60826d2b3c45ec718e745ec5749aad64258d083c74b3a0fc
4
- data.tar.gz: 98380816b48dd3dd06c9528396908607902068846cc98dd4cbd3d20d0ebd4979
3
+ metadata.gz: a463d5fefac0a24c9690767a00ddc2d85bea2eacd0e4ac97f7e917b7b091e2ed
4
+ data.tar.gz: e4e1764191e3a48b77f5ff113520a832122c7127e05f020ea17d07217abc781b
5
5
  SHA512:
6
- metadata.gz: 839c1692bebb95a4f80129bd90c7dc8f514b884efa870665bc6c03a021f362422de4e56064bc237a3f1aa9608c81c431d8b6f2e049996f5dd530b733348b4faa
7
- data.tar.gz: c4dc80e885c700ad15acba992f9a4d85a9137bec2dc18a16847b21fd5f4223dbab9728b94094ad037022292a59a16192dc6a42608a066edd143bd5eb8adba2b4
6
+ metadata.gz: d888c1aa5df614564a0eb7dc31d3125e1fd01dd09125bfa5b003d7279f23a4f973a0c61bb872476a5a7203cceabc53437dacf62c13d461633cf0de38d2da9a58
7
+ data.tar.gz: 5c27cf932778fe055f4c1162f2efb8777c4aa08e705b47ca05151576325807e4fbfee2ea614781698496628560a143066e384a3612baf18c8af4ebf3d5395f78
data/CHANGELOG.md CHANGED
@@ -3,3 +3,7 @@
3
3
  ## [0.1.0] - 2023-02-26
4
4
 
5
5
  - Initial release
6
+
7
+ ## [0.1.1] - 2023-02-26
8
+
9
+ - Adding example usage in `README.md`
data/README.md CHANGED
@@ -20,9 +20,636 @@ Or install it yourself as:
20
20
 
21
21
  $ gem install rordash
22
22
 
23
- ## Usage
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/dev/setup.rb CHANGED
@@ -12,6 +12,7 @@ require 'active_support/core_ext/hash/keys'
12
12
  require 'active_support/core_ext/module/delegation'
13
13
  require 'active_support/core_ext/object/blank'
14
14
  require 'active_support/core_ext/enumerable'
15
+ require 'active_support/hash_with_indifferent_access'
15
16
  require 'mime-types'
16
17
  require 'faker'
17
18
  require 'oj'
@@ -0,0 +1,42 @@
1
+ module Rordash
2
+ class Chain
3
+ attr_reader :value
4
+
5
+ def initialize(value)
6
+ @value = value
7
+ end
8
+
9
+ def to_h
10
+ @value = HashUtil.from_string(@value)
11
+
12
+ self
13
+ end
14
+
15
+ def to_str
16
+ @value = HashUtil.to_str(@value)
17
+
18
+ self
19
+ end
20
+
21
+ def deep_compact
22
+ @value = HashUtil.deep_compact(@value)
23
+
24
+ self
25
+ end
26
+
27
+ def pick(paths)
28
+ @value = HashUtil.pick(@value, paths)
29
+ self
30
+ end
31
+
32
+ def merge(other)
33
+ @value = @value.merge(other)
34
+ self
35
+ end
36
+
37
+ def set(path, value)
38
+ HashUtil.set(@value, path, value)
39
+ self
40
+ end
41
+ end
42
+ end
@@ -51,7 +51,7 @@ module Rordash
51
51
  end
52
52
 
53
53
  def open_fixture_file(filename)
54
- pathname = Utils::FileUtil.fixture_file_path(filename)
54
+ pathname = fixture_file_path(filename)
55
55
  return nil unless pathname.exist?
56
56
 
57
57
  ::File.open(pathname.to_s)
@@ -127,7 +127,7 @@ module Rordash
127
127
  result = {}
128
128
 
129
129
  dot(attrs, keep_arrays: true) do |k, v|
130
- value = each_value_proc.respond_to?(:call) ? each_value_proc&.call(k, v) : v.compact
130
+ value = each_value_proc.respond_to?(:call) ? each_value_proc&.call(k, v) : v.try(:compact)
131
131
  next if value.nil?
132
132
 
133
133
  set(result, k, value)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Rordash
4
- VERSION = "0.1.0"
4
+ VERSION = "0.1.2"
5
5
  end
data/lib/rordash.rb CHANGED
@@ -10,9 +10,15 @@ file_util
10
10
  url_util
11
11
  object_util
12
12
  numeric_util
13
+ chain
13
14
  ].each do |filename|
14
15
  require File.expand_path("../rordash/#{filename}", Pathname.new(__FILE__).realpath)
15
16
  end
16
17
 
17
18
  module Rordash
18
- ; end
19
+ class << self
20
+ def chain(value)
21
+ Chain.new(value.dup)
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,3 @@
1
+ name,date,favorite_color
2
+ John Smith,Oct 2 1901,blue
3
+ Gemma Jones,Sept 1 2018,silver
@@ -0,0 +1,6 @@
1
+ [
2
+ {
3
+ "color": "red",
4
+ "value": "#f00"
5
+ }
6
+ ]
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rordash
4
+ RSpec.describe Chain do
5
+ let(:instance) { described_class.new(value) }
6
+
7
+ describe "#to_h" do
8
+ let(:value) { JSON.dump(a: 1) }
9
+
10
+ it "chains underlying hash util method" do
11
+ expect(instance.to_h.value).to eql(a: 1)
12
+ end
13
+ end
14
+
15
+ describe "#deep_compact" do
16
+ let(:value) { { a: { b: { c: nil } }, d: 1, e: [], f: [1, 2, nil, 3, nil] } }
17
+
18
+ it "chains underlying hash util method" do
19
+ expect(
20
+ instance.deep_compact.value
21
+ ).to eql(e: [], f: [1, 2, 3])
22
+ end
23
+ end
24
+ end
25
+ end
data/spec/rordash_spec.rb CHANGED
@@ -4,4 +4,14 @@ RSpec.describe Rordash do
4
4
  it "has a version number" do
5
5
  expect(Rordash::VERSION).not_to be_nil
6
6
  end
7
+
8
+ describe '.chain' do
9
+ describe "#to_h" do
10
+ let(:value) { JSON.dump(a: 1) }
11
+
12
+ it "chains underlying hash util method" do
13
+ expect(described_class.chain(JSON.dump(a: 1)).to_h.value).to eql(a: 1)
14
+ end
15
+ end
16
+ end
7
17
  end
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.0
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Desmond O'Leary
@@ -257,6 +257,7 @@ files:
257
257
  - bin/setup
258
258
  - dev/setup.rb
259
259
  - lib/rordash.rb
260
+ - lib/rordash/chain.rb
260
261
  - lib/rordash/debug_util.rb
261
262
  - lib/rordash/file_util.rb
262
263
  - lib/rordash/hash_util.rb
@@ -271,6 +272,9 @@ files:
271
272
  - spec/coverage_helper.rb
272
273
  - spec/fixtures/files/sample.csv
273
274
  - spec/fixtures/files/sample.json
275
+ - spec/fixtures/sample.csv
276
+ - spec/fixtures/sample.json
277
+ - spec/rordash/chain_spec.rb
274
278
  - spec/rordash/debug_util_spec.rb
275
279
  - spec/rordash/file_util_spec.rb
276
280
  - spec/rordash/hash_util_spec.rb
@@ -311,6 +315,9 @@ test_files:
311
315
  - spec/coverage_helper.rb
312
316
  - spec/fixtures/files/sample.csv
313
317
  - spec/fixtures/files/sample.json
318
+ - spec/fixtures/sample.csv
319
+ - spec/fixtures/sample.json
320
+ - spec/rordash/chain_spec.rb
314
321
  - spec/rordash/debug_util_spec.rb
315
322
  - spec/rordash/file_util_spec.rb
316
323
  - spec/rordash/hash_util_spec.rb