Created
February 13, 2009 13:37
-
-
Save pauldix/63897 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Let's say we have a feed entry object | |
entry = feed.entries.first | |
# now I could provide access to sanitization a few ways | |
sanitized_content = Feedzirra::Feed.sanitize(entry.content) | |
# but that's verbose and ugly, so maybe I could do this | |
sanitized_content = entry.sanitized_content | |
# that looks better, but I still didn't like it for some reason. | |
# I could also do this | |
santitized_content = entry.sanitized(:title) | |
# and define the sanitized method in entry like so | |
def sanitized(method) | |
Dryopteris.sanitize(send(method)) | |
end | |
# However, what I really wanted was something like this | |
sanitized_content = entry.sanitized.content | |
# I think this reads a little better | |
# Here's the code inside of entry that makes this possible | |
def sanitized | |
dispatcher = Class.new do | |
def initialize(entry) | |
@entry = entry | |
end | |
def method_missing(method, *args) | |
Dryopteris.sanitize(@entry.send(method)) | |
end | |
end | |
dispatcher.new(self) | |
end | |
# So in the sanitized method I'm creating a new proxy class | |
# on the fly. I'm redefining method_missing to send | |
# it to the method straight back to the entry. | |
# The last is an initialization of this proxy object that | |
# passes the entry itself in so it can have methods called against | |
# it later. So now I can also do | |
entry.sanitized.author | |
entry.sanitized.title |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
that looks better, but I still didn't like it for some reason.
I could also do this
santitized_content = entry.sanitized(:title)
can you tell those reason?