hashie 3.5.7 → 5.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (85) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +281 -195
  3. data/CONTRIBUTING.md +13 -6
  4. data/LICENSE +1 -1
  5. data/README.md +320 -60
  6. data/Rakefile +2 -2
  7. data/UPGRADING.md +121 -7
  8. data/hashie.gemspec +13 -7
  9. data/lib/hashie/clash.rb +12 -1
  10. data/lib/hashie/dash.rb +56 -35
  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 +29 -1
  14. data/lib/hashie/extensions/dash/predefined_values.rb +88 -0
  15. data/lib/hashie/extensions/dash/property_translation.rb +59 -28
  16. data/lib/hashie/extensions/deep_fetch.rb +5 -3
  17. data/lib/hashie/extensions/deep_find.rb +14 -5
  18. data/lib/hashie/extensions/deep_locate.rb +22 -8
  19. data/lib/hashie/extensions/deep_merge.rb +26 -10
  20. data/lib/hashie/extensions/ignore_undeclared.rb +4 -5
  21. data/lib/hashie/extensions/indifferent_access.rb +43 -10
  22. data/lib/hashie/extensions/key_conflict_warning.rb +55 -0
  23. data/lib/hashie/extensions/mash/define_accessors.rb +90 -0
  24. data/lib/hashie/extensions/mash/keep_original_keys.rb +4 -5
  25. data/lib/hashie/extensions/mash/permissive_respond_to.rb +61 -0
  26. data/lib/hashie/extensions/mash/safe_assignment.rb +3 -1
  27. data/lib/hashie/extensions/mash/symbolize_keys.rb +6 -6
  28. data/lib/hashie/extensions/method_access.rb +47 -14
  29. data/lib/hashie/extensions/parsers/yaml_erb_parser.rb +28 -4
  30. data/lib/hashie/extensions/ruby_version_check.rb +5 -1
  31. data/lib/hashie/extensions/strict_key_access.rb +16 -13
  32. data/lib/hashie/extensions/stringify_keys.rb +1 -1
  33. data/lib/hashie/extensions/symbolize_keys.rb +13 -2
  34. data/lib/hashie/hash.rb +18 -11
  35. data/lib/hashie/mash.rb +147 -81
  36. data/lib/hashie/railtie.rb +7 -0
  37. data/lib/hashie/rash.rb +6 -6
  38. data/lib/hashie/utils.rb +28 -0
  39. data/lib/hashie/version.rb +1 -1
  40. data/lib/hashie.rb +22 -19
  41. metadata +23 -131
  42. data/spec/hashie/array_spec.rb +0 -29
  43. data/spec/hashie/clash_spec.rb +0 -70
  44. data/spec/hashie/dash_spec.rb +0 -573
  45. data/spec/hashie/extensions/autoload_spec.rb +0 -24
  46. data/spec/hashie/extensions/coercion_spec.rb +0 -631
  47. data/spec/hashie/extensions/dash/coercion_spec.rb +0 -13
  48. data/spec/hashie/extensions/dash/indifferent_access_spec.rb +0 -84
  49. data/spec/hashie/extensions/deep_fetch_spec.rb +0 -97
  50. data/spec/hashie/extensions/deep_find_spec.rb +0 -138
  51. data/spec/hashie/extensions/deep_locate_spec.rb +0 -137
  52. data/spec/hashie/extensions/deep_merge_spec.rb +0 -70
  53. data/spec/hashie/extensions/ignore_undeclared_spec.rb +0 -47
  54. data/spec/hashie/extensions/indifferent_access_spec.rb +0 -282
  55. data/spec/hashie/extensions/indifferent_access_with_rails_hwia_spec.rb +0 -208
  56. data/spec/hashie/extensions/key_conversion_spec.rb +0 -12
  57. data/spec/hashie/extensions/mash/keep_original_keys_spec.rb +0 -46
  58. data/spec/hashie/extensions/mash/safe_assignment_spec.rb +0 -50
  59. data/spec/hashie/extensions/mash/symbolize_keys_spec.rb +0 -39
  60. data/spec/hashie/extensions/merge_initializer_spec.rb +0 -23
  61. data/spec/hashie/extensions/method_access_spec.rb +0 -188
  62. data/spec/hashie/extensions/strict_key_access_spec.rb +0 -110
  63. data/spec/hashie/extensions/stringify_keys_spec.rb +0 -124
  64. data/spec/hashie/extensions/symbolize_keys_spec.rb +0 -129
  65. data/spec/hashie/hash_spec.rb +0 -84
  66. data/spec/hashie/mash_spec.rb +0 -763
  67. data/spec/hashie/parsers/yaml_erb_parser_spec.rb +0 -46
  68. data/spec/hashie/rash_spec.rb +0 -83
  69. data/spec/hashie/trash_spec.rb +0 -268
  70. data/spec/hashie/utils_spec.rb +0 -25
  71. data/spec/hashie/version_spec.rb +0 -7
  72. data/spec/hashie_spec.rb +0 -13
  73. data/spec/integration/omniauth/app.rb +0 -11
  74. data/spec/integration/omniauth/integration_spec.rb +0 -38
  75. data/spec/integration/omniauth-oauth2/app.rb +0 -53
  76. data/spec/integration/omniauth-oauth2/integration_spec.rb +0 -26
  77. data/spec/integration/omniauth-oauth2/some_site.rb +0 -38
  78. data/spec/integration/rails/app.rb +0 -48
  79. data/spec/integration/rails/integration_spec.rb +0 -26
  80. data/spec/integration/rails-without-dependency/integration_spec.rb +0 -15
  81. data/spec/spec_helper.rb +0 -23
  82. data/spec/support/integration_specs.rb +0 -36
  83. data/spec/support/logger.rb +0 -24
  84. data/spec/support/module_context.rb +0 -11
  85. data/spec/support/ruby_version_check.rb +0 -6
@@ -35,12 +35,12 @@ module Hashie
35
35
  # end
36
36
  #
37
37
  # model = DataModelHash.new(id: '123', created: '2014-04-25 22:35:28')
38
- # model.id.class #=> Fixnum
38
+ # model.id.class #=> Integer (Fixnum if you are using Ruby 2.3 or lower)
39
39
  # model.created_at.class #=> Time
40
40
  module PropertyTranslation
41
41
  def self.included(base)
42
42
  base.instance_variable_set(:@transforms, {})
43
- base.instance_variable_set(:@translations_hash, {})
43
+ base.instance_variable_set(:@translations_hash, ::Hash.new { |hash, key| hash[key] = {} })
44
44
  base.extend(ClassMethods)
45
45
  base.send(:include, InstanceMethods)
46
46
  end
@@ -58,7 +58,9 @@ module Hashie
58
58
  end
59
59
 
60
60
  def permitted_input_keys
61
- @permitted_input_keys ||= properties.map { |property| inverse_translations.fetch property, property }
61
+ @permitted_input_keys ||=
62
+ properties
63
+ .map { |property| inverse_translations.fetch property, property }
62
64
  end
63
65
 
64
66
  # Defines a property on the Trash. Options are as follows:
@@ -72,23 +74,16 @@ module Hashie
72
74
  def property(property_name, options = {})
73
75
  super
74
76
 
75
- if options[:from]
76
- if property_name == options[:from]
77
- fail ArgumentError, "Property name (#{property_name}) and :from option must not be the same"
78
- end
79
-
80
- translations_hash[options[:from]] ||= {}
81
- translations_hash[options[:from]][property_name] = options[:with] || options[:transform_with]
77
+ from = options[:from]
78
+ converter = options[:with]
79
+ transformer = options[:transform_with]
82
80
 
83
- define_method "#{options[:from]}=" do |val|
84
- self.class.translations_hash[options[:from]].each do |name, with|
85
- self[name] = with.respond_to?(:call) ? with.call(val) : val
86
- end
87
- end
88
- else
89
- if options[:transform_with].respond_to? :call
90
- transforms[property_name] = options[:transform_with]
91
- end
81
+ if from
82
+ fail_self_transformation_error!(property_name) if property_name == from
83
+ define_translation(from, property_name, converter || transformer)
84
+ define_writer_for_source_property(from)
85
+ elsif valid_transformer?(transformer)
86
+ transforms[property_name] = transformer
92
87
  end
93
88
  end
94
89
 
@@ -105,26 +100,50 @@ module Hashie
105
100
  end
106
101
 
107
102
  def translations
108
- @translations ||= {}.tap do |h|
103
+ @translations ||= {}.tap do |translations|
109
104
  translations_hash.each do |(property_name, property_translations)|
110
- if property_translations.size > 1
111
- h[property_name] = property_translations.keys
112
- else
113
- h[property_name] = property_translations.keys.first
114
- end
105
+ translations[property_name] =
106
+ if property_translations.size > 1
107
+ property_translations.keys
108
+ else
109
+ property_translations.keys.first
110
+ end
115
111
  end
116
112
  end
117
113
  end
118
114
 
119
115
  def inverse_translations
120
- @inverse_translations ||= {}.tap do |h|
116
+ @inverse_translations ||= {}.tap do |translations|
121
117
  translations_hash.each do |(property_name, property_translations)|
122
- property_translations.keys.each do |k|
123
- h[k] = property_name
118
+ property_translations.each_key do |key|
119
+ translations[key] = property_name
124
120
  end
125
121
  end
126
122
  end
127
123
  end
124
+
125
+ private
126
+
127
+ def define_translation(from, property_name, translator)
128
+ translations_hash[from][property_name] = translator
129
+ end
130
+
131
+ def define_writer_for_source_property(property)
132
+ define_method "#{property}=" do |val|
133
+ __translations[property].each do |name, with|
134
+ self[name] = with.respond_to?(:call) ? with.call(val) : val
135
+ end
136
+ end
137
+ end
138
+
139
+ def fail_self_transformation_error!(property_name)
140
+ raise ArgumentError,
141
+ "Property name (#{property_name}) and :from option must not be the same"
142
+ end
143
+
144
+ def valid_transformer?(transformer)
145
+ transformer.respond_to? :call
146
+ end
128
147
  end
129
148
 
130
149
  module InstanceMethods
@@ -134,6 +153,12 @@ module Hashie
134
153
  def []=(property, value)
135
154
  if self.class.translation_exists? property
136
155
  send("#{property}=", value)
156
+
157
+ if self.class.transformation_exists? property
158
+ super property, self.class.transformed_property(property, value)
159
+ elsif self.class.properties.include?(property)
160
+ super(property, value)
161
+ end
137
162
  elsif self.class.transformation_exists? property
138
163
  super property, self.class.transformed_property(property, value)
139
164
  elsif property_exists? property
@@ -158,6 +183,12 @@ module Hashie
158
183
  fail_no_property_error!(property) unless self.class.property?(property)
159
184
  true
160
185
  end
186
+
187
+ private
188
+
189
+ def __translations
190
+ self.class.translations_hash
191
+ end
161
192
  end
162
193
  end
163
194
  end
@@ -9,7 +9,8 @@ module Hashie
9
9
  #
10
10
  # options.deep_fetch(:user, :non_existent_key) { 'a value' } #=> 'a value'
11
11
  #
12
- # This is particularly useful for fetching values from deeply nested api responses or params hashes.
12
+ # This is particularly useful for fetching values from deeply nested api responses
13
+ # or params hashes.
13
14
  module DeepFetch
14
15
  class UndefinedPathError < StandardError; end
15
16
 
@@ -19,8 +20,9 @@ module Hashie
19
20
  arg = Integer(arg) if obj.is_a? Array
20
21
  obj.fetch(arg)
21
22
  rescue ArgumentError, IndexError, NoMethodError => e
22
- break block.call(arg) if block
23
- raise UndefinedPathError, "Could not fetch path (#{args.join(' > ')}) at #{arg}", e.backtrace
23
+ break yield(arg) if block
24
+ raise UndefinedPathError,
25
+ "Could not fetch path (#{args.join(' > ')}) at #{arg}", e.backtrace
24
26
  end
25
27
  end
26
28
  end
@@ -1,3 +1,4 @@
1
+ require 'hashie/extensions/deep_locate'
1
2
  module Hashie
2
3
  module Extensions
3
4
  module DeepFind
@@ -19,12 +20,17 @@ module Hashie
19
20
  _deep_find(key)
20
21
  end
21
22
 
22
- alias_method :deep_detect, :deep_find
23
+ alias deep_detect deep_find
23
24
 
24
25
  # Performs a depth-first search on deeply nested data structures for
25
26
  # a key and returns all occurrences of the key.
26
27
  #
27
- # options = {users: [{location: {address: '123 Street'}}, {location: {address: '234 Street'}}]}
28
+ # options = {
29
+ # users: [
30
+ # { location: {address: '123 Street'} },
31
+ # { location: {address: '234 Street'}}
32
+ # ]
33
+ # }
28
34
  # options.extend(Hashie::Extensions::DeepFind)
29
35
  # options.deep_find_all(:address) # => ['123 Street', '234 Street']
30
36
  #
@@ -33,14 +39,17 @@ module Hashie
33
39
  # end
34
40
  #
35
41
  # my_hash = MyHash.new
36
- # my_hash[:users] = [{location: {address: '123 Street'}}, {location: {address: '234 Street'}}]
42
+ # my_hash[:users] = [
43
+ # {location: {address: '123 Street'}},
44
+ # {location: {address: '234 Street'}}
45
+ # ]
37
46
  # my_hash.deep_find_all(:address) # => ['123 Street', '234 Street']
38
47
  def deep_find_all(key)
39
48
  matches = _deep_find_all(key)
40
49
  matches.empty? ? nil : matches
41
50
  end
42
51
 
43
- alias_method :deep_select, :deep_find_all
52
+ alias deep_select deep_find_all
44
53
 
45
54
  private
46
55
 
@@ -49,7 +58,7 @@ module Hashie
49
58
  end
50
59
 
51
60
  def _deep_find_all(key, object = self, matches = [])
52
- deep_locate_result = Hashie::Extensions::DeepLocate.deep_locate(key, object).tap do |result|
61
+ deep_locate_result = DeepLocate.deep_locate(key, object).tap do |result|
53
62
  result.map! { |element| element[key] }
54
63
  end
55
64
 
@@ -14,10 +14,12 @@ module Hashie
14
14
  # ...
15
15
  # ]
16
16
  #
17
- # Hashie::Extensions::DeepLocate.deep_locate -> (key, value, object) { key == :title }, books
17
+ # DeepLocate.deep_locate -> (key, value, object) { key == :title }, books
18
18
  # # => [{:title=>"Ruby for beginners", :pages=>120}, ...]
19
19
  def self.deep_locate(comparator, object)
20
- comparator = _construct_key_comparator(comparator, object) unless comparator.respond_to?(:call)
20
+ unless comparator.respond_to?(:call)
21
+ comparator = _construct_key_comparator(comparator, object)
22
+ end
21
23
 
22
24
  _deep_locate(comparator, object)
23
25
  end
@@ -53,30 +55,34 @@ module Hashie
53
55
  # # http://ruby-journal.com/becareful-with-space-in-lambda-hash-rocket-syntax-between-ruby-1-dot-9-and-2-dot-0/
54
56
  #
55
57
  # books.deep_locate -> (key, value, object) { key == :title && value.include?("Ruby") }
56
- # # => [{:title=>"Ruby for beginners", :pages=>120}, {:title=>"Ruby for the rest of us", :pages=>576}]
58
+ # # => [{:title=>"Ruby for beginners", :pages=>120},
59
+ # # {:title=>"Ruby for the rest of us", :pages=>576}]
57
60
  #
58
61
  # books.deep_locate -> (key, value, object) { key == :pages && value <= 120 }
59
- # # => [{:title=>"Ruby for beginners", :pages=>120}, {:title=>"CSS for intermediates", :pages=>80}]
62
+ # # => [{:title=>"Ruby for beginners", :pages=>120},
63
+ # # {:title=>"CSS for intermediates", :pages=>80}]
60
64
  def deep_locate(comparator)
61
65
  Hashie::Extensions::DeepLocate.deep_locate(comparator, self)
62
66
  end
63
67
 
64
- private
65
-
66
68
  def self._construct_key_comparator(search_key, object)
67
- search_key = search_key.to_s if defined?(::ActiveSupport::HashWithIndifferentAccess) && object.is_a?(::ActiveSupport::HashWithIndifferentAccess)
68
- search_key = search_key.to_s if object.respond_to?(:indifferent_access?) && object.indifferent_access?
69
+ if object.respond_to?(:indifferent_access?) && object.indifferent_access? ||
70
+ activesupport_indifferent?(object)
71
+ search_key = search_key.to_s
72
+ end
69
73
 
70
74
  lambda do |non_callable_object|
71
75
  ->(key, _, _) { key == non_callable_object }
72
76
  end.call(search_key)
73
77
  end
78
+ private_class_method :_construct_key_comparator
74
79
 
75
80
  def self._deep_locate(comparator, object, result = [])
76
81
  if object.is_a?(::Enumerable)
77
82
  if object.any? { |value| _match_comparator?(value, comparator, object) }
78
83
  result.push object
79
84
  end
85
+
80
86
  (object.respond_to?(:values) ? object.values : object.entries).each do |value|
81
87
  _deep_locate(comparator, value, result)
82
88
  end
@@ -84,6 +90,7 @@ module Hashie
84
90
 
85
91
  result
86
92
  end
93
+ private_class_method :_deep_locate
87
94
 
88
95
  def self._match_comparator?(value, comparator, object)
89
96
  if object.is_a?(::Hash)
@@ -94,6 +101,13 @@ module Hashie
94
101
 
95
102
  comparator.call(key, value, object)
96
103
  end
104
+ private_class_method :_match_comparator?
105
+
106
+ def self.activesupport_indifferent?(object)
107
+ defined?(::ActiveSupport::HashWithIndifferentAccess) &&
108
+ object.is_a?(::ActiveSupport::HashWithIndifferentAccess)
109
+ end
110
+ private_class_method :activesupport_indifferent?
97
111
  end
98
112
  end
99
113
  end
@@ -3,7 +3,7 @@ module Hashie
3
3
  module DeepMerge
4
4
  # Returns a new hash with +self+ and +other_hash+ merged recursively.
5
5
  def deep_merge(other_hash, &block)
6
- copy = dup
6
+ copy = _deep_dup(self)
7
7
  copy.extend(Hashie::Extensions::DeepMerge) unless copy.respond_to?(:deep_merge!)
8
8
  copy.deep_merge!(other_hash, &block)
9
9
  end
@@ -18,17 +18,33 @@ module Hashie
18
18
 
19
19
  private
20
20
 
21
+ def _deep_dup(hash)
22
+ copy = hash.dup
23
+
24
+ copy.each do |key, value|
25
+ copy[key] =
26
+ if value.is_a?(::Hash)
27
+ _deep_dup(value)
28
+ else
29
+ Hashie::Utils.safe_dup(value)
30
+ end
31
+ end
32
+
33
+ copy
34
+ end
35
+
21
36
  def _recursive_merge(hash, other_hash, &block)
22
37
  other_hash.each do |k, v|
23
- hash[k] = if hash.key?(k) && hash[k].is_a?(::Hash) && v.is_a?(::Hash)
24
- _recursive_merge(hash[k], v, &block)
25
- else
26
- if hash.key?(k) && block_given?
27
- block.call(k, hash[k], v)
28
- else
29
- v.respond_to?(:deep_dup) ? v.deep_dup : v
30
- end
31
- end
38
+ hash[k] =
39
+ if hash.key?(k) && hash[k].is_a?(::Hash) && v.is_a?(::Hash)
40
+ _recursive_merge(hash[k], v, &block)
41
+ elsif v.is_a?(::Hash)
42
+ _recursive_merge({}, v, &block)
43
+ elsif hash.key?(k) && block_given?
44
+ yield(k, hash[k], v)
45
+ else
46
+ v.respond_to?(:deep_dup) ? v.deep_dup : v
47
+ end
32
48
  end
33
49
  hash
34
50
  end
@@ -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,21 +23,26 @@ 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
- Hashie::Extensions::Dash::IndifferentAccess::ClassMethods.tap do |extension|
28
- base.extend(extension) if base <= Hashie::Dash && !base.singleton_class.included_modules.include?(extension)
29
- end
34
+ Hashie::Extensions::Dash::IndifferentAccess.maybe_extend(base)
30
35
 
31
36
  base.class_eval do
32
37
  alias_method :regular_writer, :[]= unless method_defined?(:regular_writer)
33
38
  alias_method :[]=, :indifferent_writer
34
39
  alias_method :store, :indifferent_writer
35
- %w(default update replace fetch delete key? values_at).each do |m|
40
+ %w[default update replace fetch delete key? values_at].each do |m|
36
41
  alias_method "regular_#{m}", m unless method_defined?("regular_#{m}")
37
42
  alias_method m, "indifferent_#{m}"
38
43
  end
39
44
 
40
- %w(include? member? has_key?).each do |key_alias|
45
+ %w[include? member? has_key?].each do |key_alias|
41
46
  alias_method key_alias, :indifferent_key?
42
47
  end
43
48
 
@@ -68,15 +73,15 @@ module Hashie
68
73
  end
69
74
 
70
75
  def convert_key(key)
71
- key.to_s
76
+ IndifferentAccess.convert_key(key)
72
77
  end
73
78
 
74
79
  # Iterates through the keys and values, reconverting them to
75
80
  # their proper indifferent state. Used when IndifferentAccess
76
81
  # is injecting itself into member hashes.
77
82
  def convert!
78
- keys.each do |k|
79
- regular_writer convert_key(k), indifferent_value(regular_delete(k))
83
+ keys.each do |k| # rubocop:disable Performance/HashEachMethods
84
+ indifferent_writer k, regular_delete(k)
80
85
  end
81
86
  self
82
87
  end
@@ -133,14 +138,42 @@ module Hashie
133
138
  self
134
139
  end
135
140
 
136
- def merge(*)
137
- super.convert!
141
+ def merge(*args)
142
+ result = super
143
+ return IndifferentAccess.inject!(result) if hash_lacking_indifference?(result)
144
+ result.convert!
138
145
  end
139
146
 
140
147
  def merge!(*)
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)
@@ -0,0 +1,55 @@
1
+ module Hashie
2
+ module Extensions
3
+ module KeyConflictWarning
4
+ class CannotDisableMashWarnings < StandardError
5
+ def initialize
6
+ super(
7
+ 'You cannot disable warnings on the base Mash class. ' \
8
+ 'Please subclass the Mash and disable it in the subclass.'
9
+ )
10
+ end
11
+ end
12
+
13
+ # Disable the logging of warnings based on keys conflicting keys/methods
14
+ #
15
+ # @api semipublic
16
+ # @return [void]
17
+ def disable_warnings(*method_keys)
18
+ raise CannotDisableMashWarnings if self == Hashie::Mash
19
+ if method_keys.any?
20
+ disabled_warnings.concat(method_keys).tap(&:flatten!).uniq!
21
+ else
22
+ disabled_warnings.clear
23
+ end
24
+
25
+ @disable_warnings = true
26
+ end
27
+
28
+ # Checks whether this class disables warnings for conflicting keys/methods
29
+ #
30
+ # @api semipublic
31
+ # @return [Boolean]
32
+ def disable_warnings?(method_key = nil)
33
+ return disabled_warnings.include?(method_key) if disabled_warnings.any? && method_key
34
+ @disable_warnings ||= false
35
+ end
36
+
37
+ # Returns an array of methods that this class disables warnings for.
38
+ #
39
+ # @api semipublic
40
+ # @return [Boolean]
41
+ def disabled_warnings
42
+ @_disabled_warnings ||= []
43
+ end
44
+
45
+ # Inheritance hook that sets class configuration when inherited.
46
+ #
47
+ # @api semipublic
48
+ # @return [void]
49
+ def inherited(subclass)
50
+ super
51
+ subclass.disable_warnings(disabled_warnings) if disable_warnings?
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,90 @@
1
+ module Hashie
2
+ module Extensions
3
+ module Mash
4
+ module DefineAccessors
5
+ def self.included(klass)
6
+ klass.class_eval do
7
+ mod = Ext.new
8
+ include mod
9
+ end
10
+ end
11
+
12
+ def self.extended(obj)
13
+ included(obj.singleton_class)
14
+ end
15
+
16
+ class Ext < Module
17
+ def initialize
18
+ mod = self
19
+ define_method(:method_missing) do |method_name, *args, &block|
20
+ key, suffix = method_name_and_suffix(method_name)
21
+ case suffix
22
+ when '='.freeze
23
+ mod.define_writer(key, method_name)
24
+ when '?'.freeze
25
+ mod.define_predicate(key, method_name)
26
+ when '!'.freeze
27
+ mod.define_initializing_reader(key, method_name)
28
+ when '_'.freeze
29
+ mod.define_underbang_reader(key, method_name)
30
+ else
31
+ mod.define_reader(key, method_name)
32
+ end
33
+ send(method_name, *args, &block)
34
+ end
35
+ end
36
+
37
+ def define_reader(key, method_name)
38
+ define_method(method_name) do |&block|
39
+ if key? method_name
40
+ self.[](method_name, &block)
41
+ else
42
+ self.[](key, &block)
43
+ end
44
+ end
45
+ end
46
+
47
+ def define_writer(key, method_name)
48
+ define_method(method_name) do |value = nil|
49
+ if key? method_name
50
+ self.[](method_name, &proc)
51
+ else
52
+ assign_property(key, value)
53
+ end
54
+ end
55
+ end
56
+
57
+ def define_predicate(key, method_name)
58
+ define_method(method_name) do
59
+ if key? method_name
60
+ self.[](method_name, &proc)
61
+ else
62
+ !!self[key]
63
+ end
64
+ end
65
+ end
66
+
67
+ def define_initializing_reader(key, method_name)
68
+ define_method(method_name) do
69
+ if key? method_name
70
+ self.[](method_name, &proc)
71
+ else
72
+ initializing_reader(key)
73
+ end
74
+ end
75
+ end
76
+
77
+ def define_underbang_reader(key, method_name)
78
+ define_method(method_name) do
79
+ if key? method_name
80
+ self.[](key, &proc)
81
+ else
82
+ underbang_reader(key)
83
+ end
84
+ end
85
+ end
86
+ end
87
+ end
88
+ end
89
+ end
90
+ end
@@ -14,14 +14,13 @@ module Hashie
14
14
  # mash['string_key'] == mash[:string_key] #=> true
15
15
  # mash[:symbol_key] == mash['symbol_key'] #=> true
16
16
  module KeepOriginalKeys
17
- private
18
-
19
17
  def self.included(descendant)
20
- unless descendant <= Hashie::Mash
21
- fail ArgumentError, "#{descendant} is not a kind of Hashie::Mash"
22
- end
18
+ error_message = "#{descendant} is not a kind of Hashie::Mash"
19
+ raise ArgumentError, error_message unless descendant <= Hashie::Mash
23
20
  end
24
21
 
22
+ private
23
+
25
24
  # Converts the key when necessary to access the correct Mash key.
26
25
  #
27
26
  # @param [Object, String, Symbol] key the key to access.