Skip to content

Instantly share code, notes, and snippets.

@mauricio
Created January 29, 2015 04:20
Show Gist options
  • Save mauricio/099939251102a3916139 to your computer and use it in GitHub Desktop.
Save mauricio/099939251102a3916139 to your computer and use it in GitHub Desktop.
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