Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Don't call super when Mash doesn't have id or type #57

Merged
merged 3 commits into from

5 participants

@steved

Ruby 1.8 has an Object#id method that is called when the
Mash doesn't have an id key. This causes unexpected failures since
there is no id key-value, but Mash is still returning a value. If the
object_id is needed just use #object_id or #__id__

(Same with type -> use class)

@steved steved don't try and call Object#id when calling Mash#id
Ruby 1.8 has an Object#id method that is called when the
Mash doesn't have an id key. This causes unexpected failures since
there is no id key-value, but Mash is still returning a value. If the
object_id is needed just use Object#object_id or Object#__id__

(Same with type -> class)
d433d8b
@etiennebarrie

Same problem here, with object_id this time (using 1.2.0).

Hashie::Mash.new('object_id' => 42).object_id == 42 # => false

The Instagram API for example uses object_id: http://instagram.com/developer/realtime/.
Simplest workaround is using mash[:object_id].

@steved

Added object_id exception as well since __id__ is still usable.

@jch

@steved555 this looks great! Thanks for the pull and the easy to understand tests. :beers:

@grosser

@intridea can we get this merged ?

@mbleigh mbleigh merged commit f7adbf6 into intridea:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Sep 1, 2012
  1. @steved

    don't try and call Object#id when calling Mash#id

    steved authored
    Ruby 1.8 has an Object#id method that is called when the
    Mash doesn't have an id key. This causes unexpected failures since
    there is no id key-value, but Mash is still returning a value. If the
    object_id is needed just use Object#object_id or Object#__id__
    
    (Same with type -> class)
Commits on Sep 4, 2012
  1. @steved
  2. @steved
This page is out of date. Refresh to see the latest.
Showing with 29 additions and 7 deletions.
  1. +6 −2 lib/hashie/mash.rb
  2. +23 −5 spec/hashie/mash_spec.rb
View
8 lib/hashie/mash.rb
@@ -70,11 +70,15 @@ def initialize(source_hash = nil, default = nil, &blk)
class << self; alias [] new; end
def id #:nodoc:
- key?("id") ? self["id"] : super
+ self["id"]
end
def type #:nodoc:
- key?("type") ? self["type"] : super
+ self["type"]
+ end
+
+ def object_id #:nodoc:
+ self["object_id"]
end
alias_method :regular_reader, :[]
View
28 spec/hashie/mash_spec.rb
@@ -97,10 +97,28 @@
@mash.author.should be_nil
end
+ it "should not call super if object_id is not a key" do
+ @mash.object_id.should == nil
+ end
+
+ it "should return the value if object_id is a key" do
+ @mash.object_id = "Steve"
+ @mash.object_id.should == "Steve"
+ end
- # it "should call super if type is not a key" do
- # @mash.type.should == Hashie::Mash
- # end
+
+ it "should not call super if id is not a key" do
+ @mash.id.should == nil
+ end
+
+ it "should return the value if id is a key" do
+ @mash.id = "Steve"
+ @mash.id.should == "Steve"
+ end
+
+ it "should not call super if type is not a key" do
+ @mash.type.should == nil
+ end
it "should return the value if type is a key" do
@mash.type = "Steve"
@@ -283,11 +301,11 @@ class SubMash < Hashie::Mash
initial = Hashie::Mash.new(:name => 'randy', :address => {:state => 'TX'})
copy = Hashie::Mash.new(initial)
initial.name.should == copy.name
- initial.object_id.should_not == copy.object_id
+ initial.__id__.should_not == copy.__id__
copy.address.state.should == 'TX'
copy.address.state = 'MI'
initial.address.state.should == 'TX'
- copy.address.object_id.should_not == initial.address.object_id
+ copy.address.__id__.should_not == initial.address.__id__
end
it "should accept a default block" do
Something went wrong with that request. Please try again.