Permalink
Browse files

check for method existance in a ruby 1.8- and 1.9-compatible way

  • Loading branch information...
1 parent f7bd386 commit ed6785b59ca76b4b70d6aef0053ff6fd1124521a @mislav mislav committed May 20, 2009
@@ -1,9 +1,20 @@
require 'set'
require 'will_paginate/array'
-## Everything below blatantly stolen from ActiveSupport :o
+# helper to check for method existance in ruby 1.8- and 1.9-compatible way
+# because `methods`, `instance_methods` and others return strings in 1.8 and symbols in 1.9
+#
+# ['foo', 'bar'].include_method?(:foo) # => true
+class Array
+ def include_method?(name)
+ name = name.to_sym
+ !!(find { |item| item.to_sym == name })
+ end
+end
+
+## everything below copied from ActiveSupport so we don't depend on it ##
-unless Hash.instance_methods.include? 'except'
+unless Hash.instance_methods.include_method? :except
Hash.class_eval do
# Returns a new hash without the given keys.
def except(*keys)
@@ -18,7 +29,7 @@ def except!(*keys)
end
end
-unless Hash.instance_methods.include? 'slice'
+unless Hash.instance_methods.include_method? :slice
Hash.class_eval do
# Returns a new hash with only the given keys.
def slice(*keys)
@@ -33,7 +44,7 @@ def slice!(*keys)
end
end
-unless String.instance_methods.include? 'constantize'
+unless String.instance_methods.include_method? :constantize
String.class_eval do
def constantize
unless /\A(?:::)?([A-Z]\w*(?:::[A-Z]\w*)*)\z/ =~ self
@@ -45,7 +56,7 @@ def constantize
end
end
-unless String.instance_methods.include? 'underscore'
+unless String.instance_methods.include_method? :underscore
String.class_eval do
def underscore
self.to_s.gsub(/::/, '/').
@@ -162,7 +162,7 @@ def wp_parse_count_options(options, klass) #:nodoc:
# forget about includes if they are irrelevant (Rails 2.1)
if count_options[:include] and
- klass.private_methods.include?('references_eager_loaded_tables?') and
+ klass.private_methods.include_method?(:references_eager_loaded_tables?) and
!klass.send(:references_eager_loaded_tables?, count_options)
count_options.delete :include
end
@@ -5,10 +5,10 @@
Sequel::Dataset::Pagination.module_eval do
# it should quack like a WillPaginate::Collection
- alias :total_pages :page_count unless existing_methods.include? 'total_pages'
- alias :per_page :page_size unless existing_methods.include? 'per_page'
- alias :previous_page :prev_page unless existing_methods.include? 'previous_page'
- alias :total_entries :pagination_record_count unless existing_methods.include? 'total_entries'
+ alias :total_pages :page_count unless existing_methods.include_method? :total_pages
+ alias :per_page :page_size unless existing_methods.include_method? :per_page
+ alias :previous_page :prev_page unless existing_methods.include_method? :previous_page
+ alias :total_entries :pagination_record_count unless existing_methods.include_method? :total_entries
def out_of_bounds?
current_page > total_pages
@@ -236,7 +236,7 @@ def self.column_names
end
# detect ActiveRecord 2.1
- if ActiveRecord::Base.private_methods.include?('references_eager_loaded_tables?')
+ if ActiveRecord::Base.private_methods.include_method?(:references_eager_loaded_tables?)
it "should remove :include for count" do
Developer.expects(:find).returns([1])
Developer.expects(:count).with({}).returns(0)

0 comments on commit ed6785b

Please sign in to comment.