coaster 1.3.17 → 1.3.18

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: ab2eb8291386db2096f9972b5094c8377337cf1ba33741222dc748959e9eb6c8
4
- data.tar.gz: 599d8e3771f71618d5cd93b111bc04b68b61cd632bedb212e8070d61758462ae
3
+ metadata.gz: 65e15c20364ddf5904a27bacf4444b398fc3c1546358d1dea7bc528b3b3d8984
4
+ data.tar.gz: f404e8e526232d2d1989cf34599b448fab05f85b01be2d71a841fd75510c1369
5
5
  SHA512:
6
- metadata.gz: 6700f6a695c28ca3d4eda360ccf651a5a35aa30568ad38f41e0e0992aeb3edd9cee94d23beab7e1f79e03f378553a7b09efbc278ce7090511dee9c046394e916
7
- data.tar.gz: da224933981d1593c1ddf6318829ca63a6610dcf75535bd49a3c043a32ef604a074292c50da53e8ff5790e690ddc851bf6a9ea3e251ee2afcb6e0b3308b52be4
6
+ metadata.gz: 6c5a2801171bb3e725a28763efdeb9585f4200d9ae651c167efe3efca7b1840417853d7ac684ac7f52f28f537ddf53b6309798715aed42da106e542b4f715800
7
+ data.tar.gz: 464be9d5a8c7af75fc8f005645203c4b7c19ffd236ecb27431bf8ae9653df2de3ad58650c93087139baf8a91322e5d14d7e386f06fd1d7507cc12527e565e663
data/lib/coaster/util.rb CHANGED
@@ -1,18 +1,19 @@
1
1
  module Coaster
2
2
  module Util
3
3
  FLATTEN_HASH_DELIMITER = '.'.freeze
4
+ ARRAY_START = 0
4
5
 
5
6
  class << self
6
- def flatten_hashify(object, delimiter: FLATTEN_HASH_DELIMITER, breadcrumbs: [], include_array: nil)
7
+ def flatten_hashify(object, delimiter: FLATTEN_HASH_DELIMITER, breadcrumbs: [], array_start: nil)
7
8
  case object
8
9
  when Hash
9
10
  object.each_with_object({}) do |(key, value), memo|
10
- memo.merge!(flatten_hashify(value, delimiter: delimiter, breadcrumbs: breadcrumbs + [key], include_array: include_array))
11
+ memo.merge!(flatten_hashify(value, delimiter: delimiter, breadcrumbs: breadcrumbs + [key], array_start: array_start))
11
12
  end
12
13
  when Array
13
- if include_array
14
- object.each.with_index(1).with_object({}) do |(element, ix), memo|
15
- memo.merge!(flatten_hashify(element, delimiter: delimiter, breadcrumbs: breadcrumbs + [ix], include_array: include_array))
14
+ if array_start
15
+ object.each.with_index(array_start).with_object({}) do |(element, ix), memo|
16
+ memo.merge!(flatten_hashify(element, delimiter: delimiter, breadcrumbs: breadcrumbs + [ix], array_start: array_start))
16
17
  end
17
18
  else
18
19
  {breadcrumbs.join(delimiter) => object}
@@ -21,6 +22,39 @@ module Coaster
21
22
  {breadcrumbs.join(delimiter) => object}
22
23
  end
23
24
  end
25
+
26
+ def roughen_hashify(object, delimiter: FLATTEN_HASH_DELIMITER, array_start: ARRAY_START)
27
+ step1 = object.each_with_object({}) do |(key, value), memo|
28
+ sp_keys = key.split(delimiter)
29
+ k = sp_keys.shift
30
+ if sp_keys.present?
31
+ memo[k] ||= {}
32
+ memo[k][sp_keys.join(delimiter)] = value
33
+ else
34
+ memo[k] = value
35
+ end
36
+ end
37
+
38
+ step2 = step1.each_with_object({}) do |(key, value), memo|
39
+ case value
40
+ when Hash
41
+ memo[key] = roughen_hashify(value, delimiter: delimiter, array_start: array_start)
42
+ else
43
+ memo[key] = value
44
+ end
45
+ end
46
+
47
+ array_keys = (array_start...(step2.keys.size+array_start)).to_a
48
+ if array_keys == step2.keys.map(&:to_i).sort
49
+ step3 = []
50
+ array_keys.map(&:to_s).each do |k|
51
+ step3 << step2[k]
52
+ end
53
+ step3
54
+ else
55
+ step2
56
+ end
57
+ end
24
58
  end
25
59
  end
26
60
  end
@@ -1,3 +1,3 @@
1
1
  module Coaster
2
- VERSION = '1.3.17'
2
+ VERSION = '1.3.18'
3
3
  end
data/test/test_util.rb CHANGED
@@ -11,19 +11,31 @@ module Coaster
11
11
  end
12
12
 
13
13
  def test_flatten_hashify
14
- res = Util.flatten_hashify({a: [1], b: {aa: 1, bb: [2, 1]}})
14
+ a = {'a'=>[1], 'b'=>{'aa'=>1, 'bb'=>[2, 1]}}
15
+ res = Util.flatten_hashify(a)
15
16
  assert_equal({"a"=>[1], "b.aa"=>1, "b.bb"=>[2, 1]}, res)
16
- res = Util.flatten_hashify({a: [1], b: {aa: 1, bb: [2, 1]}}, include_array: true)
17
+ b = Util.roughen_hashify(res)
18
+ assert_equal(a, b)
19
+ res = Util.flatten_hashify(a, array_start: 1)
17
20
  assert_equal({"a.1"=>1, "b.aa"=>1, "b.bb.1"=>2, "b.bb.2"=>1}, res)
21
+ b = Util.roughen_hashify(res, array_start: 1)
22
+ assert_equal(a, b)
23
+ res = Util.flatten_hashify(a, array_start: 0)
24
+ assert_equal({"a.0"=>1, "b.aa"=>1, "b.bb.0"=>2, "b.bb.1"=>1}, res)
25
+ b = Util.roughen_hashify(res)
26
+ assert_equal(a, b)
18
27
  res = Util.flatten_hashify([{a: 1}, 22, [33, 44], {b: {c: [33, {d: 4}]}}])
19
28
  assert_equal({""=>[{:a=>1}, 22, [33, 44], {:b=>{:c=>[33, {:d=>4}]}}]}, res) # no meaning result
20
- res = Util.flatten_hashify([{a: 1}, 22, [33, 44], {b: {c: [33, {d: 4}]}}], include_array: true)
29
+ res = Util.flatten_hashify([{a: 1}, 22, [33, 44], {b: {c: [33, {d: 4}]}}], array_start: 1)
21
30
  assert_equal({"1.a"=>1, "2"=>22, "3.1"=>33, "3.2"=>44, "4.b.c.1"=>33, "4.b.c.2.d"=>4}, res)
22
31
  end
23
32
 
24
33
  def test_flatten_hashify_delimiter
25
- res = Util.flatten_hashify([{a: 1}, 22, [33, 44], {b: {c: [33, {d: 4}]}}], include_array: true, delimiter: '/')
34
+ a = [{'a'=>1}, 22, [33, 44], {'b'=>{'c'=>[33, {'d'=>4}]}}]
35
+ res = Util.flatten_hashify(a, array_start: 1, delimiter: '/')
26
36
  assert_equal({"1/a"=>1, "2"=>22, "3/1"=>33, "3/2"=>44, "4/b/c/1"=>33, "4/b/c/2/d"=>4}, res)
37
+ b = Util.roughen_hashify(res, array_start: 1, delimiter: '/')
38
+ assert_equal(a, b)
27
39
  end
28
40
  end
29
41
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: coaster
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.17
4
+ version: 1.3.18
5
5
  platform: ruby
6
6
  authors:
7
7
  - buzz jung
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-08-10 00:00:00.000000000 Z
11
+ date: 2022-09-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: i18n