Skip to content

Instantly share code, notes, and snippets.

@gbuesing
Forked from will/ugly couchrest hacks.rb
Created March 20, 2009 22:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gbuesing/82606 to your computer and use it in GitHub Desktop.
Save gbuesing/82606 to your computer and use it in GitHub Desktop.
Hash hacks so that CouchRest::Document plays nice with Rails routes
# Hacks so that CouchRest::Document, which descends from Hash,
# doesn't appear to Rails routing as a Hash of options
class Hash
def self.===(other)
return false if other.is_a?(CouchRest::Document)
super
end
end
module CouchRest
class Document < Hash
def is_a?(o)
return false if o == Hash
super
end
alias_method :kind_of?, :is_a?
end
end
class Model < CouchRest::Document; end
require 'test/unit'
class TestCouchRestHashHacks < Test::Unit::TestCase
def test_case_equality
assert_equal true, Hash === Hash.new
assert_equal false, Hash === Array.new
assert_equal false, Hash === CouchRest::Document.new
assert_equal false, Hash === Model.new
end
def test_is_a
assert_equal true, CouchRest::Document.new.is_a?(CouchRest::Document)
assert_equal true, Model.new.is_a?(CouchRest::Document)
assert_equal false, CouchRest::Document.new.is_a?(Hash)
assert_equal false, Model.new.is_a?(Hash)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment