bson 1.7.1 → 1.8.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of bson might be problematic. Click here for more details.

File without changes
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.8.0
@@ -0,0 +1,29 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'bson'
3
+
4
+ s.version = File.read(File.join(File.dirname(__FILE__), 'VERSION'))
5
+ s.authors = ['Tyler Brock', 'Gary Murakami', 'Emily Stolfo', 'Brandon Black']
6
+ s.email = 'mongodb-dev@googlegroups.com'
7
+ s.homepage = 'http://www.mongodb.org'
8
+ s.summary = 'Ruby implementation of BSON'
9
+ s.description = 'A Ruby BSON implementation for MongoDB. For more information about Mongo, see http://www.mongodb.org. For more information on BSON, see http://www.bsonspec.org.'
10
+ s.rubyforge_project = 'bson'
11
+
12
+ s.files = ['bson.gemspec', 'LICENSE', 'VERSION']
13
+ s.files += ['bin/b2json', 'bin/j2bson', 'lib/bson.rb']
14
+ s.files += Dir['lib/bson/**/*.rb']
15
+
16
+ if RUBY_PLATFORM =~ /java/
17
+ s.files += ['ext/java/jar/jbson.jar', 'ext/java/jar/mongo-2.6.5.jar']
18
+ s.platform = Gem::Platform::JAVA
19
+ else
20
+ s.platform = Gem::Platform::RUBY
21
+ end
22
+
23
+ s.test_files = Dir['test/bson/*.rb']
24
+ s.executables = ['b2json', 'j2bson']
25
+ s.require_paths = ['lib']
26
+ s.has_rdoc = 'yard'
27
+
28
+ s.add_dependency('activesupport')
29
+ end
@@ -1,7 +1,7 @@
1
1
  # encoding: UTF-8
2
2
  #
3
3
  # --
4
- # Copyright (C) 2008-2011 10gen Inc.
4
+ # Copyright (C) 2008-2012 10gen Inc.
5
5
  #
6
6
  # Licensed under the Apache License, Version 2.0 (the "License");
7
7
  # you may not use this file except in compliance with the License.
@@ -16,8 +16,6 @@
16
16
  # limitations under the License.
17
17
  # ++
18
18
 
19
- require 'bson/version'
20
-
21
19
  module BSON
22
20
  if defined? Mongo::DEFAULT_MAX_BSON_SIZE
23
21
  DEFAULT_MAX_BSON_SIZE = Mongo::DEFAULT_MAX_BSON_SIZE
@@ -87,6 +85,9 @@ else
87
85
  end
88
86
  end
89
87
 
88
+ require 'active_support'
89
+ require 'active_support/hash_with_indifferent_access'
90
+
90
91
  require 'bson/types/binary'
91
92
  require 'bson/types/code'
92
93
  require 'bson/types/dbref'
@@ -1,7 +1,7 @@
1
1
  # encoding: UTF-8
2
2
 
3
3
  # --
4
- # Copyright (C) 2008-2011 10gen Inc.
4
+ # Copyright (C) 2008-2012 10gen Inc.
5
5
  #
6
6
  # Licensed under the Apache License, Version 2.0 (the "License");
7
7
  # you may not use this file except in compliance with the License.
@@ -1,3 +1,5 @@
1
+ require 'jruby'
2
+
1
3
  include Java
2
4
  module BSON
3
5
  class BSON_JAVA
@@ -1,7 +1,7 @@
1
1
  # encoding: UTF-8
2
2
 
3
3
  # --
4
- # Copyright (C) 2008-2011 10gen Inc.
4
+ # Copyright (C) 2008-2012 10gen Inc.
5
5
  #
6
6
  # Licensed under the Apache License, Version 2.0 (the "License");
7
7
  # you may not use this file except in compliance with the License.
@@ -628,7 +628,9 @@ module BSON
628
628
  raise InvalidDocument, "#{o.class} is not currently supported; " +
629
629
  "use a UTC Time instance instead."
630
630
  else
631
- if defined?(ActiveSupport::TimeWithZone) && o.is_a?(ActiveSupport::TimeWithZone)
631
+ if defined?(ActiveSupport::Multibyte::Chars) && o.is_a?(ActiveSupport::Multibyte::Chars)
632
+ STRING
633
+ elsif defined?(ActiveSupport::TimeWithZone) && o.is_a?(ActiveSupport::TimeWithZone)
632
634
  raise InvalidDocument, "ActiveSupport::TimeWithZone is not currently supported; " +
633
635
  "use a UTC Time instance instead."
634
636
  else
@@ -1,7 +1,7 @@
1
1
  # encoding: UTF-8
2
2
 
3
3
  # --
4
- # Copyright (C) 2008-2011 10gen Inc.
4
+ # Copyright (C) 2008-2012 10gen Inc.
5
5
  #
6
6
  # Licensed under the Apache License, Version 2.0 (the "License");
7
7
  # you may not use this file except in compliance with the License.
@@ -1,7 +1,7 @@
1
1
  # encoding: UTF-8
2
2
 
3
3
  # --
4
- # Copyright (C) 2008-2011 10gen Inc.
4
+ # Copyright (C) 2008-2012 10gen Inc.
5
5
  #
6
6
  # Licensed under the Apache License, Version 2.0 (the "License");
7
7
  # you may not use this file except in compliance with the License.
@@ -1,7 +1,7 @@
1
1
  # encoding: UTF-8
2
2
 
3
3
  # --
4
- # Copyright (C) 2008-2011 10gen Inc.
4
+ # Copyright (C) 2008-2012 10gen Inc.
5
5
  #
6
6
  # Licensed under the Apache License, Version 2.0 (the "License");
7
7
  # you may not use this file except in compliance with the License.
@@ -1,7 +1,7 @@
1
1
  # encoding: UTF-8
2
2
 
3
3
  # --
4
- # Copyright (C) 2008-2011 10gen Inc.
4
+ # Copyright (C) 2008-2012 10gen Inc.
5
5
  #
6
6
  # Licensed under the Apache License, Version 2.0 (the "License");
7
7
  # you may not use this file except in compliance with the License.
@@ -1,7 +1,7 @@
1
1
  # encoding: UTF-8
2
2
 
3
3
  # --
4
- # Copyright (C) 2008-2011 10gen Inc.
4
+ # Copyright (C) 2008-2012 10gen Inc.
5
5
  #
6
6
  # Licensed under the Apache License, Version 2.0 (the "License");
7
7
  # you may not use this file except in compliance with the License.
@@ -1,7 +1,7 @@
1
1
  # encoding: UTF-8
2
2
 
3
3
  # --
4
- # Copyright (C) 2008-2011 10gen Inc.
4
+ # Copyright (C) 2008-2012 10gen Inc.
5
5
  #
6
6
  # Licensed under the Apache License, Version 2.0 (the "License");
7
7
  # you may not use this file except in compliance with the License.
@@ -1,7 +1,7 @@
1
1
  # encoding: UTF-8
2
2
 
3
3
  # --
4
- # Copyright (C) 2008-2011 10gen Inc.
4
+ # Copyright (C) 2008-2012 10gen Inc.
5
5
  #
6
6
  # Licensed under the Apache License, Version 2.0 (the "License");
7
7
  # you may not use this file except in compliance with the License.
@@ -1,7 +1,7 @@
1
1
  # encoding: UTF-8
2
2
 
3
3
  # --
4
- # Copyright (C) 2008-2011 10gen Inc.
4
+ # Copyright (C) 2008-2012 10gen Inc.
5
5
  #
6
6
  # Licensed under the Apache License, Version 2.0 (the "License");
7
7
  # you may not use this file except in compliance with the License.
@@ -1,7 +1,7 @@
1
1
  # encoding: UTF-8
2
2
 
3
3
  # --
4
- # Copyright (C) 2008-2011 10gen Inc.
4
+ # Copyright (C) 2008-2012 10gen Inc.
5
5
  #
6
6
  # Licensed under the Apache License, Version 2.0 (the "License");
7
7
  # you may not use this file except in compliance with the License.
@@ -1,5 +1,5 @@
1
1
  # encoding:utf-8
2
- require File.expand_path("../test_helper", __FILE__)
2
+ require 'test_helper'
3
3
 
4
4
  class BinaryTest < Test::Unit::TestCase
5
5
  def setup
@@ -1,5 +1,5 @@
1
1
  # encoding:utf-8
2
- require File.expand_path("../test_helper", __FILE__)
2
+ require 'test_helper'
3
3
  require 'set'
4
4
 
5
5
  if RUBY_VERSION < '1.9'
@@ -27,6 +27,18 @@ rescue LoadError
27
27
  Zone = ActiveSupport::TimeWithZone.new(Time.now.utc, 'EST')
28
28
  end
29
29
 
30
+ begin
31
+ require 'active_support/multibyte/chars'
32
+ rescue LoadError
33
+ warn 'Mocking ActiveSupport::Multibyte::Chars'
34
+ module ActiveSupport
35
+ module Multibyte
36
+ class Chars < String
37
+ end
38
+ end
39
+ end
40
+ end
41
+
30
42
  class BSONTest < Test::Unit::TestCase
31
43
 
32
44
  include BSON
@@ -70,6 +82,17 @@ class BSONTest < Test::Unit::TestCase
70
82
  assert_doc_pass(doc)
71
83
  end
72
84
 
85
+ def test_valid_active_support_multibyte_chars
86
+ unless RUBY_PLATFORM =~ /java/
87
+ doc = {'doc' => ActiveSupport::Multibyte::Chars.new('aé')}
88
+ assert_doc_pass(doc)
89
+
90
+ bson = @encoder.serialize(doc)
91
+ doc = @encoder.deserialize(bson)
92
+ assert_equal doc['doc'], 'aé'
93
+ end
94
+ end
95
+
73
96
  def test_valid_utf8_key
74
97
  doc = {'aé' => 'hello'}
75
98
  assert_doc_pass(doc)
@@ -101,12 +124,11 @@ class BSONTest < Test::Unit::TestCase
101
124
  # In 1.8 we test that other string encodings raise an exception.
102
125
  # In 1.9 we test that they get auto-converted.
103
126
  if RUBY_VERSION < '1.9'
104
- if ! RUBY_PLATFORM =~ /java/
127
+ unless RUBY_PLATFORM == 'java'
105
128
  require 'iconv'
106
129
  def test_non_utf8_string
107
130
  string = Iconv.conv('iso-8859-1', 'utf-8', 'aé')
108
131
  doc = {'doc' => string}
109
- assert_doc_pass(doc)
110
132
  assert_raise InvalidStringEncoding do
111
133
  @encoder.serialize(doc)
112
134
  end
@@ -121,48 +143,50 @@ class BSONTest < Test::Unit::TestCase
121
143
  end
122
144
  end
123
145
  else
124
- def test_non_utf8_string
125
- assert_raise BSON::InvalidStringEncoding do
126
- BSON::BSON_CODER.serialize({'str' => 'aé'.encode('iso-8859-1')})
146
+ unless RUBY_PLATFORM == 'java'
147
+ def test_non_utf8_string
148
+ assert_raise BSON::InvalidStringEncoding do
149
+ BSON::BSON_CODER.serialize({'str' => 'aé'.encode('iso-8859-1')})
150
+ end
127
151
  end
128
- end
129
152
 
130
- def test_invalid_utf8_string
131
- str = "123\xD9"
132
- assert !str.valid_encoding?
133
- assert_raise BSON::InvalidStringEncoding do
134
- BSON::BSON_CODER.serialize({'str' => str})
153
+ def test_invalid_utf8_string
154
+ str = "123\xD9"
155
+ assert !str.valid_encoding?
156
+ assert_raise BSON::InvalidStringEncoding do
157
+ BSON::BSON_CODER.serialize({'str' => str})
158
+ end
135
159
  end
136
- end
137
160
 
138
- def test_non_utf8_key
139
- assert_raise BSON::InvalidStringEncoding do
140
- BSON::BSON_CODER.serialize({'aé'.encode('iso-8859-1') => 'hello'})
161
+ def test_non_utf8_key
162
+ assert_raise BSON::InvalidStringEncoding do
163
+ BSON::BSON_CODER.serialize({'aé'.encode('iso-8859-1') => 'hello'})
164
+ end
141
165
  end
142
- end
143
166
 
144
- def test_forced_encoding_with_valid_utf8
145
- doc = {'doc' => "\xC3\xB6".force_encoding("ISO-8859-1")}
146
- serialized = @encoder.serialize(doc)
147
- deserialized = @encoder.deserialize(serialized)
148
- assert_equal(doc['doc'], deserialized['doc'].force_encoding("ISO-8859-1"))
149
- end
167
+ def test_forced_encoding_with_valid_utf8
168
+ doc = {'doc' => "\xC3\xB6".force_encoding("ISO-8859-1")}
169
+ serialized = @encoder.serialize(doc)
170
+ deserialized = @encoder.deserialize(serialized)
171
+ assert_equal(doc['doc'], deserialized['doc'].force_encoding("ISO-8859-1"))
172
+ end
150
173
 
151
- # Based on a test from sqlite3-ruby
152
- def test_default_internal_is_honored
153
- before_enc = Encoding.default_internal
174
+ # Based on a test from sqlite3-ruby
175
+ def test_default_internal_is_honored
176
+ before_enc = Encoding.default_internal
154
177
 
155
- str = "壁に耳あり、障子に目あり"
156
- bson = BSON::BSON_CODER.serialize("x" => str)
178
+ str = "壁に耳あり、障子に目あり"
179
+ bson = BSON::BSON_CODER.serialize("x" => str)
157
180
 
158
- silently { Encoding.default_internal = 'EUC-JP' }
159
- out = BSON::BSON_CODER.deserialize(bson)["x"]
181
+ silently { Encoding.default_internal = 'EUC-JP' }
182
+ out = BSON::BSON_CODER.deserialize(bson)["x"]
160
183
 
161
- assert_equal Encoding.default_internal, out.encoding
162
- assert_equal str.encode('EUC-JP'), out
163
- assert_equal str, out.encode(str.encoding)
164
- ensure
165
- silently { Encoding.default_internal = before_enc }
184
+ assert_equal Encoding.default_internal, out.encoding
185
+ assert_equal str.encode('EUC-JP'), out
186
+ assert_equal str, out.encode(str.encoding)
187
+ ensure
188
+ silently { Encoding.default_internal = before_enc }
189
+ end
166
190
  end
167
191
  end
168
192
 
@@ -287,7 +311,7 @@ class BSONTest < Test::Unit::TestCase
287
311
  end
288
312
 
289
313
  def test_date_before_epoch
290
- if RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/ then return true end
314
+ if RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/ then return true end
291
315
  begin
292
316
  doc = {'date' => Time.utc(1600)}
293
317
  bson = @encoder.serialize(doc)
@@ -1,5 +1,5 @@
1
1
  # encoding: binary
2
- require File.expand_path("../test_helper", __FILE__)
2
+ require 'test_helper'
3
3
 
4
4
  class ByteBufferTest < Test::Unit::TestCase
5
5
  include BSON
@@ -1,6 +1,6 @@
1
1
  # encoding:utf-8
2
- require File.expand_path("../test_helper", __FILE__)
3
- require './test/support/hash_with_indifferent_access'
2
+ require 'test_helper'
3
+ require 'support/hash_with_indifferent_access'
4
4
 
5
5
  class HashWithIndifferentAccessTest < Test::Unit::TestCase
6
6
  include BSON
@@ -1,5 +1,4 @@
1
- require File.expand_path("../test_helper", __FILE__)
2
- require 'rubygems'
1
+ require 'test_helper'
3
2
  require 'json'
4
3
 
5
4
  class JSONTest < Test::Unit::TestCase
@@ -1,5 +1,4 @@
1
- require File.expand_path("../test_helper", __FILE__)
2
- require 'rubygems'
1
+ require 'test_helper'
3
2
  require 'json'
4
3
 
5
4
  class ObjectIdTest < Test::Unit::TestCase
@@ -1,4 +1,4 @@
1
- require File.expand_path("../test_helper", __FILE__)
1
+ require 'test_helper'
2
2
 
3
3
  class OrderedHashTest < Test::Unit::TestCase
4
4
 
@@ -1,4 +1,4 @@
1
- require File.expand_path("../test_helper", __FILE__)
1
+ require 'test_helper'
2
2
 
3
3
  class TimestampTest < Test::Unit::TestCase
4
4
 
metadata CHANGED
@@ -1,18 +1,35 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bson
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.7.1
4
+ version: 1.8.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
8
- - Jim Menard
9
- - Mike Dirolf
10
- - Kyle Banker
8
+ - Tyler Brock
9
+ - Gary Murakami
10
+ - Emily Stolfo
11
+ - Brandon Black
11
12
  autorequire:
12
13
  bindir: bin
13
14
  cert_chain: []
14
- date: 2012-11-15 00:00:00.000000000 Z
15
- dependencies: []
15
+ date: 2012-11-27 00:00:00.000000000 Z
16
+ dependencies:
17
+ - !ruby/object:Gem::Dependency
18
+ name: activesupport
19
+ requirement: !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - ! '>='
23
+ - !ruby/object:Gem::Version
24
+ version: '0'
25
+ type: :runtime
26
+ prerelease: false
27
+ version_requirements: !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
16
33
  description: A Ruby BSON implementation for MongoDB. For more information about Mongo,
17
34
  see http://www.mongodb.org. For more information on BSON, see http://www.bsonspec.org.
18
35
  email: mongodb-dev@googlegroups.com
@@ -22,33 +39,32 @@ executables:
22
39
  extensions: []
23
40
  extra_rdoc_files: []
24
41
  files:
25
- - LICENSE.txt
42
+ - bson.gemspec
43
+ - LICENSE
44
+ - VERSION
45
+ - bin/b2json
46
+ - bin/j2bson
26
47
  - lib/bson.rb
27
- - lib/bson/bson_c.rb
28
- - lib/bson/bson_java.rb
29
- - lib/bson/bson_ruby.rb
30
- - lib/bson/byte_buffer.rb
31
- - lib/bson/exceptions.rb
32
48
  - lib/bson/ordered_hash.rb
33
- - lib/bson/types/binary.rb
34
- - lib/bson/types/code.rb
49
+ - lib/bson/bson_c.rb
35
50
  - lib/bson/types/dbref.rb
36
51
  - lib/bson/types/min_max_keys.rb
37
- - lib/bson/types/object_id.rb
38
52
  - lib/bson/types/timestamp.rb
39
- - lib/bson/version.rb
40
- - bin/b2json
41
- - bin/j2bson
42
- - test/bson/binary_test.rb
43
- - test/bson/bson_test.rb
44
- - test/bson/byte_buffer_test.rb
53
+ - lib/bson/types/code.rb
54
+ - lib/bson/types/binary.rb
55
+ - lib/bson/types/object_id.rb
56
+ - lib/bson/bson_java.rb
57
+ - lib/bson/byte_buffer.rb
58
+ - lib/bson/exceptions.rb
59
+ - lib/bson/bson_ruby.rb
45
60
  - test/bson/hash_with_indifferent_access_test.rb
46
- - test/bson/json_test.rb
47
- - test/bson/object_id_test.rb
61
+ - test/bson/bson_test.rb
62
+ - test/bson/binary_test.rb
48
63
  - test/bson/ordered_hash_test.rb
49
- - test/bson/test_helper.rb
50
64
  - test/bson/timestamp_test.rb
51
- - test/support/hash_with_indifferent_access.rb
65
+ - test/bson/byte_buffer_test.rb
66
+ - test/bson/object_id_test.rb
67
+ - test/bson/json_test.rb
52
68
  homepage: http://www.mongodb.org
53
69
  licenses: []
54
70
  post_install_message:
@@ -61,9 +77,6 @@ required_ruby_version: !ruby/object:Gem::Requirement
61
77
  - - ! '>='
62
78
  - !ruby/object:Gem::Version
63
79
  version: '0'
64
- segments:
65
- - 0
66
- hash: -549832473750178804
67
80
  required_rubygems_version: !ruby/object:Gem::Requirement
68
81
  none: false
69
82
  requirements:
@@ -71,20 +84,18 @@ required_rubygems_version: !ruby/object:Gem::Requirement
71
84
  - !ruby/object:Gem::Version
72
85
  version: '0'
73
86
  requirements: []
74
- rubyforge_project:
87
+ rubyforge_project: bson
75
88
  rubygems_version: 1.8.24
76
89
  signing_key:
77
90
  specification_version: 3
78
91
  summary: Ruby implementation of BSON
79
92
  test_files:
80
- - test/bson/binary_test.rb
81
- - test/bson/bson_test.rb
82
- - test/bson/byte_buffer_test.rb
83
93
  - test/bson/hash_with_indifferent_access_test.rb
84
- - test/bson/json_test.rb
85
- - test/bson/object_id_test.rb
94
+ - test/bson/bson_test.rb
95
+ - test/bson/binary_test.rb
86
96
  - test/bson/ordered_hash_test.rb
87
- - test/bson/test_helper.rb
88
97
  - test/bson/timestamp_test.rb
89
- - test/support/hash_with_indifferent_access.rb
90
- has_rdoc: true
98
+ - test/bson/byte_buffer_test.rb
99
+ - test/bson/object_id_test.rb
100
+ - test/bson/json_test.rb
101
+ has_rdoc: yard
@@ -1,3 +0,0 @@
1
- module BSON
2
- VERSION = "1.7.1"
3
- end
@@ -1,30 +0,0 @@
1
- require File.join(File.dirname(__FILE__), '..', '..', 'lib', 'bson')
2
- require 'rubygems' if RUBY_VERSION < '1.9.0'
3
- gem 'test-unit'
4
- require 'test/unit'
5
-
6
- def silently
7
- warn_level = $VERBOSE
8
- $VERBOSE = nil
9
- result = yield
10
- $VERBOSE = warn_level
11
- result
12
- end
13
-
14
- require 'bson_ext/cbson' if !(RUBY_PLATFORM =~ /java/) && ENV['C_EXT']
15
-
16
- class Test::Unit::TestCase
17
- include BSON
18
-
19
- def assert_raise_error(klass, message)
20
- begin
21
- yield
22
- rescue => e
23
- assert_equal klass, e.class
24
- assert e.message.include?(message), "#{e.message} does not include #{message}."
25
- else
26
- flunk "Expected assertion #{klass} but none was raised."
27
- end
28
- end
29
-
30
- end
@@ -1,186 +0,0 @@
1
- # Note: HashWithIndifferentAccess is so commonly used
2
- # that we always need to make sure that the driver works
3
- # with it.
4
- #require File.join(File.dirname(__FILE__), 'keys.rb')
5
-
6
- # This class has dubious semantics and we only have it so that
7
- # people can write params[:key] instead of params['key']
8
- # and they get the same value for both keys.
9
-
10
- class Hash
11
- # Return a new hash with all keys converted to strings.
12
- def stringify_keys
13
- dup.stringify_keys!
14
- end
15
-
16
- # Destructively convert all keys to strings.
17
- def stringify_keys!
18
- keys.each do |key|
19
- self[key.to_s] = delete(key)
20
- end
21
- self
22
- end
23
-
24
- # Return a new hash with all keys converted to symbols, as long as
25
- # they respond to +to_sym+.
26
- def symbolize_keys
27
- dup.symbolize_keys!
28
- end
29
-
30
- # Destructively convert all keys to symbols, as long as they respond
31
- # to +to_sym+.
32
- def symbolize_keys!
33
- keys.each do |key|
34
- self[(key.to_sym rescue key) || key] = delete(key)
35
- end
36
- self
37
- end
38
-
39
- alias_method :to_options, :symbolize_keys
40
- #alias_method :to_options!, :symbolize_keys!
41
- end
42
-
43
- module ActiveSupport
44
- class HashWithIndifferentAccess < Hash
45
- def extractable_options?
46
- true
47
- end
48
-
49
- def initialize(constructor = {})
50
- if constructor.is_a?(Hash)
51
- super()
52
- update(constructor)
53
- else
54
- super(constructor)
55
- end
56
- end
57
-
58
- def default(key = nil)
59
- if key.is_a?(Symbol) && include?(key = key.to_s)
60
- self[key]
61
- else
62
- super
63
- end
64
- end
65
-
66
- def self.new_from_hash_copying_default(hash)
67
- ActiveSupport::HashWithIndifferentAccess.new(hash).tap do |new_hash|
68
- new_hash.default = hash.default
69
- end
70
- end
71
-
72
- alias_method :regular_writer, :[]= unless method_defined?(:regular_writer)
73
- alias_method :regular_update, :update unless method_defined?(:regular_update)
74
-
75
- # Assigns a new value to the hash:
76
- #
77
- # hash = HashWithIndifferentAccess.new
78
- # hash[:key] = "value"
79
- #
80
- def []=(key, value)
81
- regular_writer(convert_key(key), convert_value(value))
82
- end
83
-
84
- # Updates the instantized hash with values from the second:
85
- #
86
- # hash_1 = HashWithIndifferentAccess.new
87
- # hash_1[:key] = "value"
88
- #
89
- # hash_2 = HashWithIndifferentAccess.new
90
- # hash_2[:key] = "New Value!"
91
- #
92
- # hash_1.update(hash_2) # => {"key"=>"New Value!"}
93
- #
94
- def update(other_hash)
95
- other_hash.each_pair { |key, value| regular_writer(convert_key(key), convert_value(value)) }
96
- self
97
- end
98
-
99
- alias_method :merge!, :update
100
-
101
- # Checks the hash for a key matching the argument passed in:
102
- #
103
- # hash = HashWithIndifferentAccess.new
104
- # hash["key"] = "value"
105
- # hash.key? :key # => true
106
- # hash.key? "key" # => true
107
- #
108
- def key?(key)
109
- super(convert_key(key))
110
- end
111
-
112
- alias_method :include?, :key?
113
- alias_method :has_key?, :key?
114
- alias_method :member?, :key?
115
-
116
- # Fetches the value for the specified key, same as doing hash[key]
117
- def fetch(key, *extras)
118
- super(convert_key(key), *extras)
119
- end
120
-
121
- # Returns an array of the values at the specified indices:
122
- #
123
- # hash = HashWithIndifferentAccess.new
124
- # hash[:a] = "x"
125
- # hash[:b] = "y"
126
- # hash.values_at("a", "b") # => ["x", "y"]
127
- #
128
- def values_at(*indices)
129
- indices.collect {|key| self[convert_key(key)]}
130
- end
131
-
132
- # Returns an exact copy of the hash.
133
- def dup
134
- HashWithIndifferentAccess.new(self)
135
- end
136
-
137
- # Merges the instantized and the specified hashes together, giving precedence to the values from the second hash
138
- # Does not overwrite the existing hash.
139
- def merge(hash)
140
- self.dup.update(hash)
141
- end
142
-
143
- # Performs the opposite of merge, with the keys and values from the first hash taking precedence over the second.
144
- # This overloaded definition prevents returning a regular hash, if reverse_merge is called on a HashWithDifferentAccess.
145
- def reverse_merge(other_hash)
146
- super self.class.new_from_hash_copying_default(other_hash)
147
- end
148
-
149
- def reverse_merge!(other_hash)
150
- replace(reverse_merge( other_hash ))
151
- end
152
-
153
- # Removes a specified key from the hash.
154
- def delete(key)
155
- super(convert_key(key))
156
- end
157
-
158
- def stringify_keys!; self end
159
- def stringify_keys; dup end
160
- def symbolize_keys; to_hash.symbolize_keys end
161
- def to_options!; self end
162
-
163
- # Convert to a Hash with String keys.
164
- def to_hash
165
- Hash.new(default).merge!(self)
166
- end
167
-
168
- protected
169
- def convert_key(key)
170
- key.kind_of?(Symbol) ? key.to_s : key
171
- end
172
-
173
- def convert_value(value)
174
- case value
175
- when Hash
176
- self.class.new_from_hash_copying_default(value)
177
- when Array
178
- value.collect { |e| e.is_a?(Hash) ? self.class.new_from_hash_copying_default(e) : e }
179
- else
180
- value
181
- end
182
- end
183
- end
184
- end
185
-
186
- HashWithIndifferentAccess = ActiveSupport::HashWithIndifferentAccess