Skip to content

Commit

Permalink
check for method existance in a ruby 1.8- and 1.9-compatible way
Browse files Browse the repository at this point in the history
  • Loading branch information
mislav committed May 20, 2009
1 parent f7bd386 commit ed6785b
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 11 deletions.
21 changes: 16 additions & 5 deletions lib/will_paginate/core_ext.rb
@@ -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)
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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(/::/, '/').
Expand Down
2 changes: 1 addition & 1 deletion lib/will_paginate/finders/active_record.rb
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions lib/will_paginate/finders/sequel.rb
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion spec/finders/active_record_spec.rb
Expand Up @@ -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)
Expand Down

0 comments on commit ed6785b

Please sign in to comment.