indifference 0.0.1

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e6c95187b14bfca687e3780bd1097ceeae7a4d85
4
+ data.tar.gz: 9dea95bd5ea351331deb6af3abc3f18dbb925e3d
5
+ SHA512:
6
+ metadata.gz: 38cfcb7affe79b50785db22ca45da4ba5f39ca80b0a9d1040c863d36f17c4ab5911db8c3dedb0482ba4cd01908ec26d97a7eaa0e36c3da667a8a5fc82cfaab3d
7
+ data.tar.gz: 8a8ff82fe1ddf39dbe894e43de61271c3d3966de4d06ace7c5189354c9219b036649a37f5dfc216c1e92cab9cc99191661cebc87ec6ddb9c37ca052d51c9b14b
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.0
5
+ before_install: gem install bundler -v 1.12.5
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at chase@code0100fun.com. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Chase McCarthy
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,24 @@
1
+ # Indifference
2
+
3
+ For when you just need `with_indifferent_access`. Stolen 100% from ActiveSupport.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'indifference'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install indifference
20
+
21
+ ## License
22
+
23
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
24
+
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "indifference"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'indifference/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "indifference"
8
+ spec.version = Indifference::VERSION
9
+ spec.authors = ["Chase McCarthy"]
10
+ spec.email = ["chase@code0100fun.com"]
11
+
12
+ spec.summary = %q{Just `with_indifferent_access`.}
13
+ spec.description = %q{For when you just need `with_indifferent_access`. Stolen 100% from ActiveSupport.}
14
+ spec.homepage = "https://github.com/code0100fun/indifference"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.12"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec", "~> 3.0"
25
+ end
@@ -0,0 +1,6 @@
1
+ require "indifference/version"
2
+ require "indifference/hash"
3
+
4
+ module Indifference
5
+
6
+ end
@@ -0,0 +1,23 @@
1
+ require 'indifference/hash_with_indifferent_access'
2
+
3
+ class Hash
4
+
5
+ # Returns an <tt>Indifference::HashWithIndifferentAccess</tt> out of its receiver:
6
+ #
7
+ # { a: 1 }.with_indifferent_access['a'] # => 1
8
+ def with_indifferent_access
9
+ Indifference::HashWithIndifferentAccess.new(self)
10
+ end
11
+
12
+ # Called when object is nested under an object that receives
13
+ # #with_indifferent_access. This method will be called on the current object
14
+ # by the enclosing object and is aliased to #with_indifferent_access by
15
+ # default. Subclasses of Hash may overwrite this method to return +self+ if
16
+ # converting to an <tt>Indifference::HashWithIndifferentAccess</tt> would not be
17
+ # desirable.
18
+ #
19
+ # b = { b: 1 }
20
+ # { a: b }.with_indifferent_access['a'] # calls b.nested_under_indifferent_access
21
+ # # => {"b"=>1}
22
+ alias nested_under_indifferent_access with_indifferent_access
23
+ end
@@ -0,0 +1,170 @@
1
+ class Hash
2
+ # Returns a new hash with all keys converted using the +block+ operation.
3
+ #
4
+ # hash = { name: 'Rob', age: '28' }
5
+ #
6
+ # hash.transform_keys { |key| key.to_s.upcase } # => {"NAME"=>"Rob", "AGE"=>"28"}
7
+ #
8
+ # If you do not provide a +block+, it will return an Enumerator
9
+ # for chaining with other methods:
10
+ #
11
+ # hash.transform_keys.with_index { |k, i| [k, i].join } # => {"name0"=>"Rob", "age1"=>"28"}
12
+ def transform_keys
13
+ return enum_for(:transform_keys) { size } unless block_given?
14
+ result = {}
15
+ each_key do |key|
16
+ result[yield(key)] = self[key]
17
+ end
18
+ result
19
+ end
20
+
21
+ # Destructively converts all keys using the +block+ operations.
22
+ # Same as +transform_keys+ but modifies +self+.
23
+ def transform_keys!
24
+ return enum_for(:transform_keys!) { size } unless block_given?
25
+ keys.each do |key|
26
+ self[yield(key)] = delete(key)
27
+ end
28
+ self
29
+ end
30
+
31
+ # Returns a new hash with all keys converted to strings.
32
+ #
33
+ # hash = { name: 'Rob', age: '28' }
34
+ #
35
+ # hash.stringify_keys
36
+ # # => {"name"=>"Rob", "age"=>"28"}
37
+ def stringify_keys
38
+ transform_keys(&:to_s)
39
+ end
40
+
41
+ # Destructively converts all keys to strings. Same as
42
+ # +stringify_keys+, but modifies +self+.
43
+ def stringify_keys!
44
+ transform_keys!(&:to_s)
45
+ end
46
+
47
+ # Returns a new hash with all keys converted to symbols, as long as
48
+ # they respond to +to_sym+.
49
+ #
50
+ # hash = { 'name' => 'Rob', 'age' => '28' }
51
+ #
52
+ # hash.symbolize_keys
53
+ # # => {:name=>"Rob", :age=>"28"}
54
+ def symbolize_keys
55
+ transform_keys{ |key| key.to_sym rescue key }
56
+ end
57
+ alias_method :to_options, :symbolize_keys
58
+
59
+ # Destructively converts all keys to symbols, as long as they respond
60
+ # to +to_sym+. Same as +symbolize_keys+, but modifies +self+.
61
+ def symbolize_keys!
62
+ transform_keys!{ |key| key.to_sym rescue key }
63
+ end
64
+ alias_method :to_options!, :symbolize_keys!
65
+
66
+ # Validates all keys in a hash match <tt>*valid_keys</tt>, raising
67
+ # +ArgumentError+ on a mismatch.
68
+ #
69
+ # Note that keys are treated differently than HashWithIndifferentAccess,
70
+ # meaning that string and symbol keys will not match.
71
+ #
72
+ # { name: 'Rob', years: '28' }.assert_valid_keys(:name, :age) # => raises "ArgumentError: Unknown key: :years. Valid keys are: :name, :age"
73
+ # { name: 'Rob', age: '28' }.assert_valid_keys('name', 'age') # => raises "ArgumentError: Unknown key: :name. Valid keys are: 'name', 'age'"
74
+ # { name: 'Rob', age: '28' }.assert_valid_keys(:name, :age) # => passes, raises nothing
75
+ def assert_valid_keys(*valid_keys)
76
+ valid_keys.flatten!
77
+ each_key do |k|
78
+ unless valid_keys.include?(k)
79
+ raise ArgumentError.new("Unknown key: #{k.inspect}. Valid keys are: #{valid_keys.map(&:inspect).join(', ')}")
80
+ end
81
+ end
82
+ end
83
+
84
+ # Returns a new hash with all keys converted by the block operation.
85
+ # This includes the keys from the root hash and from all
86
+ # nested hashes and arrays.
87
+ #
88
+ # hash = { person: { name: 'Rob', age: '28' } }
89
+ #
90
+ # hash.deep_transform_keys{ |key| key.to_s.upcase }
91
+ # # => {"PERSON"=>{"NAME"=>"Rob", "AGE"=>"28"}}
92
+ def deep_transform_keys(&block)
93
+ _deep_transform_keys_in_object(self, &block)
94
+ end
95
+
96
+ # Destructively converts all keys by using the block operation.
97
+ # This includes the keys from the root hash and from all
98
+ # nested hashes and arrays.
99
+ def deep_transform_keys!(&block)
100
+ _deep_transform_keys_in_object!(self, &block)
101
+ end
102
+
103
+ # Returns a new hash with all keys converted to strings.
104
+ # This includes the keys from the root hash and from all
105
+ # nested hashes and arrays.
106
+ #
107
+ # hash = { person: { name: 'Rob', age: '28' } }
108
+ #
109
+ # hash.deep_stringify_keys
110
+ # # => {"person"=>{"name"=>"Rob", "age"=>"28"}}
111
+ def deep_stringify_keys
112
+ deep_transform_keys(&:to_s)
113
+ end
114
+
115
+ # Destructively converts all keys to strings.
116
+ # This includes the keys from the root hash and from all
117
+ # nested hashes and arrays.
118
+ def deep_stringify_keys!
119
+ deep_transform_keys!(&:to_s)
120
+ end
121
+
122
+ # Returns a new hash with all keys converted to symbols, as long as
123
+ # they respond to +to_sym+. This includes the keys from the root hash
124
+ # and from all nested hashes and arrays.
125
+ #
126
+ # hash = { 'person' => { 'name' => 'Rob', 'age' => '28' } }
127
+ #
128
+ # hash.deep_symbolize_keys
129
+ # # => {:person=>{:name=>"Rob", :age=>"28"}}
130
+ def deep_symbolize_keys
131
+ deep_transform_keys{ |key| key.to_sym rescue key }
132
+ end
133
+
134
+ # Destructively converts all keys to symbols, as long as they respond
135
+ # to +to_sym+. This includes the keys from the root hash and from all
136
+ # nested hashes and arrays.
137
+ def deep_symbolize_keys!
138
+ deep_transform_keys!{ |key| key.to_sym rescue key }
139
+ end
140
+
141
+ private
142
+ # support methods for deep transforming nested hashes and arrays
143
+ def _deep_transform_keys_in_object(object, &block)
144
+ case object
145
+ when Hash
146
+ object.each_with_object({}) do |(key, value), result|
147
+ result[yield(key)] = _deep_transform_keys_in_object(value, &block)
148
+ end
149
+ when Array
150
+ object.map {|e| _deep_transform_keys_in_object(e, &block) }
151
+ else
152
+ object
153
+ end
154
+ end
155
+
156
+ def _deep_transform_keys_in_object!(object, &block)
157
+ case object
158
+ when Hash
159
+ object.keys.each do |key|
160
+ value = object.delete(key)
161
+ object[yield(key)] = _deep_transform_keys_in_object!(value, &block)
162
+ end
163
+ object
164
+ when Array
165
+ object.map! {|e| _deep_transform_keys_in_object!(e, &block)}
166
+ else
167
+ object
168
+ end
169
+ end
170
+ end
@@ -0,0 +1,22 @@
1
+ class Hash
2
+ # Merges the caller into +other_hash+. For example,
3
+ #
4
+ # options = options.reverse_merge(size: 25, velocity: 10)
5
+ #
6
+ # is equivalent to
7
+ #
8
+ # options = { size: 25, velocity: 10 }.merge(options)
9
+ #
10
+ # This is particularly useful for initializing an options hash
11
+ # with default values.
12
+ def reverse_merge(other_hash)
13
+ other_hash.merge(self)
14
+ end
15
+
16
+ # Destructive +reverse_merge+.
17
+ def reverse_merge!(other_hash)
18
+ # right wins if there is no left
19
+ merge!( other_hash ){|key,left,right| left }
20
+ end
21
+ alias_method :reverse_update, :reverse_merge!
22
+ end
@@ -0,0 +1,309 @@
1
+ require 'indifference/hash_keys'
2
+ require 'indifference/hash_reverse_merge'
3
+
4
+ module Indifference
5
+ # Implements a hash where keys <tt>:foo</tt> and <tt>"foo"</tt> are considered
6
+ # to be the same.
7
+ #
8
+ # rgb = Indifference::HashWithIndifferentAccess.new
9
+ #
10
+ # rgb[:black] = '#000000'
11
+ # rgb[:black] # => '#000000'
12
+ # rgb['black'] # => '#000000'
13
+ #
14
+ # rgb['white'] = '#FFFFFF'
15
+ # rgb[:white] # => '#FFFFFF'
16
+ # rgb['white'] # => '#FFFFFF'
17
+ #
18
+ # Internally symbols are mapped to strings when used as keys in the entire
19
+ # writing interface (calling <tt>[]=</tt>, <tt>merge</tt>, etc). This
20
+ # mapping belongs to the public interface. For example, given:
21
+ #
22
+ # hash = Indifference::HashWithIndifferentAccess.new(a: 1)
23
+ #
24
+ # You are guaranteed that the key is returned as a string:
25
+ #
26
+ # hash.keys # => ["a"]
27
+ #
28
+ # Technically other types of keys are accepted:
29
+ #
30
+ # hash = Indifference::HashWithIndifferentAccess.new(a: 1)
31
+ # hash[0] = 0
32
+ # hash # => {"a"=>1, 0=>0}
33
+ #
34
+ # but this class is intended for use cases where strings or symbols are the
35
+ # expected keys and it is convenient to understand both as the same. For
36
+ # example the +params+ hash in Ruby on Rails.
37
+ #
38
+ # Note that core extensions define <tt>Hash#with_indifferent_access</tt>:
39
+ #
40
+ # rgb = { black: '#000000', white: '#FFFFFF' }.with_indifferent_access
41
+ #
42
+ # which may be handy.
43
+ class HashWithIndifferentAccess < Hash
44
+ # Returns +true+ so that <tt>Array#extract_options!</tt> finds members of
45
+ # this class.
46
+ def extractable_options?
47
+ true
48
+ end
49
+
50
+ def with_indifferent_access
51
+ dup
52
+ end
53
+
54
+ def nested_under_indifferent_access
55
+ self
56
+ end
57
+
58
+ def initialize(constructor = {})
59
+ if constructor.respond_to?(:to_hash)
60
+ super()
61
+ update(constructor)
62
+
63
+ hash = constructor.to_hash
64
+ self.default = hash.default if hash.default
65
+ self.default_proc = hash.default_proc if hash.default_proc
66
+ else
67
+ super(constructor)
68
+ end
69
+ end
70
+
71
+ def default(*args)
72
+ arg_key = args.first
73
+
74
+ if include?(key = convert_key(arg_key))
75
+ self[key]
76
+ else
77
+ super
78
+ end
79
+ end
80
+
81
+ def self.new_from_hash_copying_default(hash)
82
+ new(hash)
83
+ end
84
+
85
+ def self.[](*args)
86
+ new.merge!(Hash[*args])
87
+ end
88
+
89
+ alias_method :regular_writer, :[]= unless method_defined?(:regular_writer)
90
+ alias_method :regular_update, :update unless method_defined?(:regular_update)
91
+
92
+ # Assigns a new value to the hash:
93
+ #
94
+ # hash = Indifference::HashWithIndifferentAccess.new
95
+ # hash[:key] = 'value'
96
+ #
97
+ # This value can be later fetched using either +:key+ or <tt>'key'</tt>.
98
+ def []=(key, value)
99
+ regular_writer(convert_key(key), convert_value(value, for: :assignment))
100
+ end
101
+
102
+ alias_method :store, :[]=
103
+
104
+ # Updates the receiver in-place, merging in the hash passed as argument:
105
+ #
106
+ # hash_1 = Indifference::HashWithIndifferentAccess.new
107
+ # hash_1[:key] = 'value'
108
+ #
109
+ # hash_2 = Indifference::HashWithIndifferentAccess.new
110
+ # hash_2[:key] = 'New Value!'
111
+ #
112
+ # hash_1.update(hash_2) # => {"key"=>"New Value!"}
113
+ #
114
+ # The argument can be either an
115
+ # <tt>Indifference::HashWithIndifferentAccess</tt> or a regular +Hash+.
116
+ # In either case the merge respects the semantics of indifferent access.
117
+ #
118
+ # If the argument is a regular hash with keys +:key+ and +"key"+ only one
119
+ # of the values end up in the receiver, but which one is unspecified.
120
+ #
121
+ # When given a block, the value for duplicated keys will be determined
122
+ # by the result of invoking the block with the duplicated key, the value
123
+ # in the receiver, and the value in +other_hash+. The rules for duplicated
124
+ # keys follow the semantics of indifferent access:
125
+ #
126
+ # hash_1[:key] = 10
127
+ # hash_2['key'] = 12
128
+ # hash_1.update(hash_2) { |key, old, new| old + new } # => {"key"=>22}
129
+ def update(other_hash)
130
+ if other_hash.is_a? HashWithIndifferentAccess
131
+ super(other_hash)
132
+ else
133
+ other_hash.to_hash.each_pair do |key, value|
134
+ if block_given? && key?(key)
135
+ value = yield(convert_key(key), self[key], value)
136
+ end
137
+ regular_writer(convert_key(key), convert_value(value))
138
+ end
139
+ self
140
+ end
141
+ end
142
+
143
+ alias_method :merge!, :update
144
+
145
+ # Checks the hash for a key matching the argument passed in:
146
+ #
147
+ # hash = Indifference::HashWithIndifferentAccess.new
148
+ # hash['key'] = 'value'
149
+ # hash.key?(:key) # => true
150
+ # hash.key?('key') # => true
151
+ def key?(key)
152
+ super(convert_key(key))
153
+ end
154
+
155
+ alias_method :include?, :key?
156
+ alias_method :has_key?, :key?
157
+ alias_method :member?, :key?
158
+
159
+
160
+ # Same as <tt>Hash#[]</tt> where the key passed as argument can be
161
+ # either a string or a symbol:
162
+ #
163
+ # counters = Indifference::HashWithIndifferentAccess.new
164
+ # counters[:foo] = 1
165
+ #
166
+ # counters['foo'] # => 1
167
+ # counters[:foo] # => 1
168
+ # counters[:zoo] # => nil
169
+ def [](key)
170
+ super(convert_key(key))
171
+ end
172
+
173
+ # Same as <tt>Hash#fetch</tt> where the key passed as argument can be
174
+ # either a string or a symbol:
175
+ #
176
+ # counters = Indifference::HashWithIndifferentAccess.new
177
+ # counters[:foo] = 1
178
+ #
179
+ # counters.fetch('foo') # => 1
180
+ # counters.fetch(:bar, 0) # => 0
181
+ # counters.fetch(:bar) { |key| 0 } # => 0
182
+ # counters.fetch(:zoo) # => KeyError: key not found: "zoo"
183
+ def fetch(key, *extras)
184
+ super(convert_key(key), *extras)
185
+ end
186
+
187
+ # Returns an array of the values at the specified indices:
188
+ #
189
+ # hash = Indifference::HashWithIndifferentAccess.new
190
+ # hash[:a] = 'x'
191
+ # hash[:b] = 'y'
192
+ # hash.values_at('a', 'b') # => ["x", "y"]
193
+ def values_at(*indices)
194
+ indices.collect { |key| self[convert_key(key)] }
195
+ end
196
+
197
+ # Returns a shallow copy of the hash.
198
+ #
199
+ # hash = Indifference::HashWithIndifferentAccess.new({ a: { b: 'b' } })
200
+ # dup = hash.dup
201
+ # dup[:a][:c] = 'c'
202
+ #
203
+ # hash[:a][:c] # => nil
204
+ # dup[:a][:c] # => "c"
205
+ def dup
206
+ self.class.new(self).tap do |new_hash|
207
+ set_defaults(new_hash)
208
+ end
209
+ end
210
+
211
+ # This method has the same semantics of +update+, except it does not
212
+ # modify the receiver but rather returns a new hash with indifferent
213
+ # access with the result of the merge.
214
+ def merge(hash, &block)
215
+ self.dup.update(hash, &block)
216
+ end
217
+
218
+ # Like +merge+ but the other way around: Merges the receiver into the
219
+ # argument and returns a new hash with indifferent access as result:
220
+ #
221
+ # hash = Indifference::HashWithIndifferentAccess.new
222
+ # hash['a'] = nil
223
+ # hash.reverse_merge(a: 0, b: 1) # => {"a"=>nil, "b"=>1}
224
+ def reverse_merge(other_hash)
225
+ super(self.class.new(other_hash))
226
+ end
227
+
228
+ # Same semantics as +reverse_merge+ but modifies the receiver in-place.
229
+ def reverse_merge!(other_hash)
230
+ replace(reverse_merge( other_hash ))
231
+ end
232
+
233
+ # Replaces the contents of this hash with other_hash.
234
+ #
235
+ # h = { "a" => 100, "b" => 200 }
236
+ # h.replace({ "c" => 300, "d" => 400 }) # => {"c"=>300, "d"=>400}
237
+ def replace(other_hash)
238
+ super(self.class.new(other_hash))
239
+ end
240
+
241
+ # Removes the specified key from the hash.
242
+ def delete(key)
243
+ super(convert_key(key))
244
+ end
245
+
246
+ def stringify_keys!; self end
247
+ def deep_stringify_keys!; self end
248
+ def stringify_keys; dup end
249
+ def deep_stringify_keys; dup end
250
+ undef :symbolize_keys!
251
+ undef :deep_symbolize_keys!
252
+ def symbolize_keys; to_hash.symbolize_keys! end
253
+ def deep_symbolize_keys; to_hash.deep_symbolize_keys! end
254
+ def to_options!; self end
255
+
256
+ def select(*args, &block)
257
+ return to_enum(:select) unless block_given?
258
+ dup.tap { |hash| hash.select!(*args, &block) }
259
+ end
260
+
261
+ def reject(*args, &block)
262
+ return to_enum(:reject) unless block_given?
263
+ dup.tap { |hash| hash.reject!(*args, &block) }
264
+ end
265
+
266
+ # Convert to a regular hash with string keys.
267
+ def to_hash
268
+ _new_hash = Hash.new
269
+ set_defaults(_new_hash)
270
+
271
+ each do |key, value|
272
+ _new_hash[key] = convert_value(value, for: :to_hash)
273
+ end
274
+ _new_hash
275
+ end
276
+
277
+ protected
278
+ def convert_key(key)
279
+ key.kind_of?(Symbol) ? key.to_s : key
280
+ end
281
+
282
+ def convert_value(value, options = {})
283
+ if value.is_a? Hash
284
+ if options[:for] == :to_hash
285
+ value.to_hash
286
+ else
287
+ value.nested_under_indifferent_access
288
+ end
289
+ elsif value.is_a?(Array)
290
+ if options[:for] != :assignment || value.frozen?
291
+ value = value.dup
292
+ end
293
+ value.map! { |e| convert_value(e, options) }
294
+ else
295
+ value
296
+ end
297
+ end
298
+
299
+ def set_defaults(target)
300
+ if default_proc
301
+ target.default_proc = default_proc.dup
302
+ else
303
+ target.default = default
304
+ end
305
+ end
306
+ end
307
+ end
308
+
309
+ HashWithIndifferentAccess = Indifference::HashWithIndifferentAccess
@@ -0,0 +1,3 @@
1
+ module Indifference
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: indifference
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Chase McCarthy
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-06-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.12'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.12'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: For when you just need `with_indifferent_access`. Stolen 100% from ActiveSupport.
56
+ email:
57
+ - chase@code0100fun.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - CODE_OF_CONDUCT.md
66
+ - Gemfile
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - bin/console
71
+ - bin/setup
72
+ - indifference.gemspec
73
+ - lib/indifference.rb
74
+ - lib/indifference/hash.rb
75
+ - lib/indifference/hash_keys.rb
76
+ - lib/indifference/hash_reverse_merge.rb
77
+ - lib/indifference/hash_with_indifferent_access.rb
78
+ - lib/indifference/version.rb
79
+ homepage: https://github.com/code0100fun/indifference
80
+ licenses:
81
+ - MIT
82
+ metadata: {}
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ requirements: []
98
+ rubyforge_project:
99
+ rubygems_version: 2.4.5
100
+ signing_key:
101
+ specification_version: 4
102
+ summary: Just `with_indifferent_access`.
103
+ test_files: []