Skip to content

Instantly share code, notes, and snippets.

@wesmaldonado
Created February 5, 2010 22:41
Show Gist options
  • Save wesmaldonado/296342 to your computer and use it in GitHub Desktop.
Save wesmaldonado/296342 to your computer and use it in GitHub Desktop.
require 'yaml'
# From Rails 3 ActiveSupport I believe
class StringInquirer < String
def method_missing(method_name, *arguments)
if method_name.to_s[-1,1] == "?"
self == method_name.to_s[0..-2]
else
super
end
end
end
class BrandAds
def initialize(h)
@page_names = {}
h.each do |k,v|
next unless v.kind_of?(Hash)
next unless v.has_key?("positions") && v["positions"]
@page_names[k.upcase.to_sym] = AdSet.new(v["positions"])
end
end
def for(name)
return StringInquirer.new("") unless @page_names.has_key?(name.upcase.to_sym)
@page_names[name.upcase.to_sym]
end
end
class AdSet < Array
def initialize(*args)
super(*args)
self.map!{ |v| StringInquirer.new(v) }
self
end
def <<(v)
insert(0, StringInquirer.new(v))
end
alias push <<
alias unshift <<
def method_missing(method_name, *arguments)
return self.any?{ |v| v.send(method_name) }
end
end
brand_ads = BrandAds.new(YAML.load_file 'config/page_ads.yml')
puts "AdSet is #{brand_ads.for('NEIGHBORS_MORE_INFO_NEL').inspect} and it includes? top_rail positions? #{brand_ads.for('NEIGHBORS_MORE_INFO_NEL').top_rail?}"
#puts brand_ads.to_yaml
#cool_ad_set = AdSet.new(['top_rail','right_rail','header_text'])
#puts "This AdSet #{cool_ad_set.inspect} contains top_rail? #{cool_ad_set.top_rail?}"
# => This AdSet ["top_rail", "right_rail", "header_text"] contains top_rail? true
#puts "This AdSet #{cool_ad_set.inspect} contains random_thing? #{cool_ad_set.random_thing?}"
# => This AdSet ["top_rail", "right_rail", "header_text"] contains random_thing? false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment