Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 6657

Show
Ignore:
Timestamp:
05/04/07 20:07:37 (1 year ago)
Author:
xal
Message:

Make respond_to? work as expected

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activeresource/lib/active_resource/base.rb

    r6653 r6657  
    317317      self 
    318318    end 
     319     
     320    # For checking respond_to? without searching the attributes (which is faster). 
     321    alias_method :respond_to_without_attributes?, :respond_to? 
     322 
     323    # A Person object with a name attribute can ask person.respond_to?("name"), person.respond_to?("name="), and 
     324    # person.respond_to?("name?") which will all return true. 
     325    def respond_to?(method, include_priv = false) 
     326      method_name = method.to_s 
     327      if attributes.nil? 
     328        return super 
     329      elsif attributes.has_key?(method_name) 
     330        return true  
     331      elsif ['?','='].include?(method_name.last) && attributes.has_key?(method_name.first(-1)) 
     332        return true 
     333      end 
     334      # super must be called at the end of the method, because the inherited respond_to? 
     335      # would return true for generated readers, even if the attribute wasn't present 
     336      super 
     337    end 
     338     
    319339 
    320340    protected 
  • trunk/activeresource/test/base_test.rb

    r6646 r6657  
    179179    assert_equal "Matz", matz.name 
    180180  end 
     181   
     182  def test_respond_to 
     183    matz = Person.find(1) 
     184    assert matz.respond_to?(:name) 
     185    assert matz.respond_to?(:name=) 
     186    assert matz.respond_to?(:name?) 
     187    assert !matz.respond_to?(:java) 
     188  end 
    181189 
    182190  def test_find_by_id_with_custom_prefix