Skip to content

Instantly share code, notes, and snippets.

@oliverbarnes
Created August 1, 2011 18:34
Show Gist options
  • Save oliverbarnes/1118716 to your computer and use it in GitHub Desktop.
Save oliverbarnes/1118716 to your computer and use it in GitHub Desktop.
Quick opening hours object with mongomapper and a little meta
class OpeningHours
include MongoMapper::EmbeddedDocument
[:monday, :tuesday, :wednesday, :thursday, :friday, :saturday, :sunday].each do |weekday|
class_eval <<-KEYS_AND_ACCESSORS
key :#{weekday}_opening, :default => "CLOSED"
key :#{weekday}_closing
def #{weekday}
#{weekday}_opening == "CLOSED" ? #{weekday}_opening : \"\#\{#{weekday}_opening\} - \#\{#{weekday}_closing\}\"
end
KEYS_AND_ACCESSORS
end
end
class Store
include MongoMapper::Document
one :opening_hours
end
store = Store.create!
store.opening_hours.build
store.opening_hours.monday #=> "CLOSED"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment