hash_kit 0.6.0 → 0.8.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
  SHA256:
3
- metadata.gz: 491612690cb2ff1c4cd444156af5baf82b1b73eb75be34895fcb01a0008d82e2
4
- data.tar.gz: 06313acec33394404f6287ae85f28d50db341f76e964c5d4e43ac4aad8a8652c
3
+ metadata.gz: 800ee63416294dcbb04c587e92fa18c429158142d879eab17605f7670a049312
4
+ data.tar.gz: c05a36904c4ce5b25b697d39d7f8b9c4b1a48aa0f58774f285d47868db8fb472
5
5
  SHA512:
6
- metadata.gz: cc2f86c26fcf44dbc9ebc252fadeee486f3eb77155be9b367cbd0fbcb296408f3279f84ebc86dae0f926d6c939d7a085ebade5884c5098974e3498a2e3d4bc9a
7
- data.tar.gz: 0e0bae5da75b25ab8790c72df2a535efcd4e8670f05378e3889429137bfda67de5c88ec1836e92673de16aedc8785c2878f8394c27c6776182cc98a710ec1798
6
+ metadata.gz: cc70b56602354b5a11ea2911b6850d294dccfac134adfdbe3434069504cc9f8c986409ba6504e7c0075a9a16b5c83df648a24747b3c75e5e929b948b71889820
7
+ data.tar.gz: bf0295c52e8511367f0dd4c380dcebdf5194005268804599d83c1f426b6251f1ff23d2b35aba83ab2891430038b68c95b19e7ff9e0435dde89bb6d92d190b7ff
@@ -1,31 +1,38 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module HashKit
4
+ # Hash kit Helper class
2
5
  class Helper
6
+ INDIFFERENT_PROC = proc do |h, k|
7
+ case k
8
+ when Symbol
9
+ # Symbol#name returns a frozen string without allocating (Ruby 3.0+)
10
+ str = k.name
11
+ h[str] if h.key?(str)
12
+ when String
13
+ sym = k.to_sym
14
+ h[sym] if h.key?(sym)
15
+ else
16
+ h[k.to_s]
17
+ end
18
+ end
3
19
 
4
- #This method is called to make a hash allow indifferent access (it will accept both strings & symbols for a valid key).
20
+ # This method is called to make a hash allow indifferent access (it will
21
+ # accept both strings & symbols for a valid key).
5
22
  def indifferent!(hash)
6
- unless hash.is_a?(Hash)
7
- return
8
- end
23
+ return unless hash.is_a?(Hash)
9
24
 
10
- #set the default proc to allow the key to be either string or symbol if a matching key is found.
11
- hash.default_proc = proc do |h, k|
12
- if h.key?(k.to_s)
13
- h[k.to_s]
14
- elsif h.key?(k.to_sym)
15
- h[k.to_sym]
16
- else
17
- nil
18
- end
19
- end
25
+ # Set the default proc to allow the key to be either string or symbol if
26
+ # a matching key is found.
27
+ hash.default_proc = INDIFFERENT_PROC
20
28
 
21
- #recursively process any child hashes
22
- hash.each do |key,value|
23
- if hash[key] != nil
24
- if hash[key].is_a?(Hash)
25
- indifferent!(hash[key])
26
- elsif hash[key].is_a?(Array)
27
- indifferent_array!(hash[key])
28
- end
29
+ # Recursively process any child hashes
30
+ hash.each_value do |value|
31
+ case value
32
+ when Hash
33
+ indifferent!(value)
34
+ when Array
35
+ indifferent_array!(value)
29
36
  end
30
37
  end
31
38
 
@@ -33,27 +40,28 @@ module HashKit
33
40
  end
34
41
 
35
42
  def indifferent_array!(array)
36
- unless array.is_a?(Array)
37
- return
38
- end
43
+ return unless array.is_a?(Array)
39
44
 
40
45
  array.each do |i|
41
- if i.is_a?(Hash)
46
+ case i
47
+ when Hash
42
48
  indifferent!(i)
43
- elsif i.is_a?(Array)
49
+ when Array
44
50
  indifferent_array!(i)
45
51
  end
46
52
  end
47
53
  end
48
54
 
49
- #This method is called to convert all the keys of a hash into symbols to allow consistent usage of hashes within your Ruby application.
55
+ # This method is called to convert all the keys of a hash into symbols to
56
+ # allow consistent usage of hashes within your Ruby application.
50
57
  def symbolize(hash)
51
58
  {}.tap do |h|
52
59
  hash.each { |key, value| h[key.to_sym] = map_value_symbol(value) }
53
60
  end
54
61
  end
55
62
 
56
- #This method is called to convert all the keys of a hash into strings to allow consistent usage of hashes within your Ruby application.
63
+ # This method is called to convert all the keys of a hash into strings to
64
+ # allow consistent usage of hashes within your Ruby application.
57
65
  def stringify(hash)
58
66
  {}.tap do |h|
59
67
  hash.each { |key, value| h[key.to_s] = map_value_string(value) }
@@ -73,18 +81,22 @@ module HashKit
73
81
  hash
74
82
  end
75
83
 
76
- def from_hash(hash, klass, transforms = [])
84
+ # Return an object of type klass from the values in the given hash
85
+ #
86
+ # @param [Hash] hash
87
+ # @param [Class] klass
88
+ # @param [Array] transforms
89
+ # @return [Object]
90
+ def from_hash(hash, klass, transforms = [])
77
91
  obj = klass.new
78
- if hash ==nil || hash == {}
79
- return obj
80
- end
92
+ return obj if hash.nil? || hash.empty?
93
+ raise ArgumentError, "#{hash.inspect} is not a hash" unless hash.is_a?(Hash)
94
+
95
+ hash.each do |k, v|
96
+ next unless obj.respond_to?(k)
81
97
 
82
- hash.each do |k,v|
83
- if !obj.respond_to?(k)
84
- next
85
- end
86
98
  transform = transforms.detect { |t| t.key.to_sym == k.to_sym }
87
- if transform != nil
99
+ if !transform.nil?
88
100
  if v.is_a?(Hash)
89
101
  child = from_hash(v, transform.klass, transforms)
90
102
  obj.instance_variable_set("@#{k}", child)
@@ -98,7 +110,8 @@ module HashKit
98
110
  obj.instance_variable_set("@#{k}", v)
99
111
  end
100
112
  end
101
- return obj
113
+
114
+ obj
102
115
  end
103
116
 
104
117
  private
@@ -0,0 +1,5 @@
1
+ # Namespace
2
+ module HashKit
3
+ # :nodoc:
4
+ VERSION = '0.8.0'
5
+ end
metadata CHANGED
@@ -1,59 +1,87 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hash_kit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sage One
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-02-27 00:00:00.000000000 Z
11
+ date: 2026-02-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: bundler
14
+ name: benchmark-ips
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '1.11'
19
+ version: '2.0'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '1.11'
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
27
55
  - !ruby/object:Gem::Dependency
28
56
  name: rake
29
57
  requirement: !ruby/object:Gem::Requirement
30
58
  requirements:
31
- - - "~>"
59
+ - - ">="
32
60
  - !ruby/object:Gem::Version
33
- version: '10.0'
61
+ version: '0'
34
62
  type: :development
35
63
  prerelease: false
36
64
  version_requirements: !ruby/object:Gem::Requirement
37
65
  requirements:
38
- - - "~>"
66
+ - - ">="
39
67
  - !ruby/object:Gem::Version
40
- version: '10.0'
68
+ version: '0'
41
69
  - !ruby/object:Gem::Dependency
42
70
  name: rspec
43
71
  requirement: !ruby/object:Gem::Requirement
44
72
  requirements:
45
- - - "~>"
73
+ - - ">="
46
74
  - !ruby/object:Gem::Version
47
- version: '3.0'
75
+ version: '0'
48
76
  type: :development
49
77
  prerelease: false
50
78
  version_requirements: !ruby/object:Gem::Requirement
51
79
  requirements:
52
- - - "~>"
80
+ - - ">="
53
81
  - !ruby/object:Gem::Version
54
- version: '3.0'
82
+ version: '0'
55
83
  - !ruby/object:Gem::Dependency
56
- name: pry
84
+ name: simplecov
57
85
  requirement: !ruby/object:Gem::Requirement
58
86
  requirements:
59
87
  - - ">="
@@ -78,11 +106,12 @@ files:
78
106
  - lib/hash_kit.rb
79
107
  - lib/hash_kit/helper.rb
80
108
  - lib/hash_kit/transform_item.rb
109
+ - lib/hash_kit/version.rb
81
110
  homepage: https://github.com/sage/hash_kit
82
111
  licenses:
83
112
  - MIT
84
113
  metadata: {}
85
- post_install_message:
114
+ post_install_message:
86
115
  rdoc_options: []
87
116
  require_paths:
88
117
  - lib
@@ -90,16 +119,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
90
119
  requirements:
91
120
  - - ">="
92
121
  - !ruby/object:Gem::Version
93
- version: '0'
122
+ version: 3.0.0
94
123
  required_rubygems_version: !ruby/object:Gem::Requirement
95
124
  requirements:
96
125
  - - ">="
97
126
  - !ruby/object:Gem::Version
98
127
  version: '0'
99
128
  requirements: []
100
- rubyforge_project:
101
- rubygems_version: 2.7.6
102
- signing_key:
129
+ rubygems_version: 3.4.20
130
+ signing_key:
103
131
  specification_version: 4
104
132
  summary: Toolkit for working with Hashes
105
133
  test_files: []