hashie 3.5.7 → 4.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (82) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +240 -164
  3. data/CONTRIBUTING.md +13 -6
  4. data/README.md +210 -29
  5. data/Rakefile +2 -2
  6. data/UPGRADING.md +83 -7
  7. data/hashie.gemspec +13 -7
  8. data/lib/hashie.rb +21 -19
  9. data/lib/hashie/clash.rb +12 -1
  10. data/lib/hashie/dash.rb +42 -21
  11. data/lib/hashie/extensions/active_support/core_ext/hash.rb +14 -0
  12. data/lib/hashie/extensions/coercion.rb +26 -19
  13. data/lib/hashie/extensions/dash/indifferent_access.rb +20 -1
  14. data/lib/hashie/extensions/dash/property_translation.rb +54 -28
  15. data/lib/hashie/extensions/deep_fetch.rb +5 -3
  16. data/lib/hashie/extensions/deep_find.rb +14 -5
  17. data/lib/hashie/extensions/deep_locate.rb +22 -8
  18. data/lib/hashie/extensions/deep_merge.rb +26 -10
  19. data/lib/hashie/extensions/indifferent_access.rb +8 -8
  20. data/lib/hashie/extensions/key_conflict_warning.rb +55 -0
  21. data/lib/hashie/extensions/mash/define_accessors.rb +90 -0
  22. data/lib/hashie/extensions/mash/keep_original_keys.rb +4 -5
  23. data/lib/hashie/extensions/mash/permissive_respond_to.rb +61 -0
  24. data/lib/hashie/extensions/mash/safe_assignment.rb +3 -1
  25. data/lib/hashie/extensions/mash/symbolize_keys.rb +1 -1
  26. data/lib/hashie/extensions/method_access.rb +47 -14
  27. data/lib/hashie/extensions/parsers/yaml_erb_parser.rb +28 -4
  28. data/lib/hashie/extensions/ruby_version_check.rb +5 -1
  29. data/lib/hashie/extensions/strict_key_access.rb +16 -13
  30. data/lib/hashie/extensions/stringify_keys.rb +1 -1
  31. data/lib/hashie/extensions/symbolize_keys.rb +1 -1
  32. data/lib/hashie/hash.rb +18 -11
  33. data/lib/hashie/mash.rb +131 -70
  34. data/lib/hashie/railtie.rb +7 -0
  35. data/lib/hashie/rash.rb +6 -6
  36. data/lib/hashie/utils.rb +28 -0
  37. data/lib/hashie/version.rb +1 -1
  38. metadata +19 -128
  39. data/spec/hashie/array_spec.rb +0 -29
  40. data/spec/hashie/clash_spec.rb +0 -70
  41. data/spec/hashie/dash_spec.rb +0 -573
  42. data/spec/hashie/extensions/autoload_spec.rb +0 -24
  43. data/spec/hashie/extensions/coercion_spec.rb +0 -631
  44. data/spec/hashie/extensions/dash/coercion_spec.rb +0 -13
  45. data/spec/hashie/extensions/dash/indifferent_access_spec.rb +0 -84
  46. data/spec/hashie/extensions/deep_fetch_spec.rb +0 -97
  47. data/spec/hashie/extensions/deep_find_spec.rb +0 -138
  48. data/spec/hashie/extensions/deep_locate_spec.rb +0 -137
  49. data/spec/hashie/extensions/deep_merge_spec.rb +0 -70
  50. data/spec/hashie/extensions/ignore_undeclared_spec.rb +0 -47
  51. data/spec/hashie/extensions/indifferent_access_spec.rb +0 -282
  52. data/spec/hashie/extensions/indifferent_access_with_rails_hwia_spec.rb +0 -208
  53. data/spec/hashie/extensions/key_conversion_spec.rb +0 -12
  54. data/spec/hashie/extensions/mash/keep_original_keys_spec.rb +0 -46
  55. data/spec/hashie/extensions/mash/safe_assignment_spec.rb +0 -50
  56. data/spec/hashie/extensions/mash/symbolize_keys_spec.rb +0 -39
  57. data/spec/hashie/extensions/merge_initializer_spec.rb +0 -23
  58. data/spec/hashie/extensions/method_access_spec.rb +0 -188
  59. data/spec/hashie/extensions/strict_key_access_spec.rb +0 -110
  60. data/spec/hashie/extensions/stringify_keys_spec.rb +0 -124
  61. data/spec/hashie/extensions/symbolize_keys_spec.rb +0 -129
  62. data/spec/hashie/hash_spec.rb +0 -84
  63. data/spec/hashie/mash_spec.rb +0 -763
  64. data/spec/hashie/parsers/yaml_erb_parser_spec.rb +0 -46
  65. data/spec/hashie/rash_spec.rb +0 -83
  66. data/spec/hashie/trash_spec.rb +0 -268
  67. data/spec/hashie/utils_spec.rb +0 -25
  68. data/spec/hashie/version_spec.rb +0 -7
  69. data/spec/hashie_spec.rb +0 -13
  70. data/spec/integration/omniauth-oauth2/app.rb +0 -53
  71. data/spec/integration/omniauth-oauth2/integration_spec.rb +0 -26
  72. data/spec/integration/omniauth-oauth2/some_site.rb +0 -38
  73. data/spec/integration/omniauth/app.rb +0 -11
  74. data/spec/integration/omniauth/integration_spec.rb +0 -38
  75. data/spec/integration/rails-without-dependency/integration_spec.rb +0 -15
  76. data/spec/integration/rails/app.rb +0 -48
  77. data/spec/integration/rails/integration_spec.rb +0 -26
  78. data/spec/spec_helper.rb +0 -23
  79. data/spec/support/integration_specs.rb +0 -36
  80. data/spec/support/logger.rb +0 -24
  81. data/spec/support/module_context.rb +0 -11
  82. data/spec/support/ruby_version_check.rb +0 -6
@@ -3,7 +3,9 @@ module Hashie
3
3
  module Mash
4
4
  module SafeAssignment
5
5
  def custom_writer(key, *args) #:nodoc:
6
- fail ArgumentError, "The property #{key} clashes with an existing method." if !key?(key) && respond_to?(key, true)
6
+ if !key?(key) && respond_to?(key, true)
7
+ raise ArgumentError, "The property #{key} clashes with an existing method."
8
+ end
7
9
  super
8
10
  end
9
11
 
@@ -19,7 +19,7 @@ module Hashie
19
19
  # @return [void]
20
20
  # @raise [ArgumentError] when the base class isn't a Mash
21
21
  def self.included(base)
22
- fail ArgumentError, "#{base} must descent from Hashie::Mash" unless base <= Hashie::Mash
22
+ raise ArgumentError, "#{base} must descent from Hashie::Mash" unless base <= Hashie::Mash
23
23
  end
24
24
 
25
25
  private
@@ -27,7 +27,7 @@ module Hashie
27
27
  #
28
28
  # user.not_declared # => NoMethodError
29
29
  module MethodReader
30
- def respond_to?(name, include_private = false)
30
+ def respond_to_missing?(name, include_private = false)
31
31
  return true if key?(name.to_s) || key?(name.to_sym)
32
32
  super
33
33
  end
@@ -67,7 +67,7 @@ module Hashie
67
67
  # h['awesome'] # => 'sauce'
68
68
  #
69
69
  module MethodWriter
70
- def respond_to?(name, include_private = false)
70
+ def respond_to_missing?(name, include_private = false)
71
71
  return true if name.to_s =~ /=$/
72
72
  super
73
73
  end
@@ -106,7 +106,7 @@ module Hashie
106
106
  # h.def? # => false
107
107
  # h.hji? # => NoMethodError
108
108
  module MethodQuery
109
- def respond_to?(name, include_private = false)
109
+ def respond_to_missing?(name, include_private = false)
110
110
  if query_method?(name) && indifferent_key?(key_from_query_method(name))
111
111
  true
112
112
  else
@@ -156,6 +156,22 @@ module Hashie
156
156
  end
157
157
  end
158
158
 
159
+ # A module shared between MethodOverridingWriter and MethodOverridingInitializer
160
+ # to contained shared logic. This module aids in redefining existing hash methods.
161
+ module RedefineMethod
162
+ protected
163
+
164
+ def method?(name)
165
+ methods.map(&:to_s).include?(name)
166
+ end
167
+
168
+ def redefine_method(method_name)
169
+ eigenclass = class << self; self; end
170
+ eigenclass.__send__(:alias_method, "__#{method_name}", method_name)
171
+ eigenclass.__send__(:define_method, method_name, -> { self[method_name] })
172
+ end
173
+ end
174
+
159
175
  # MethodOverridingWriter gives you #key_name= shortcuts for
160
176
  # writing to your hash. It allows methods to be overridden by
161
177
  # #key_name= shortcuts and aliases those methods with two
@@ -181,6 +197,8 @@ module Hashie
181
197
  # h.__zip # => [[['awesome', 'sauce'], ['zip', 'a-dee-doo-dah']]]
182
198
  #
183
199
  module MethodOverridingWriter
200
+ include RedefineMethod
201
+
184
202
  def convert_key(key)
185
203
  key.to_s
186
204
  end
@@ -205,16 +223,6 @@ module Hashie
205
223
  def already_overridden?(name)
206
224
  method?("__#{name}")
207
225
  end
208
-
209
- def method?(name)
210
- methods.map(&:to_s).include?(name)
211
- end
212
-
213
- def redefine_method(method_name)
214
- eigenclass = class << self; self; end
215
- eigenclass.__send__(:alias_method, "__#{method_name}", method_name)
216
- eigenclass.__send__(:define_method, method_name, -> { self[method_name] })
217
- end
218
226
  end
219
227
 
220
228
  # A macro module that will automatically include MethodReader,
@@ -225,10 +233,35 @@ module Hashie
225
233
  # underscores.
226
234
  module MethodAccessWithOverride
227
235
  def self.included(base)
228
- [MethodReader, MethodOverridingWriter, MethodQuery].each do |mod|
236
+ [MethodReader, MethodOverridingWriter,
237
+ MethodQuery, MethodOverridingInitializer].each do |mod|
229
238
  base.send :include, mod
230
239
  end
231
240
  end
232
241
  end
242
+
243
+ # MethodOverridingInitializer allows you to override default hash
244
+ # methods when passing in values from an existing hash. The overriden
245
+ # methods are aliased with two leading underscores.
246
+ #
247
+ # @example
248
+ # class MyHash < Hash
249
+ # include Hashie::Extensions::MethodOverridingInitializer
250
+ # end
251
+ #
252
+ # h = MyHash.new(zip: 'a-dee-doo-dah')
253
+ # h.zip # => 'a-dee-doo-dah'
254
+ # h.__zip # => [[['zip', 'a-dee-doo-dah']]]
255
+ module MethodOverridingInitializer
256
+ include RedefineMethod
257
+
258
+ def initialize(hash = {})
259
+ hash.each do |key, value|
260
+ skey = key.to_s
261
+ redefine_method(skey) if method?(skey)
262
+ self[skey] = value
263
+ end
264
+ end
265
+ end
233
266
  end
234
267
  end
@@ -1,22 +1,46 @@
1
1
  require 'yaml'
2
2
  require 'erb'
3
+ require 'pathname'
4
+
3
5
  module Hashie
4
6
  module Extensions
5
7
  module Parsers
6
8
  class YamlErbParser
7
- def initialize(file_path)
9
+ def initialize(file_path, options = {})
8
10
  @content = File.read(file_path)
9
11
  @file_path = file_path.is_a?(Pathname) ? file_path.to_s : file_path
12
+ @options = options
10
13
  end
11
14
 
12
15
  def perform
13
16
  template = ERB.new(@content)
14
17
  template.filename = @file_path
15
- YAML.load template.result
18
+ permitted_classes = @options.fetch(:permitted_classes) { [] }
19
+ permitted_symbols = @options.fetch(:permitted_symbols) { [] }
20
+ aliases = @options.fetch(:aliases) { true }
21
+
22
+ yaml_safe_load(template, permitted_classes, permitted_symbols, aliases)
16
23
  end
17
24
 
18
- def self.perform(file_path)
19
- new(file_path).perform
25
+ def self.perform(file_path, options = {})
26
+ new(file_path, options).perform
27
+ end
28
+
29
+ private
30
+
31
+ if Gem::Version.new(Psych::VERSION) >= Gem::Version.new('3.1.0') # Ruby 2.6+
32
+ def yaml_safe_load(template, permitted_classes, permitted_symbols, aliases)
33
+ YAML.safe_load(
34
+ template.result,
35
+ permitted_classes: permitted_classes,
36
+ permitted_symbols: permitted_symbols,
37
+ aliases: aliases
38
+ )
39
+ end
40
+ else
41
+ def yaml_safe_load(template, permitted_classes, permitted_symbols, aliases)
42
+ YAML.safe_load(template.result, permitted_classes, permitted_symbols, aliases)
43
+ end
20
44
  end
21
45
  end
22
46
  end
@@ -9,7 +9,11 @@ module Hashie
9
9
 
10
10
  module ClassMethods
11
11
  def with_minimum_ruby(version)
12
- yield if RubyVersion.new(RUBY_VERSION) >= RubyVersion.new(version)
12
+ yield if with_minimum_ruby?(version)
13
+ end
14
+
15
+ def with_minimum_ruby?(version)
16
+ RubyVersion.new(RUBY_VERSION) >= RubyVersion.new(version)
13
17
  end
14
18
  end
15
19
  end
@@ -1,6 +1,7 @@
1
1
  module Hashie
2
2
  module Extensions
3
- # SRP: This extension will fail an error whenever a key is accessed that does not exist in the hash.
3
+ # SRP: This extension will fail an error whenever a key is accessed
4
+ # that does not exist in the hash.
4
5
  #
5
6
  # EXAMPLE:
6
7
  #
@@ -15,12 +16,15 @@ module Hashie
15
16
  # >> hash[:cow]
16
17
  # KeyError: key not found: :cow
17
18
  #
18
- # NOTE: For googlers coming from Python to Ruby, this extension makes a Hash behave more like a "Dictionary".
19
+ # NOTE: For googlers coming from Python to Ruby, this extension makes a Hash
20
+ # behave more like a "Dictionary".
19
21
  #
20
22
  module StrictKeyAccess
21
23
  class DefaultError < StandardError
22
- def initialize(msg = 'Setting or using a default with Hashie::Extensions::StrictKeyAccess does not make sense', *args)
23
- super
24
+ def initialize
25
+ super('Setting or using a default with Hashie::Extensions::StrictKeyAccess'\
26
+ ' does not make sense'
27
+ )
24
28
  end
25
29
  end
26
30
 
@@ -46,27 +50,26 @@ module Hashie
46
50
  end
47
51
 
48
52
  def default(_ = nil)
49
- fail DefaultError
53
+ raise DefaultError
50
54
  end
51
55
 
52
56
  def default=(_)
53
- fail DefaultError
57
+ raise DefaultError
54
58
  end
55
59
 
56
60
  def default_proc
57
- fail DefaultError
61
+ raise DefaultError
58
62
  end
59
63
 
60
64
  def default_proc=(_)
61
- fail DefaultError
65
+ raise DefaultError
62
66
  end
63
67
 
64
68
  def key(value)
65
- result = super
66
- if result.nil? && (!key?(result) || self[result] != value)
67
- fail KeyError, "key not found with value of #{value.inspect}"
68
- else
69
- result
69
+ super.tap do |result|
70
+ if result.nil? && (!key?(result) || self[result] != value)
71
+ raise KeyError, "key not found with value of #{value.inspect}"
72
+ end
70
73
  end
71
74
  end
72
75
  end
@@ -44,7 +44,7 @@ module Hashie
44
44
  # test # => {'abc' => 'def'}
45
45
  def stringify_keys!(hash)
46
46
  hash.extend(Hashie::Extensions::StringifyKeys) unless hash.respond_to?(:stringify_keys!)
47
- hash.keys.each do |k|
47
+ hash.keys.each do |k| # rubocop:disable Performance/HashEachMethods
48
48
  stringify_keys_recursively!(hash[k])
49
49
  hash[k.to_s] = hash.delete(k)
50
50
  end
@@ -44,7 +44,7 @@ module Hashie
44
44
  # test # => {:abc => 'def'}
45
45
  def symbolize_keys!(hash)
46
46
  hash.extend(Hashie::Extensions::SymbolizeKeys) unless hash.respond_to?(:symbolize_keys!)
47
- hash.keys.each do |k|
47
+ hash.keys.each do |k| # rubocop:disable Performance/HashEachMethods
48
48
  symbolize_keys_recursively!(hash[k])
49
49
  hash[k.to_sym] = hash.delete(k)
50
50
  end
data/lib/hashie/hash.rb CHANGED
@@ -17,21 +17,22 @@ module Hashie
17
17
  # Converts a mash back to a hash (with stringified or symbolized keys)
18
18
  def to_hash(options = {})
19
19
  out = {}
20
- keys.each do |k|
21
- assignment_key = if options[:stringify_keys]
22
- k.to_s
23
- elsif options[:symbolize_keys]
24
- k.to_s.to_sym
25
- else
26
- k
27
- end
20
+ each_key do |k|
21
+ assignment_key =
22
+ if options[:stringify_keys]
23
+ k.to_s
24
+ elsif options[:symbolize_keys]
25
+ k.to_s.to_sym
26
+ else
27
+ k
28
+ end
28
29
  if self[k].is_a?(Array)
29
30
  out[assignment_key] ||= []
30
31
  self[k].each do |array_object|
31
- out[assignment_key] << (Hash === array_object ? flexibly_convert_to_hash(array_object, options) : array_object)
32
+ out[assignment_key] << maybe_convert_to_hash(array_object, options)
32
33
  end
33
34
  else
34
- out[assignment_key] = (Hash === self[k] || self[k].respond_to?(:to_hash)) ? flexibly_convert_to_hash(self[k], options) : self[k]
35
+ out[assignment_key] = maybe_convert_to_hash(self[k], options)
35
36
  end
36
37
  end
37
38
  out
@@ -44,8 +45,14 @@ module Hashie
44
45
 
45
46
  private
46
47
 
48
+ def maybe_convert_to_hash(object, options)
49
+ return object unless object.is_a?(Hash) || object.respond_to?(:to_hash)
50
+
51
+ flexibly_convert_to_hash(object, options)
52
+ end
53
+
47
54
  def flexibly_convert_to_hash(object, options = {})
48
- if object.method(:to_hash).arity == 0
55
+ if object.method(:to_hash).arity.zero?
49
56
  object.to_hash
50
57
  else
51
58
  object.to_hash(options)
data/lib/hashie/mash.rb CHANGED
@@ -2,6 +2,7 @@ require 'hashie/hash'
2
2
  require 'hashie/array'
3
3
  require 'hashie/utils'
4
4
  require 'hashie/logger'
5
+ require 'hashie/extensions/key_conflict_warning'
5
6
 
6
7
  module Hashie
7
8
  # Mash allows you to create pseudo-objects that have method-like
@@ -15,9 +16,12 @@ module Hashie
15
16
  #
16
17
  # * No punctuation: Returns the value of the hash for that key, or nil if none exists.
17
18
  # * Assignment (<tt>=</tt>): Sets the attribute of the given method name.
18
- # * Existence (<tt>?</tt>): Returns true or false depending on whether that key has been set.
19
- # * Bang (<tt>!</tt>): Forces the existence of this key, used for deep Mashes. Think of it as "touch" for mashes.
20
- # * Under Bang (<tt>_</tt>): Like Bang, but returns a new Mash rather than creating a key. Used to test existance in deep Mashes.
19
+ # * Truthiness (<tt>?</tt>): Returns true or false depending on the truthiness of
20
+ # the attribute, or false if the key is not set.
21
+ # * Bang (<tt>!</tt>): Forces the existence of this key, used for deep Mashes. Think of it
22
+ # as "touch" for mashes.
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.
21
25
  #
22
26
  # == Basic Example
23
27
  #
@@ -60,49 +64,19 @@ module Hashie
60
64
  class Mash < Hash
61
65
  include Hashie::Extensions::PrettyInspect
62
66
  include Hashie::Extensions::RubyVersionCheck
67
+ extend Hashie::Extensions::KeyConflictWarning
63
68
 
64
- ALLOWED_SUFFIXES = %w(? ! = _)
65
-
66
- class CannotDisableMashWarnings < StandardError
67
- def initialize(message = 'You cannot disable warnings on the base Mash class. Please subclass the Mash and disable it in the subclass.')
68
- super(message)
69
- end
70
- end
71
-
72
- # Disable the logging of warnings based on keys conflicting keys/methods
73
- #
74
- # @api semipublic
75
- # @return [void]
76
- def self.disable_warnings
77
- fail CannotDisableMashWarnings if self == Hashie::Mash
78
- @disable_warnings = true
79
- end
80
-
81
- # Checks whether this class disables warnings for conflicting keys/methods
82
- #
83
- # @api semipublic
84
- # @return [Boolean]
85
- def self.disable_warnings?
86
- @disable_warnings ||= false
87
- end
88
-
89
- # Inheritance hook that sets class configuration when inherited.
90
- #
91
- # @api semipublic
92
- # @return [void]
93
- def self.inherited(subclass)
94
- super
95
- subclass.disable_warnings if disable_warnings?
96
- end
69
+ ALLOWED_SUFFIXES = %w[? ! = _].freeze
97
70
 
98
71
  def self.load(path, options = {})
99
72
  @_mashes ||= new
100
73
 
101
74
  return @_mashes[path] if @_mashes.key?(path)
102
- fail ArgumentError, "The following file doesn't exist: #{path}" unless File.file?(path)
75
+ raise ArgumentError, "The following file doesn't exist: #{path}" unless File.file?(path)
103
76
 
104
- parser = options.fetch(:parser) { Hashie::Extensions::Parsers::YamlErbParser }
105
- @_mashes[path] = new(parser.perform(path)).freeze
77
+ options = options.dup
78
+ parser = options.delete(:parser) { Hashie::Extensions::Parsers::YamlErbParser }
79
+ @_mashes[path] = new(parser.perform(path, options)).freeze
106
80
  end
107
81
 
108
82
  def to_module(mash_method_name = :settings)
@@ -114,7 +88,11 @@ module Hashie
114
88
  end
115
89
  end
116
90
 
117
- alias_method :to_s, :inspect
91
+ def with_accessors!
92
+ extend Hashie::Extensions::Mash::DefineAccessors
93
+ end
94
+
95
+ alias to_s inspect
118
96
 
119
97
  # If you pass in an existing hash, it will
120
98
  # convert it to a Mash including recursively
@@ -125,10 +103,23 @@ module Hashie
125
103
  default ? super(default) : super(&blk)
126
104
  end
127
105
 
128
- class << self; alias_method :[], :new; end
106
+ # Creates a new anonymous subclass with key conflict
107
+ # warnings disabled. You may pass an array of method
108
+ # symbols to restrict the disabled warnings to.
109
+ # Hashie::Mash.quiet.new(hash) all warnings disabled.
110
+ # Hashie::Mash.quiet(:zip).new(hash) only zip warning
111
+ # is disabled.
112
+ def self.quiet(*method_keys)
113
+ @memoized_classes ||= {}
114
+ @memoized_classes[method_keys] ||= Class.new(self) do
115
+ disable_warnings(*method_keys)
116
+ end
117
+ end
118
+
119
+ class << self; alias [] new; end
129
120
 
130
- alias_method :regular_reader, :[]
131
- alias_method :regular_writer, :[]=
121
+ alias regular_reader []
122
+ alias regular_writer []=
132
123
 
133
124
  # Retrieves an attribute set in the Mash. Will convert
134
125
  # any key passed in to a string before retrieving.
@@ -149,8 +140,8 @@ module Hashie
149
140
  regular_writer(key, convert ? convert_value(value) : value)
150
141
  end
151
142
 
152
- alias_method :[], :custom_reader
153
- alias_method :[]=, :custom_writer
143
+ alias [] custom_reader
144
+ alias []= custom_writer
154
145
 
155
146
  # This is the bang method reader, it will return a new Mash
156
147
  # if there isn't a value already assigned to the key requested.
@@ -183,45 +174,89 @@ module Hashie
183
174
  super(*keys.map { |key| convert_key(key) })
184
175
  end
185
176
 
186
- alias_method :regular_dup, :dup
177
+ # Returns a new instance of the class it was called on, using its keys as
178
+ # values, and its values as keys. The new values and keys will always be
179
+ # strings.
180
+ def invert
181
+ self.class.new(super)
182
+ end
183
+
184
+ # Returns a new instance of the class it was called on, containing elements
185
+ # for which the given block returns false.
186
+ def reject(&blk)
187
+ self.class.new(super(&blk))
188
+ end
189
+
190
+ # Returns a new instance of the class it was called on, containing elements
191
+ # for which the given block returns true.
192
+ def select(&blk)
193
+ self.class.new(super(&blk))
194
+ end
195
+
196
+ alias regular_dup dup
187
197
  # Duplicates the current mash as a new mash.
188
198
  def dup
189
- self.class.new(self, default)
199
+ self.class.new(self, default, &default_proc)
190
200
  end
191
201
 
192
- alias_method :regular_key?, :key?
202
+ alias regular_key? key?
193
203
  def key?(key)
194
204
  super(convert_key(key))
195
205
  end
196
- alias_method :has_key?, :key?
197
- alias_method :include?, :key?
198
- alias_method :member?, :key?
206
+ alias has_key? key?
207
+ alias include? key?
208
+ alias member? key?
199
209
 
200
- # Performs a deep_update on a duplicate of the
201
- # current mash.
202
- def deep_merge(other_hash, &blk)
203
- dup.deep_update(other_hash, &blk)
210
+ if with_minimum_ruby?('2.6.0')
211
+ # Performs a deep_update on a duplicate of the
212
+ # current mash.
213
+ def deep_merge(*other_hashes, &blk)
214
+ dup.deep_update(*other_hashes, &blk)
215
+ end
216
+
217
+ # Recursively merges this mash with the passed
218
+ # in hash, merging each hash in the hierarchy.
219
+ def deep_update(*other_hashes, &blk)
220
+ other_hashes.each do |other_hash|
221
+ _deep_update(other_hash, &blk)
222
+ end
223
+ self
224
+ end
225
+ else
226
+ # Performs a deep_update on a duplicate of the
227
+ # current mash.
228
+ def deep_merge(other_hash, &blk)
229
+ dup.deep_update(other_hash, &blk)
230
+ end
231
+
232
+ # Recursively merges this mash with the passed
233
+ # in hash, merging each hash in the hierarchy.
234
+ def deep_update(other_hash, &blk)
235
+ _deep_update(other_hash, &blk)
236
+ self
237
+ end
204
238
  end
205
- alias_method :merge, :deep_merge
206
239
 
207
- # Recursively merges this mash with the passed
208
- # in hash, merging each hash in the hierarchy.
209
- def deep_update(other_hash, &blk)
240
+ # Alias these lexically so they get the correctly defined
241
+ # #deep_merge and #deep_update based on ruby version.
242
+ alias merge deep_merge
243
+ alias deep_merge! deep_update
244
+ alias update deep_update
245
+ alias merge! update
246
+
247
+ def _deep_update(other_hash, &blk)
210
248
  other_hash.each_pair do |k, v|
211
249
  key = convert_key(k)
212
- if regular_reader(key).is_a?(Mash) && v.is_a?(::Hash)
250
+ if v.is_a?(::Hash) && key?(key) && regular_reader(key).is_a?(Mash)
213
251
  custom_reader(key).deep_update(v, &blk)
214
252
  else
215
253
  value = convert_value(v, true)
216
- value = convert_value(blk.call(key, self[k], value), true) if blk && self.key?(k)
254
+ value = convert_value(yield(key, self[k], value), true) if blk && key?(k)
217
255
  custom_writer(key, value, false)
218
256
  end
219
257
  end
220
- self
221
258
  end
222
- alias_method :deep_merge!, :deep_update
223
- alias_method :update, :deep_update
224
- alias_method :merge!, :update
259
+ private :_deep_update
225
260
 
226
261
  # Assigns a value to a key
227
262
  def assign_property(name, value)
@@ -263,7 +298,7 @@ module Hashie
263
298
  method_name.end_with?(*ALLOWED_SUFFIXES) && key?(method_name.chop)
264
299
  end
265
300
 
266
- def method_missing(method_name, *args, &blk)
301
+ def method_missing(method_name, *args, &blk) # rubocop:disable Style/MethodMissing
267
302
  return self.[](method_name, &blk) if key?(method_name)
268
303
  name, suffix = method_name_and_suffix(method_name)
269
304
  case suffix
@@ -296,6 +331,29 @@ module Hashie
296
331
  end
297
332
  end
298
333
 
334
+ with_minimum_ruby('2.4.0') do
335
+ def transform_values(&blk)
336
+ self.class.new(super(&blk))
337
+ end
338
+
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
344
+ end
345
+
346
+ with_minimum_ruby('2.5.0') do
347
+ def slice(*keys)
348
+ string_keys = keys.map { |key| convert_key(key) }
349
+ self.class.new(super(*string_keys))
350
+ end
351
+
352
+ def transform_keys(&blk)
353
+ self.class.new(super(&blk))
354
+ end
355
+ end
356
+
299
357
  protected
300
358
 
301
359
  def method_name_and_suffix(method_name)
@@ -325,8 +383,6 @@ module Hashie
325
383
  when ::Hash
326
384
  val = val.dup if duping
327
385
  self.class.new(val)
328
- when Array
329
- val.map { |e| convert_value(e) }
330
386
  when ::Array
331
387
  Array.new(val.map { |e| convert_value(e) })
332
388
  else
@@ -337,7 +393,7 @@ module Hashie
337
393
  private
338
394
 
339
395
  def log_built_in_message(method_key)
340
- return if self.class.disable_warnings?
396
+ return if self.class.disable_warnings?(method_key)
341
397
 
342
398
  method_information = Hashie::Utils.method_information(method(method_key))
343
399
 
@@ -350,7 +406,12 @@ module Hashie
350
406
  end
351
407
 
352
408
  def log_collision?(method_key)
353
- respond_to?(method_key) && !self.class.disable_warnings? &&
409
+ return unless respond_to?(method_key)
410
+
411
+ _, suffix = method_name_and_suffix(method_key)
412
+
413
+ (!suffix || suffix == '='.freeze) &&
414
+ !self.class.disable_warnings?(method_key) &&
354
415
  !(regular_key?(method_key) || regular_key?(method_key.to_s))
355
416
  end
356
417
  end