Skip to content

Instantly share code, notes, and snippets.

@ephekt
Created March 1, 2012 20:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ephekt/1953043 to your computer and use it in GitHub Desktop.
Save ephekt/1953043 to your computer and use it in GitHub Desktop.
Rewrite this using ghost methods, and any other fun variations you wish
=begin
The task: Rewrite this class in multiple ways (at least 2). Currently, on class
instantiation a bunch of methods are defined. What are some other ways you could
have written this and what are the trade-offs?
I'll give you one: You can rewrite this using ghost methods (method_missing)
and dynamically handle the method call. There are multiple variations in which
you can write/handle the method call so pick one or two and give it a shot.
For each variation you should note a pro and a con to the solution. For example,
you could dynamically handle each request without defining a method, so this would
keep the class definitions limited and the code would be shorter, but if you call
the method twice in a row you have a performance hit...
Have some fun with it. (Note: There are some ruby tricks missing. Get those and get bonus points)
=end
require 'base64'
# Class: Advertising Targeting
# Purpose: Contains serialized data attributes that are used for targeting of an ad
# Assumptions: All capitalized method calls are requesting serialized data in the form of Base64 encoding.
class AdvertiserTargeting < ActiveRecord::Base
SERIALIZED_COLUMNS = [:Sizes, :Sections, :Languages, :Netspeeds, :InventoryTypes, :Publishers, :Urls, :ClickUrls, :Browsers,
:Isps, :Channels, :Rating, :Frequency, :Geography, :Gender, :Pixels
]
# a special little method that dynamically defines methods for serialized data columns
# and will return the unserialized data
SERIALIZED_COLUMNS.each do |method_name|
define_method method_name do
begin
Base64.decode64(read_attribute(method_name))
rescue
return nil
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment