Skip to content
This repository
Browse code

Refactor to return a boolean for query? methods and implement respond…

…_to?
  • Loading branch information...
commit dbd9feddd5ef06da6dc9ca60221077820dc6cb48 1 parent 2451d17
Blake Thomson thomsbg authored committed

Showing 2 changed files with 34 additions and 11 deletions. Show diff stats Hide diff stats

  1. +13 11 lib/her/model/orm.rb
  2. +21 0 spec/model/orm_spec.rb
24 lib/her/model/orm.rb
@@ -29,21 +29,23 @@ def self.initialize_collection(klass, parsed_data={}) # {{{
29 29
30 30 # Handles missing methods by routing them through @data
31 31 # @private
32   - def method_missing(method, attrs=nil) # {{{
33   - assignment_method = method.to_s =~ /\=$/
34   - method = method.to_s.gsub(/(\?|\!|\=)$/, "").to_sym
35   - if !attrs.nil? and assignment_method
36   - @data ||= {}
37   - @data[method.to_s.gsub(/\=$/, "").to_sym] = attrs
  32 + def method_missing(method, *args, &blk) # {{{
  33 + if method.to_s.end_with?('=')
  34 + @data[method.to_s.chomp('=').to_sym] = args.first
  35 + elsif method.to_s.end_with?('?')
  36 + @data.include?(method.to_s.chomp('?').to_sym)
  37 + elsif @data.include?(method)
  38 + @data[method]
38 39 else
39   - if @data and @data.include?(method)
40   - @data[method]
41   - else
42   - super
43   - end
  40 + super
44 41 end
45 42 end # }}}
46 43
  44 + # Handles returning true for the cases handled by method_missing
  45 + def respond_to?(method, include_private = false) # {{{
  46 + method.to_s.end_with?('=') || method.to_s.end_with?('?') || @data.include?(method) || super
  47 + end # }}}
  48 +
47 49 # Override the method to prevent from returning the object ID (in ruby-1.8.7)
48 50 # @private
49 51 def id # {{{
21 spec/model/orm_spec.rb
@@ -59,6 +59,27 @@
59 59 @new_user = Foo::User.new
60 60 expect { @new_user.fullname = "Tobias Fünke" }.to_not raise_error(NoMethodError)
61 61 end# }}}
  62 +
  63 + it "handles method missing for query" do#{{{
  64 + @new_user = Foo::User.new
  65 + expect { @new_user.fullname? }.to_not raise_error(NoMethodError)
  66 + end# }}}
  67 +
  68 + it "handles respond_to for getter" do#{{{
  69 + @new_user = Foo::User.new(:fullname => 'Mayonegg')
  70 + @new_user.should_not respond_to(:unknown_method_for_a_user)
  71 + @new_user.should respond_to(:fullname)
  72 + end#}}}
  73 +
  74 + it "handles respond_to for setter" do#{{{
  75 + @new_user = Foo::User.new
  76 + @new_user.should respond_to(:fullname=)
  77 + end#}}}
  78 +
  79 + it "handles respond_to for query" do#{{{
  80 + @new_user = Foo::User.new
  81 + @new_user.should respond_to(:fullname?)
  82 + end#}}}
62 83 end
63 84
64 85 context "mapping data, metadata and error data to Ruby objects" do

0 comments on commit dbd9fed

Please sign in to comment.
Something went wrong with that request. Please try again.