hash_flatten 0.3.0 → 1.0.0

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
  SHA1:
3
- metadata.gz: 73bc14c6b41e2943e5c7a9e244db24681324a060
4
- data.tar.gz: 7c89f6bc9f49d95098fa37ef0bf88e15fb54a600
3
+ metadata.gz: 83fff9dffa4f78c3437e291b31519ff7035a2665
4
+ data.tar.gz: 01c61a4e9a3c2877c9593064bb55ecf616bbc415
5
5
  SHA512:
6
- metadata.gz: 1a7e487f9d2467a51c247c88f040b4099f613f55ce1df312329bb2b02a81665c283d3a3bbfc3ef2693fc0f29c3ac145518086fa5febd70d1aea5b458548cc3cb
7
- data.tar.gz: 3c750e9ecdb551ad7e5c0a4f2981446722be93960b9b937c817bbb38cea970811babbbb875d745cd915ee3538ce8aa9dd64cdc9c1ed9b547f2f98319afca8abb
6
+ metadata.gz: e607f013258e812f6106a008d0fe5075ac66dd862b35dd3bbfb1151b8c2a930cfcba3dec2970cc2a6c48e4200fa30504667ce7399c61dce0d65c87dd2964d9ea
7
+ data.tar.gz: 64b47cfbcef60c76936051c5ae8afb2eb15778fb7f9317851e2a57ab26dab44603235f94fea6edd7b0a0c7229bcefe02b7952bba3854990aab5993342f495a72
@@ -1,5 +1,4 @@
1
- sudo: false
2
1
  language: ruby
3
2
  rvm:
4
3
  - 2.3.1
5
- before_install: gem install bundler -v 1.15.4
4
+ - 2.5
data/Gemfile CHANGED
@@ -1,6 +1,4 @@
1
- source "https://rubygems.org"
2
-
3
- git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
1
+ source 'https://rubygems.org'
4
2
 
5
3
  # Specify your gem's dependencies in hash_flatten.gemspec
6
4
  gemspec
data/README.md CHANGED
@@ -1,32 +1,24 @@
1
- # HashFlatten
1
+ # HashFlatten [![Build Status](https://travis-ci.org/Masa331/hash_flatten.svg?branch=master)](https://travis-ci.org/Masa331/hash_flatten)
2
2
 
3
- Two methods i sometimes miss on Hash.
3
+ Flatten nested Hash to one level depth with keys joined with a dot and vice versa.
4
4
 
5
- ## `#destructure`
5
+ Content:
6
+ 1. [About](#about)
7
+ 2. [How to](#how-to)
8
+ 1. [Installation](#installation)
9
+ 2. [Usage](#usage)
10
+ 3. [squish_levels](#squish_levels)
11
+ 4. [stretch_to_levels](#stretch_to_levels)
12
+ 5. [Notes](#notes)
13
+ 3. [License](#license)
6
14
 
7
- hash = { a: 'a',
8
- b: { a: 'b' },
9
- c: { b: { a: 'c' } } }
10
-
11
- hash.destructure =>
12
-
13
- { 'a' => 'a',
14
- 'b.a' => 'b',
15
- 'c.b.a' => 'c' }
16
-
17
- ## `#structure`
18
-
19
- hash = { 'a' => 'a',
20
- 'b.a' => 'b',
21
- 'c.b.a' => 'c' }
15
+ ## About
22
16
 
23
- hash.structure =>
17
+ This gem includes Refinement which adds `#squish_levels` and `#stretch_to_levels` to Hash. With them you can transform this hash: `{ a: { b: 'b', c: 'c' } }` to this: `{ 'a.b' => 'b', 'a.c' => 'c' }` and vice versa. I use these to transform Rails locales to somehow readable csv for external translators or showing XML structure(transformed to a hash) in a table.
24
18
 
25
- { 'a' => 'a',
26
- 'b' => { 'a' => 'b' },
27
- 'c' => { 'b' => { 'a' => 'c' } } }
19
+ ## How to
28
20
 
29
- ## Installation
21
+ ### Installation
30
22
 
31
23
  Add this line to your application's Gemfile:
32
24
 
@@ -42,17 +34,52 @@ Or install it yourself as:
42
34
 
43
35
  $ gem install hash_flatten
44
36
 
45
- ## Usage
37
+ ### Usage
46
38
 
47
- Use as refinement:
39
+ Use as a Refinement:
48
40
  ```
49
41
  class MyClass
50
42
  using HashFlatten
51
43
  end
52
44
  ```
53
- now you can call `#destructure` od `#structure` on hashes in `MyClass`.
45
+ now you can call `#squish_levels` and `#stretch_to_levels` on hashes in `MyClass`(lexically).
46
+
47
+ Here is an excellent blog post on Refinements if you didn't use them before: http://interblah.net/why-is-nobody-using-refinements
48
+
49
+ ### `#squish_levels`
50
+
51
+ ```ruby
52
+ hash = { a: 'a',
53
+ b: { a: 'b' },
54
+ c: { b: { a: 'c' } } }
55
+
56
+ hash.squish_levels =>
57
+
58
+ { 'a' => 'a',
59
+ 'b.a' => 'b',
60
+ 'c.b.a' => 'c' }
61
+ ```
62
+
63
+ ### `#stretch_to_levels`
64
+
65
+ ```ruby
66
+ hash = { 'a' => 'a',
67
+ 'b.a' => 'b',
68
+ 'c.b.a' => 'c' }
69
+
70
+ hash.stretch_to_levels =>
71
+
72
+ { 'a' => 'a',
73
+ 'b' => { 'a' => 'b' },
74
+ 'c' => { 'b' => { 'a' => 'c' } } }
75
+ ```
76
+
77
+ ### Notes
78
+
79
+ Some things to know:
54
80
 
55
- Here is an excellent blog post on refinements if you didn't use them before: http://interblah.net/why-is-nobody-using-refinements
81
+ * whenever you call `#squish_levels` or `#stretch_to_levels` returned hash has keys transformed to strings
82
+ * squishes or stretches only sub Hashes, other hash-like structure aren't touched(`#to_h` isn't called on values..)
56
83
 
57
84
  ## License
58
85
 
@@ -4,19 +4,18 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = 'hash_flatten'
7
- spec.version = '0.3.0'
7
+ spec.version = '1.0.0'
8
8
  spec.authors = ['Premysl Donat']
9
9
  spec.email = ['pdonat@seznam.cz']
10
10
 
11
- spec.summary = 'Two method i miss in std lib'
12
- spec.description = spec.summary
11
+ spec.summary = 'Flatten nested Hash to one level depth with keys joined with a dot.'
12
+ spec.description = 'A refinement which includes a method for flattening nested Hash to one level depth with keys joined with a dot. Also includes a method for reverse operation.'
13
13
  spec.homepage = 'https://github.com/Masa331/hash_flatten'
14
14
  spec.license = 'MIT'
15
15
 
16
16
  spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
17
  f.match(%r{^(test|spec|features)/})
18
18
  end
19
- spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
19
  spec.require_paths = ['lib']
21
20
 
22
21
  spec.add_development_dependency 'bundler'
@@ -1,48 +1,29 @@
1
1
  module HashFlatten
2
2
  refine Hash do
3
- def destructure
4
- flattened = each_with_object({}) do |(k, v), n|
5
- if v.is_a? Hash
6
- v.each do |k2, v2|
7
- n["#{k}.#{k2}"] = v2
8
- end
3
+ def squish_levels
4
+ each_with_object({}) do |(key, value), squished|
5
+ if value.is_a? Hash
6
+ value.squish_levels.each { |sub_key, sub_value| squished.store("#{key}.#{sub_key}", sub_value) }
9
7
  else
10
- n[k.to_s] = v
8
+ squished.store(key.to_s, value)
11
9
  end
12
10
  end
13
-
14
- if flattened.any? { |_, v| v.is_a? Hash }
15
- flattened.destructure
16
- else
17
- flattened
18
- end
19
11
  end
20
12
 
21
- def structure
22
- new_hash = decompose_keys
13
+ def stretch_to_levels
14
+ each_with_object({}) do |(key, value), stretched|
15
+ key_parts = key.to_s.split('.')
23
16
 
24
- new_hash.each do |k, v|
25
- if v.is_a? Hash
26
- new_value = v.structure
17
+ if key_parts.size > 1
18
+ level_key = key_parts.shift
19
+ tail = key_parts.join('.')
27
20
 
28
- new_hash[k] = new_value
29
- end
30
- end
31
- end
32
-
33
- def decompose_keys
34
- each_with_object({}) do |(k, v), new_hash|
35
- key_parts = k.split('.')
36
- top_level_key = key_parts.shift
37
- tail = key_parts.join('.')
21
+ existing_content = stretched.fetch(level_key, {})
22
+ new_content = existing_content.merge({ tail => value }).stretch_to_levels
38
23
 
39
- if tail.empty?
40
- new_hash[top_level_key] = v
24
+ stretched.store(level_key, new_content)
41
25
  else
42
- existing_value = new_hash.fetch(top_level_key, {})
43
- new_value = existing_value.merge({ tail => v })
44
-
45
- new_hash[top_level_key] = new_value
26
+ stretched.store(key.to_s, value)
46
27
  end
47
28
  end
48
29
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hash_flatten
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Premysl Donat
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-02-08 00:00:00.000000000 Z
11
+ date: 2018-03-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -66,7 +66,8 @@ dependencies:
66
66
  - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
- description: Two method i miss in std lib
69
+ description: A refinement which includes a method for flattening nested Hash to one
70
+ level depth with keys joined with a dot. Also includes a method for reverse operation.
70
71
  email:
71
72
  - pdonat@seznam.cz
72
73
  executables: []
@@ -107,5 +108,5 @@ rubyforge_project:
107
108
  rubygems_version: 2.6.14
108
109
  signing_key:
109
110
  specification_version: 4
110
- summary: Two method i miss in std lib
111
+ summary: Flatten nested Hash to one level depth with keys joined with a dot.
111
112
  test_files: []