Commit Graph

47 Commits

Author SHA1 Message Date
Arthur Neves a94966ea09
Fix OrderedHash.select to return self instance.
On ruby 2.1.1 the behavior of .select and .reject has changed.
They will return a Hash new instance, so we need to override them to
keep the instance object class.
2014-03-07 12:30:20 -05:00
Godfrey Chan 64c88fb5d2 Moved all JSON core extensions into core_ext/object/json
TL;DR The primary driver is to remove autoload surprise.

This is related to #12106. (The root cause for that ticket is that
json/add defines Regexp#to_json among others, but here I'll reproduce
the problem without json/add.)

Before:

   >> require 'active_support/core_ext/to_json'
   => true
   >> //.as_json
   NoMethodError: undefined method `as_json' for //:Regexp
     from (irb):3
     from /Users/godfrey/.rvm/rubies/ruby-2.0.0-p195/bin/irb:16:in `<main>'
   >> //.to_json
   => "\"(?-mix:)\""
   >> //.as_json
   => "(?-mix:)"

After:

   >> require 'active_support/core_ext/to_json'
   => true
   >> //.as_json
   => "(?-mix:)"

This is because ActiveSupport::JSON is autoloaded the first time
Object#to_json is called, which causes additional core extentions
(previously defined in active_support/json/encoding.rb) to be loaded.

When someone require 'active_support/core_ext', the expectation is
that it would add certain methods to the core classes NOW. The
previous behaviour causes additional methods to be loaded the first
time you call `to_json`, which could cause nasty surprises and other
unplesant side-effects.

This change moves all core extensions in to core_ext/json. AS::JSON is
still autoloaded on first #to_json call, but since it nolonger
include the core extensions, it should address the aforementioned bug.

*Requiring core_ext/object/to_json now causes a deprecation warnning*
2013-09-13 03:04:10 -07:00
Vipul A M afdf279e2f refactor order hash test 2013-04-11 21:55:45 +05:30
Akira Matsuda 9ec14c223b Missing require extract_options 2013-01-31 15:56:47 +09:00
Jeremy Kemper b9999c58c2 Ruby 2 compat. Hash[] now raises on bad elements rather than ignoring them. No sense over-testing this MRI-specific behavior. See ruby/ruby@8d6add973e 2012-10-06 20:57:22 -07:00
John Firebaugh e11348063b Don't test language-level exception messages
Ruby implementations should be free to produce exception
messages that are not identical to MRI. For example,
Rubinius produces 'Expected an even number, got 5'.
2012-07-27 12:51:12 -07:00
Aaron Patterson b8d8c50785 use AS::TestCase as the base class 2012-01-05 17:12:46 -08:00
Rafael Mendonça França 7d26fad384 No need to require psych since require yaml does that. 2012-01-04 14:29:13 -03:00
Rafael Mendonça França 761b049b2e No need to use rescue block to require psych 2012-01-04 13:30:57 -03:00
José Valim 7ab4775106 Initial pass at removing dead 1.8.x code from Active Support.
There are a bunch of other implicit branches that adds
1.8.x specific code that still needs to be removed. Pull
requests for those cases are welcome.
2011-12-20 18:22:21 +01:00
Prem Sichanugrist 9984266c04 Make `ActiveSupport::OrderedHash` extractable when using `Array#extract_options!`
`ActiveSupport::OrderedHash` is actually a subclass of the hash, so it does make sense that it should be extractable from the array list.
2011-09-03 14:43:33 +07:00
Andrew Radev c9cc36bde9 Fixes minor ruby 1.8 inconsistency
ActiveSupport::OrderedHash did not behave identically to Hash when given
a block with a splat.
2011-05-27 14:04:39 +03:00
Santiago Pastorino c53d3929cd Add missing requires 2011-05-11 13:05:04 -03:00
David Lee 099eb2b3fd indifferent access should recurse Hash subclasses
This commit makes Hash subclasses convert to HWIA by default for nested
objects of subclasses of Hash, but allows certain subclasses to prevent nested
conversion by introducing Hash#nested_under_indifferent_access that subclasses
can overwrite.

ActiveSupport::OrderedHash is one such subclass that overwrites
+nested_under_indifferent_access+, since implicitly converting it to HWIA would
remove the ordering of keys and values in Ruby 1.8.

This change is necessary because commit ce9456e broke nested indifferent access
conversion for all subclasses of Hash.
2011-05-08 03:40:51 -07:00
Santiago Pastorino 092a4e296d just < 1.9 is fine and 1.9.1 is not supported 2011-02-03 19:27:43 -02:00
Gabriel Horner c0b4db0c28 fix OrderedHash#each* methods to return Enumerators when called without a block [#6366 state:resolved]
Signed-off-by: Santiago Pastorino <santiago@wyeworks.com>
2011-02-03 19:26:36 -02:00
Gabriel Horner 9a0f43d3c1 OrderedHash#each* methods return self like Hash does [#6364 state:resolved]
Signed-off-by: Santiago Pastorino <santiago@wyeworks.com>
2011-02-02 13:40:36 -02:00
Aaron Patterson 4fabad7cf1 test that the custom ordered hash can be round-tripped 2011-01-04 15:07:15 -08:00
Aaron Patterson b1dc9c004e use dots for method calls 2011-01-04 15:07:15 -08:00
James A. Rosen 697f4851b8 OrderedHash#select now preserves order [#5843 state:resolved]
Signed-off-by: Santiago Pastorino <santiago@wyeworks.com>
2010-11-07 17:27:07 -02:00
Thiago Pradi 3c9bf6e1dc Exception handling more readable
[#5601 state:committed]

Signed-off-by: Santiago Pastorino <santiago@wyeworks.com>
2010-09-19 12:18:51 -07:00
Santiago Pastorino e1d4e78b15 Removes unused vars
Signed-off-by: José Valim <jose.valim@gmail.com>
2010-07-24 01:19:44 +02:00
Santiago Pastorino 4a0c514eb4 AS json refactor, move to_json implementation to core_ext and a cleanup a bit the code 2010-07-01 12:04:11 -07:00
José Valim 3782010377 Oops. Make previous commit pass on 1.9.2. 2010-06-26 12:14:25 +02:00
José Valim aa48ab05f4 Tidy up tests in previous commit since they did not assure an OrderedHash is returned (the test would pass for an array and would pass by chance for hashes).
[#4875 state:resolved]
2010-06-26 12:09:56 +02:00
chaitanyav 9958950f78 Add OrderedHash#invert to preserve order in ruby 1.8 [#4875]
Signed-off-by: José Valim <jose.valim@gmail.com>
2010-06-26 12:05:25 +02:00
José Valim 7bd85a8fc2 Work around the fact the JSON gem was overwriting to_json implementation for all Ruby core classes.
This is required because the JSON gem is incompatible with Rails behavior and was not allowing ActiveModel::Errors to be serialized.
So we need to ensure Rails implementation is the one triggered. [#4890 state:resolved]
2010-06-26 12:01:13 +02:00
Paul Mucur 158e22dae0 Alias ActiveSupport::OrderedHash#update to ActiveSupport::OrderedHash.merge!
This ensures that an OrderedHash's keys are set up appropriately when using update.

[#4973 state:committed]

Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
2010-06-25 15:02:20 -07:00
Xavier Noria 36143d26cb revises implementation of AS::OrderedHash#merge! 2010-06-13 06:37:54 +02:00
Xavier Noria 3359af63a5 Revert "hash merging with a block ignores non-existing keys altogether"
This reverts commit 72f9fec607.

It is wrong, this feature is a little undocumented, doing some research.
2010-06-13 05:53:42 +02:00
Xavier Noria 72f9fec607 hash merging with a block ignores non-existing keys altogether 2010-06-13 05:09:31 +02:00
Paul Barry 9183eaebed re-organized test for merge into separate tests
Signed-off-by: Xavier Noria <fxn@hashref.com>
2010-06-13 04:19:35 +02:00
Paul Mucur 58e21a4a0d Support passing a block to ActiveSupport::OrderedHash's merge and merge! [#4838 state:committed]
For better consistency with Ruby's own Hash implementation.

Signed-off-by: Xavier Noria <fxn@hashref.com>
2010-06-13 04:19:35 +02:00
Gregor Schmidt 7b8c6472ff Adding custom yaml (de-)serialization for OrderedHash
[#3608 state:committed]

Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
2010-01-27 15:29:06 -08:00
Chris Hapgood 6c59e5a558 Fix OrderedHash#replace
Signed-off-by: Michael Koziarski <michael@koziarski.com>
2009-11-05 21:17:13 +13:00
Brian Abreu d036899717 Fixed ActiveSupport::OrderedHash::[] work identically to ::Hash::[] in ruby 1.8.7 [#2832 state:resolved]
Signed-off-by: Yehuda Katz + Carl Lerche <ykatz+clerche@engineyard.com>
2009-07-02 12:01:05 -07:00
Douglas F Shearer e1854e0b19 ActiveSupport::OrderedHash[1,2,3,4] creates an OrderedHash instead of a Hash.
[#2615 state:committed]

Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
2009-05-11 11:45:57 -07:00
Ken Collins 9e0cfdb7f9 ActiveSupport::OrderedHash#to_a method returns an ordered set of arrays. Matches ruby1.9's Hash#to_a.
Signed-off-by: Michael Koziarski <michael@koziarski.com>
[#2629 state:committed]
2009-05-10 13:04:46 +12:00
Jeremy Kemper 9d13b9eed8 Fix OrderedHash#inspect recursion 2009-03-04 16:21:44 -08:00
Jeremy Kemper cb53452438 Fix OrderedHash#to_hash to return self instead of a new hash with self as default value 2009-03-03 19:10:33 -08:00
Brandon Keepers 452cd74d81 Dup keys in OrderedHash to prevent them from being modified [#1676 state:resolved]
Signed-off-by: Frederick Cheung <frederick.cheung@gmail.com>
2009-01-16 17:23:43 +00:00
Frederick Cheung 4dcd8f01af Make delete_if/reject faster and fix other mutators
[#1559 state:committed]

Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
2008-12-15 15:34:29 +01:00
Eloy Duran 7394d12dc7 Fixed ActiveSupport::OrderedHash #delete_if, #reject!, and #reject, which did not sync the @keys after the operation.
This probably holds true for other mutating methods as well.

Signed-off-by: Michael Koziarski <michael@koziarski.com>
2008-12-11 14:32:20 +01:00
Frederick Cheung 355f41d8aa Rework ActiveSupport::OrderedHash to make lookups faster
[#1352 state:committed]

Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
2008-12-10 09:07:47 -08:00
Christoffer Sawicki 05a938c5f7 Added ActiveSupport::OrderedHash#each_key and ActiveSupport::OrderedHash#each_value [#1410 state:resolved]
Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
2008-11-26 14:54:35 +01:00
Steve Purcell 7cfa6ec8a3 Add more standard Hash methods to ActiveSupport::OrderedHash [#314 state:resolved]
Signed-off-by: Joshua Peek <josh@joshpeek.com>
2008-06-03 14:15:33 -05:00
Joshua Peek 49846f8586 Create a seperate file for ActiveSupport::OrderedHash. 2008-05-14 12:48:37 -05:00