Created
February 5, 2010 21:23
-
-
Save wesmaldonado/296259 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# 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 | |
## | |
# Creates an adset capable of answering about positions in the set as methods. | |
# ` AdSet.new("top_rail).top_rail? => true` | |
# | |
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 | |
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