nice_hash 1.12.4 → 1.12.5

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.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +18 -0
  3. data/lib/nice_hash.rb +66 -0
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 167a8424743b40b5de4d966a6a552051390cd5d44f775b52622046e5af949ecf
4
- data.tar.gz: f85151c49c2fd93f745d3738b9845afe9e0bc9f4f5fa9340b0ba07858983ee66
3
+ metadata.gz: 94d7070c81a81872cb9e5d24575edfd64415c928e73c771d83c63c271febe1cd
4
+ data.tar.gz: ff0196fe8a9e5430a21d7eb1e9384af29294b415b8345ad92b659070b18497f6
5
5
  SHA512:
6
- metadata.gz: 1b7264f0c6f777a2d613660abd284d1e5086f4fb1ed8c8717f74a2a8fa5e8e02c6a1da6581c15feb65b3972fdca617a33a80fe7718b750ba42df70a276f37c73
7
- data.tar.gz: ebb300beff36bd7774532d185d28603045e9bf426d38612b00448e608c464c52232825cb22c115af846332edb577de028c09b9899ad960bf102c744f7e1bd1e2
6
+ metadata.gz: 59455491dea59f70d6b27662db5dcda9a3d568ee39d50d6d4380460a1ee9b0e930de86c31e8073817793b1b9bf03748a95e87e89b67e7df8eaef103c17b2f236
7
+ data.tar.gz: '095c46c7482a207e942c22a7952988943477c76a1488b1a75a2604edf6f03dfb2f1e82f80cc775013d34509c9b42261b216ff2df64ecd2647330f0eac2879279'
data/README.md CHANGED
@@ -749,6 +749,24 @@ p my_hash
749
749
  #>{:one=>1, :two=>2, :three=>{:car=>"changed"}}
750
750
  ```
751
751
 
752
+ If you want to delete a key on a nested hash you can use `delete_nested` and supply the key you want:
753
+
754
+ ```ruby
755
+ my_hash = { user: {
756
+ address: {
757
+ city: 'Madrid',
758
+ country: 'Spain'
759
+ },
760
+ name: 'Peter',
761
+ age: 33
762
+ },
763
+ customer: true
764
+ }
765
+ NiceHash.delete_nested(my_hash, 'user.address.city')
766
+ #>{:user=>{:address=>{:country=>"Spain"}, :name=>"Peter", :age=>33}, :customer=>true}
767
+
768
+ ```
769
+
752
770
  ## Contributing
753
771
 
754
772
  Bug reports and pull requests are welcome on GitHub at https://github.com/marioruiz/nice_hash.
data/lib/nice_hash.rb CHANGED
@@ -787,4 +787,70 @@ class NiceHash
787
787
  end
788
788
  return true
789
789
  end
790
+
791
+ ##################################################
792
+ # Translate a hash of hashes into a string separted by .
793
+ #
794
+ # @param hash [Hash] The hash we want to translate
795
+ #
796
+ # @return [String]
797
+ #
798
+ # @example
799
+ # my_hash = { uno: {dos: :tres} }
800
+ # NiceHash.transtring(my_hash)
801
+ # #>"uno.dos.tres"
802
+ ##################################################
803
+ def self.transtring(hash)
804
+ keys = []
805
+ if hash.is_a?(Hash)
806
+ hash.each do |k,v|
807
+ if v.is_a?(Hash)
808
+ keys << k
809
+ keys << trans(v)
810
+ else
811
+ keys << k
812
+ keys << v
813
+ end
814
+ end
815
+ else
816
+ keys << hash
817
+ end
818
+ return keys.join(".")
819
+ end
820
+
821
+ ##################################################
822
+ # Deletes the supplied key
823
+ #
824
+ # @param hash [Hash] The hash we want
825
+ # @param nested_key [Hash] [String] String with the nested key: 'uno.dos.tres' or a hash { uno: {dos: :tres} }
826
+ #
827
+ # @return [Hash]
828
+ #
829
+ # @example
830
+ # my_hash = { user: {
831
+ # address: {
832
+ # city: 'Madrid',
833
+ # country: 'Spain'
834
+ # },
835
+ # name: 'Peter',
836
+ # age: 33
837
+ # },
838
+ # customer: true
839
+ # }
840
+ # NiceHash.delete_nested(my_hash, 'user.address.city')
841
+ # #>{:user=>{:address=>{:country=>"Spain"}, :name=>"Peter", :age=>33}, :customer=>true}
842
+ ##################################################
843
+ def self.delete_nested(hash, nested_key)
844
+ nested_key = transtring(nested_key)
845
+ keys = nested_key.split('.')
846
+ if keys.size == 1
847
+ hash.delete(nested_key.to_sym)
848
+ else
849
+ last_key = keys[-1]
850
+ eval("hash.#{keys[0..(keys.size-2)].join(".")}.delete(:#{last_key})")
851
+ end
852
+ return hash
853
+ end
854
+
855
+
790
856
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nice_hash
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.12.4
4
+ version: 1.12.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mario Ruiz
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-04-12 00:00:00.000000000 Z
11
+ date: 2019-05-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: string_pattern