flattenator 0.1.0 → 0.1.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a7dbb3d8989a54d268ebf7b3a0a5bde6b93a8c13dca8a43bc5b710dc506690f2
4
- data.tar.gz: dd57d11d80449c646741ac4c11beb80ce289d967b845ee692595ac191e2eb361
3
+ metadata.gz: 3b92cd0d6ef19e1f44533d54ad8a681903cba243a137ba11c5cb860c9abcf2f1
4
+ data.tar.gz: 81dc044b46d5b9509e2221e4bcd8ed8ab1eff15a2a187bbbf951590dda048801
5
5
  SHA512:
6
- metadata.gz: 065f92ab7f72cf509742a2e6760169b93a7d06133f7cda6a47e1aa3d3079e581f721e8abaf013384cbfede9d89f41838e4f075dfc926989da7b306edb1d39bbe
7
- data.tar.gz: 4d25b5c9f671780d63cde1f5cd3fcd68118cd3dc71bfa6ff07fee55e0c2ff08a8df0b10b0528c387065452901a51f40c7fffa63146e42a11220a23655c61b302
6
+ metadata.gz: 7192f5c026ee85154cb5c7ac7254a9519f91a79395f6ac65d8a26f1b11401e4afebe61431db5f513eae8a5722c389930068bd15142fe3091e025d08b7e52eb62
7
+ data.tar.gz: f062533cc3e3e351418116aedeb63003eceee5cbef1cb4e4e9f116135a3acc93c032009ee5a4146344c321d448dd5e8e1a71410d4aef88d5f2ea0d0ee1fbe86e
data/CHANGELOG.markdown CHANGED
@@ -1,3 +1,7 @@
1
+ # v0.1.1
2
+
3
+ * Fix that `Flattenator::Hash` wasn't available by default when adding the gem to your project.
4
+
1
5
  # v0.1.0
2
6
 
3
- Initial commit
7
+ * Initial commit
data/README.markdown CHANGED
@@ -14,10 +14,10 @@ Converts a deeply nested hash with a flattened hash, prefixing the keys for chil
14
14
  }
15
15
  # end with this
16
16
  {
17
- foo: "bar",
18
- baz_apple: 2,
19
- baz_banana: 5,
20
- baz_carrot: 0,
17
+ "foo" => "bar",
18
+ "baz_apple" => 2,
19
+ "baz_banana" => 5,
20
+ "baz_carrot" => 0,
21
21
  }
22
22
  ```
23
23
 
@@ -40,7 +40,61 @@ Or install it yourself as:
40
40
  ## Usage
41
41
 
42
42
  ```
43
- Flattenator.new(hash).flattened
43
+ Flattenator::Hash.new(hash).flattened
44
+ ```
45
+
46
+ By default, a JSON-encoded version of the object being flattened will be included in the flattened hash, but this can be turned off.
47
+
48
+ ```ruby
49
+ original = {
50
+ foo: "bar",
51
+ options: {
52
+ jiggle_handle: true,
53
+ tighten_valve: false,
54
+ },
55
+ }
56
+
57
+ Flattenator::Hash.new(original).flattened
58
+ {
59
+ "foo" => "bar",
60
+ "options" => "{\"jiggle_handle\":true,\"tighten_valve\":false}"
61
+ "options_jiggle_handle" => true,
62
+ "options_tighten_valve" => false,
63
+ }
64
+
65
+ Flattenator::Hash.new(original, include_unflattened: false).flattened
66
+ {
67
+ "foo" => "bar",
68
+ "options_jiggle_handle" => true,
69
+ "options_tighten_valve" => false,
70
+ }
71
+ ```
72
+
73
+ Additionally, arrays are flattened into the form `{key}_{index}`, like so:
74
+
75
+ ```ruby
76
+ original = {
77
+ fruits: [
78
+ "apple",
79
+ "banana",
80
+ "citrus",
81
+ ],
82
+ vegetables: [
83
+ "daikon",
84
+ "eggplant",
85
+ ],
86
+ }
87
+
88
+ Flattenator::Hash.new(original).flattened
89
+ {
90
+ "fruits" => "[\"apple\",\"banana\",\"citrus\"]",
91
+ "fruits_0" => "apple",
92
+ "fruits_1" => "banana",
93
+ "fruits_2" => "citrus",
94
+ "vegetables" => "[\"daikon\",\"eggplant\"]",
95
+ "vegetables_0" => "daikon",
96
+ "vegetables_1" => "eggplant",
97
+ }
44
98
  ```
45
99
 
46
100
  ## Development
data/lib/flattenator.rb CHANGED
@@ -1,6 +1,6 @@
1
+ require "flattenator/hash"
1
2
  require "flattenator/version"
2
3
 
3
4
  module Flattenator
4
- class Error < StandardError; end
5
- # Your code goes here...
5
+ Error = Class.new(StandardError)
6
6
  end
@@ -0,0 +1,43 @@
1
+ require "json"
2
+
3
+ class Flattenator::Hash
4
+ attr_reader *%i(
5
+ flattened
6
+ source
7
+ )
8
+
9
+ def initialize(source, include_unflattened: true)
10
+ raise ArgumentError.new("Source object does not respond to each_pair") unless source.respond_to?(:each_pair)
11
+
12
+ @source = source
13
+ @flattened = {}
14
+ @include_unflattened = !!include_unflattened
15
+
16
+ source.each_pair do |key, value|
17
+ flatten(key: key, value: value, destination: @flattened)
18
+ end
19
+ end
20
+
21
+ def include_unflattened?
22
+ @include_unflattened
23
+ end
24
+
25
+ private def flatten(key:, value:, destination:, prefix: nil)
26
+ fullkey = [prefix, key].compact.join("_").tr("-", "_")
27
+
28
+ case value
29
+ when Hash
30
+ destination[fullkey] = JSON.dump(value) if include_unflattened?
31
+ value.each do |k, v|
32
+ flatten(key: k, value: v, destination: destination, prefix: fullkey)
33
+ end
34
+ when Array
35
+ destination[fullkey] = JSON.dump(value) if include_unflattened?
36
+ value.each_with_index do |item, i|
37
+ destination["#{fullkey}_#{i}"] = item
38
+ end
39
+ else
40
+ destination[fullkey] = value
41
+ end
42
+ end
43
+ end
@@ -1,3 +1,3 @@
1
1
  module Flattenator
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: flattenator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Patrick Byrne
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-02-25 00:00:00.000000000 Z
11
+ date: 2020-02-26 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email:
@@ -19,7 +19,6 @@ extra_rdoc_files: []
19
19
  files:
20
20
  - ".gitignore"
21
21
  - ".semaphore/semaphore.yml"
22
- - ".travis.yml"
23
22
  - CHANGELOG.markdown
24
23
  - CODE_OF_CONDUCT.md
25
24
  - Gemfile
@@ -30,6 +29,7 @@ files:
30
29
  - bin/setup
31
30
  - flattenator.gemspec
32
31
  - lib/flattenator.rb
32
+ - lib/flattenator/hash.rb
33
33
  - lib/flattenator/version.rb
34
34
  homepage: https://github.com/pbyrne/flattenator
35
35
  licenses:
data/.travis.yml DELETED
@@ -1,6 +0,0 @@
1
- ---
2
- language: ruby
3
- cache: bundler
4
- rvm:
5
- - 2.6.5
6
- before_install: gem install bundler -v 2.1.4