Skip to content

Instantly share code, notes, and snippets.

@pauldix
Created February 13, 2009 13:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pauldix/63897 to your computer and use it in GitHub Desktop.
Save pauldix/63897 to your computer and use it in GitHub Desktop.
# 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
@iamacourser
Copy link

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment