Created
January 29, 2015 04:20
-
-
Save mauricio/099939251102a3916139 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
module OnSomething | |
def self.included(base) | |
base.extend(ClassMethods) | |
end | |
module ClassMethods | |
def on(type) | |
attr_accessor "#{type}_starts_at" | |
attr_accessor "#{type}_ends_at" | |
class_eval(%Q! | |
def on_#{type}? | |
Time.now >= #{type}_starts_at && Time.now <= #{type}_ends_at | |
end | |
!) | |
end | |
end | |
end | |
class Sale | |
include OnSomething | |
on :sale | |
end | |
sale = Sale.new | |
sale.sale_starts_at = Time.now | |
sale.sale_ends_at = Time.now + 10 | |
puts sale.on_sale? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment