Browse files

Draw out dynamic method creation to class definition to make response…

… object instantiation more efficient
  • Loading branch information...
1 parent f2bf79b commit ae944905abf26d039939d0668044e6b47e43c113 @jsl jsl committed May 26, 2009
Showing with 25 additions and 11 deletions.
  1. +5 −0 README.rdoc
  2. +16 −11 lib/myzofeedtosis/result.rb
  3. +4 −0 spec/myzofeedtosis/result_spec.rb
View
5 README.rdoc
@@ -131,6 +131,11 @@ Myzofeedtosis doesn't do anything about feed sanitizing, as other libraries have
been built for this purpose. FeedNormalizer has methods for escaping entries,
but to strip HTML I suggest that you look at the Ruby gem "sanitize".
+== Credits
+
+Thanks to Sander Hartlage (GitHub: Sander6) for useful feedback early in the
+development of Myzofeedtosis.
+
== Feedback
Please let me know if you have any problems with or questions about
View
27 lib/myzofeedtosis/result.rb
@@ -5,7 +5,7 @@ module Myzofeedtosis
# method calls to the correct object. If FeedNormalizer wasn't able to process
# the response, calls which would be delegated to this object return nil. In
# these cases, depending on your business logic you may want to inspect the
- # state of the Curl::Easy object.
+ # state of the Curl::Easy object by using methods forwarded to it.
class Result
# Methods which should be delegated to the FeedNormalizer::Feed object.
@@ -14,21 +14,26 @@ class Result
:new_entries, :channel, :ttl, :skip_hours, :skip_days
] unless defined?(FEED_METHODS)
+ # Precompiled regexp for detecting removing setter methods from collection
+ # of methods to be delegated to the Curl::Easy object.
+ SETTER_METHOD_RE = /=$/o unless defined?(SETTER_METHOD_RE)
+
def initialize(curl, feed)
@curl = curl
@feed = feed
raise ArgumentError, "Curl object must not be nil" if curl.nil?
-
- # See what the Curl::Easy object responds to, and send any appropriate
- # messages its way.
- @curl.public_methods(false).each do |meth|
- (class << self; self; end).class_eval do
- define_method meth do |*args|
- @curl.send(meth, *args)
- end
- end
- end
+ end
+
+ # See what the Curl::Easy object responds to, and send any appropriate
+ # messages its way. We don't worry about setter methods, since those
+ # aren't really useful to delegate.
+ Curl::Easy.instance_methods(false).reject do |m|
+ m =~ SETTER_METHOD_RE
+ end.each do |meth|
+ define_method meth do |*args|
+ @curl.send(meth, *args)
+ end
end
# Send methods through to the feed object unless it is nil. If feed
View
4 spec/myzofeedtosis/result_spec.rb
@@ -23,6 +23,10 @@
@r.body_str
end
+ it "should not respond to setter methods common in the Curl::Easy class" do
+ @r.should_not respond_to(:encoding=)
+ end
+
it "should return nil for author if the Feed is nil" do
r = Myzofeedtosis::Result.new(@c, nil)
r.author

0 comments on commit ae94490

Please sign in to comment.