hashie 4.1.0 → 5.1.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.
@@ -31,12 +31,11 @@ module Hashie
31
31
  module IgnoreUndeclared
32
32
  def initialize_attributes(attributes)
33
33
  return unless attributes
34
+
34
35
  klass = self.class
35
- translations = klass.respond_to?(:translations) && klass.translations
36
- attributes.each_pair do |att, value|
37
- next unless klass.property?(att) || (translations && translations.include?(att))
38
- self[att] = value
39
- end
36
+ translations = klass.respond_to?(:translations) && klass.translations || []
37
+
38
+ super(attributes.select { |attr, _| klass.property?(attr) || translations.include?(attr) })
40
39
  end
41
40
 
42
41
  def property_exists?(property)
@@ -23,6 +23,13 @@ module Hashie
23
23
  # h['baz'] # => 'blip'
24
24
  #
25
25
  module IndifferentAccess
26
+ include Hashie::Extensions::RubyVersionCheck
27
+
28
+ # @api private
29
+ def self.convert_key(key)
30
+ key.to_s
31
+ end
32
+
26
33
  def self.included(base)
27
34
  Hashie::Extensions::Dash::IndifferentAccess.maybe_extend(base)
28
35
 
@@ -66,7 +73,7 @@ module Hashie
66
73
  end
67
74
 
68
75
  def convert_key(key)
69
- key.to_s
76
+ IndifferentAccess.convert_key(key)
70
77
  end
71
78
 
72
79
  # Iterates through the keys and values, reconverting them to
@@ -74,7 +81,7 @@ module Hashie
74
81
  # is injecting itself into member hashes.
75
82
  def convert!
76
83
  keys.each do |k| # rubocop:disable Performance/HashEachMethods
77
- regular_writer convert_key(k), indifferent_value(regular_delete(k))
84
+ indifferent_writer k, regular_delete(k)
78
85
  end
79
86
  self
80
87
  end
@@ -133,7 +140,7 @@ module Hashie
133
140
 
134
141
  def merge(*args)
135
142
  result = super
136
- IndifferentAccess.inject!(result) if hash_lacking_indifference?(result)
143
+ return IndifferentAccess.inject!(result) if hash_lacking_indifference?(result)
137
144
  result.convert!
138
145
  end
139
146
 
@@ -141,6 +148,32 @@ module Hashie
141
148
  super.convert!
142
149
  end
143
150
 
151
+ def to_hash
152
+ {}.tap do |result|
153
+ each_pair { |key, value| result[key] = value }
154
+
155
+ if default_proc
156
+ result.default_proc = default_proc
157
+ else
158
+ result.default = default
159
+ end
160
+ end
161
+ end
162
+
163
+ with_minimum_ruby('2.5.0') do
164
+ def slice(*keys)
165
+ string_keys = keys.map { |key| convert_key(key) }
166
+ super(*string_keys)
167
+ end
168
+ end
169
+
170
+ with_minimum_ruby('3.0.0') do
171
+ def except(*keys)
172
+ string_keys = keys.map { |key| convert_key(key) }
173
+ super(*string_keys)
174
+ end
175
+ end
176
+
144
177
  protected
145
178
 
146
179
  def hash_lacking_indifference?(other)
@@ -5,7 +5,7 @@ module Hashie
5
5
  #
6
6
  # @example
7
7
  # class LazyResponse < Hashie::Mash
8
- # include Hashie::Extensions::Mash::SymbolizedKeys
8
+ # include Hashie::Extensions::Mash::SymbolizeKeys
9
9
  # end
10
10
  #
11
11
  # response = LazyResponse.new("id" => 123, "name" => "Rey").to_h
@@ -24,13 +24,13 @@ module Hashie
24
24
 
25
25
  private
26
26
 
27
- # Converts a key to a symbol
27
+ # Converts a key to a symbol, if possible
28
28
  #
29
29
  # @api private
30
- # @param [String, Symbol] key the key to convert to a symbol
31
- # @return [void]
30
+ # @param [<K>] key the key to attempt convert to a symbol
31
+ # @return [Symbol, K]
32
32
  def convert_key(key)
33
- key.to_sym
33
+ key.respond_to?(:to_sym) ? key.to_sym : key
34
34
  end
35
35
  end
36
36
  end
@@ -241,7 +241,7 @@ module Hashie
241
241
  end
242
242
 
243
243
  # MethodOverridingInitializer allows you to override default hash
244
- # methods when passing in values from an existing hash. The overriden
244
+ # methods when passing in values from an existing hash. The overridden
245
245
  # methods are aliased with two leading underscores.
246
246
  #
247
247
  # @example
@@ -46,7 +46,7 @@ module Hashie
46
46
  hash.extend(Hashie::Extensions::SymbolizeKeys) unless hash.respond_to?(:symbolize_keys!)
47
47
  hash.keys.each do |k| # rubocop:disable Performance/HashEachMethods
48
48
  symbolize_keys_recursively!(hash[k])
49
- hash[k.to_sym] = hash.delete(k)
49
+ hash[convert_key(k)] = hash.delete(k)
50
50
  end
51
51
  hash
52
52
  end
@@ -61,6 +61,17 @@ module Hashie
61
61
  symbolize_keys!(new_hash)
62
62
  end
63
63
  end
64
+
65
+ private
66
+
67
+ # Converts a key to a symbol, if possible
68
+ #
69
+ # @api private
70
+ # @param [<K>] key the key to attempt convert to a symbol
71
+ # @return [Symbol, K]
72
+ def convert_key(key)
73
+ key.respond_to?(:to_sym) ? key.to_sym : key
74
+ end
64
75
  end
65
76
 
66
77
  class << self
data/lib/hashie/hash.rb CHANGED
@@ -21,8 +21,8 @@ module Hashie
21
21
  assignment_key =
22
22
  if options[:stringify_keys]
23
23
  k.to_s
24
- elsif options[:symbolize_keys]
25
- k.to_s.to_sym
24
+ elsif options[:symbolize_keys] && k.respond_to?(:to_sym)
25
+ k.to_sym
26
26
  else
27
27
  k
28
28
  end
data/lib/hashie/mash.rb CHANGED
@@ -21,7 +21,7 @@ module Hashie
21
21
  # * Bang (<tt>!</tt>): Forces the existence of this key, used for deep Mashes. Think of it
22
22
  # as "touch" for mashes.
23
23
  # * Under Bang (<tt>_</tt>): Like Bang, but returns a new Mash rather than creating a key.
24
- # Used to test existance in deep Mashes.
24
+ # Used to test existence in deep Mashes.
25
25
  #
26
26
  # == Basic Example
27
27
  #
@@ -62,7 +62,6 @@ module Hashie
62
62
  # mash.author # => <Mash>
63
63
  #
64
64
  class Mash < Hash
65
- include Hashie::Extensions::PrettyInspect
66
65
  include Hashie::Extensions::RubyVersionCheck
67
66
  extend Hashie::Extensions::KeyConflictWarning
68
67
 
@@ -121,8 +120,8 @@ module Hashie
121
120
  alias regular_reader []
122
121
  alias regular_writer []=
123
122
 
124
- # Retrieves an attribute set in the Mash. Will convert
125
- # any key passed in to a string before retrieving.
123
+ # Retrieves an attribute set in the Mash. Will convert a key passed in
124
+ # as a symbol to a string before retrieving.
126
125
  def custom_reader(key)
127
126
  default_proc.call(self, key) if default_proc && !key?(key)
128
127
  value = regular_reader(convert_key(key))
@@ -130,14 +129,12 @@ module Hashie
130
129
  value
131
130
  end
132
131
 
133
- # Sets an attribute in the Mash. Key will be converted to
134
- # a string before it is set, and Hashes will be converted
135
- # into Mashes for nesting purposes.
132
+ # Sets an attribute in the Mash. Symbol keys will be converted to
133
+ # strings before being set, and Hashes will be converted into Mashes
134
+ # for nesting purposes.
136
135
  def custom_writer(key, value, convert = true) #:nodoc:
137
- key_as_symbol = (key = convert_key(key)).to_sym
138
-
139
- log_built_in_message(key_as_symbol) if log_collision?(key_as_symbol)
140
- regular_writer(key, convert ? convert_value(value) : value)
136
+ log_built_in_message(key) if key.respond_to?(:to_sym) && log_collision?(key.to_sym)
137
+ regular_writer(convert_key(key), convert ? convert_value(value) : value)
141
138
  end
142
139
 
143
140
  alias [] custom_reader
@@ -325,22 +322,18 @@ module Hashie
325
322
  self.class.new(other_hash).merge(self)
326
323
  end
327
324
 
328
- with_minimum_ruby('2.3.0') do
329
- def dig(*keys)
330
- super(*keys.map { |key| convert_key(key) })
331
- end
325
+ def dig(*keys)
326
+ super(*keys.map { |key| convert_key(key) })
332
327
  end
333
328
 
334
- with_minimum_ruby('2.4.0') do
335
- def transform_values(&blk)
336
- self.class.new(super(&blk))
337
- end
329
+ def transform_values(&blk)
330
+ self.class.new(super(&blk))
331
+ end
338
332
 
339
- # Returns a new instance of the class it was called on, with nil values
340
- # removed.
341
- def compact
342
- self.class.new(super)
343
- end
333
+ # Returns a new instance of the class it was called on, with nil values
334
+ # removed.
335
+ def compact
336
+ self.class.new(super)
344
337
  end
345
338
 
346
339
  with_minimum_ruby('2.5.0') do
@@ -354,6 +347,13 @@ module Hashie
354
347
  end
355
348
  end
356
349
 
350
+ with_minimum_ruby('3.0.0') do
351
+ def except(*keys)
352
+ string_keys = keys.map { |key| convert_key(key) }
353
+ self.class.new(super(*string_keys))
354
+ end
355
+ end
356
+
357
357
  protected
358
358
 
359
359
  def method_name_and_suffix(method_name)
@@ -371,7 +371,7 @@ module Hashie
371
371
  end
372
372
 
373
373
  def convert_key(key) #:nodoc:
374
- key.to_s
374
+ key.respond_to?(:to_sym) ? key.to_s : key
375
375
  end
376
376
 
377
377
  def convert_value(val, duping = false) #:nodoc:
@@ -406,6 +406,7 @@ module Hashie
406
406
  end
407
407
 
408
408
  def log_collision?(method_key)
409
+ return unless method_key.is_a?(String) || method_key.is_a?(Symbol)
409
410
  return unless respond_to?(method_key)
410
411
 
411
412
  _, suffix = method_name_and_suffix(method_key)
@@ -1,3 +1,3 @@
1
1
  module Hashie
2
- VERSION = '4.1.0'.freeze
2
+ VERSION = '5.1.0'.freeze
3
3
  end
data/lib/hashie.rb CHANGED
@@ -41,6 +41,7 @@ module Hashie
41
41
  autoload :IndifferentAccess, 'hashie/extensions/dash/indifferent_access'
42
42
  autoload :PropertyTranslation, 'hashie/extensions/dash/property_translation'
43
43
  autoload :Coercion, 'hashie/extensions/dash/coercion'
44
+ autoload :PredefinedValues, 'hashie/extensions/dash/predefined_values'
44
45
  end
45
46
 
46
47
  module Mash
metadata CHANGED
@@ -1,16 +1,30 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hashie
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.1.0
4
+ version: 5.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Bleigh
8
8
  - Jerry Cheung
9
- autorequire:
9
+ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2020-02-01 00:00:00.000000000 Z
12
+ date: 2025-12-24 00:00:00.000000000 Z
13
13
  dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: logger
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: '0'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: '0'
14
28
  - !ruby/object:Gem::Dependency
15
29
  name: bundler
16
30
  requirement: !ruby/object:Gem::Requirement
@@ -50,6 +64,7 @@ files:
50
64
  - lib/hashie/extensions/coercion.rb
51
65
  - lib/hashie/extensions/dash/coercion.rb
52
66
  - lib/hashie/extensions/dash/indifferent_access.rb
67
+ - lib/hashie/extensions/dash/predefined_values.rb
53
68
  - lib/hashie/extensions/dash/property_translation.rb
54
69
  - lib/hashie/extensions/deep_fetch.rb
55
70
  - lib/hashie/extensions/deep_find.rb
@@ -89,7 +104,7 @@ metadata:
89
104
  changelog_uri: https://github.com/hashie/hashie/blob/master/CHANGELOG.md
90
105
  documentation_uri: https://www.rubydoc.info/gems/hashie
91
106
  source_code_uri: https://github.com/hashie/hashie
92
- post_install_message:
107
+ post_install_message:
93
108
  rdoc_options: []
94
109
  require_paths:
95
110
  - lib
@@ -97,15 +112,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
97
112
  requirements:
98
113
  - - ">="
99
114
  - !ruby/object:Gem::Version
100
- version: '0'
115
+ version: '2.7'
101
116
  required_rubygems_version: !ruby/object:Gem::Requirement
102
117
  requirements:
103
118
  - - ">="
104
119
  - !ruby/object:Gem::Version
105
120
  version: '0'
106
121
  requirements: []
107
- rubygems_version: 3.0.3
108
- signing_key:
122
+ rubygems_version: 3.5.16
123
+ signing_key:
109
124
  specification_version: 4
110
125
  summary: Your friendly neighborhood hash library.
111
126
  test_files: []