Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module TimeSet
- module TwentyFourHour
- def self.included(klass)
- klass.class_eval do
- extend ClassMethods
- include InstanceMethods
- end
- end
- module ClassMethods
- attr :times_for
- def twenty_four_hour_time_for(*args)
- @times_for = args
- args.each do |attribute|
- attr_writer "#{attribute}_hour", "#{attribute}_minute", "#{attribute}_meridian"
- define_method "#{attribute}_hour" do
- time = eval("#{attribute}") ? eval("#{attribute}") : Time.now
- (time.hour > 12 ? time.hour - 12 : time.hour).to_s
- end
- define_method "#{attribute}_minute" do
- eval("#{attribute}") ? eval("#{attribute}").min.to_s : "00"
- end
- define_method "#{attribute}_meridian" do
- if eval("#{attribute}")
- eval("#{attribute}").hour < 12 ? "AM" : "PM"
- else
- Time.now.hour < 12 ? "AM" : "PM"
- end
- end
- end
- end
- end
- module InstanceMethods
- def there_are_times_for
- self.class.times_for
- end
- end
- end
- end
- class SomeClass
- include TimeSet::TwentyFourHour
- twenty_four_hour_time_for :start, :end
- end
- class OtherClass
- include TimeSet::TwentyFourHour
- twenty_four_hour_time_for :commencement, :finality
- end
- x = SomeClass.new
- y = OtherClass.new
- puts x.there_are_times_for
- puts y.there_are_times_for
- # =>
- # start
- # end
- # commencement
- # finality
Add Comment
Please, Sign In to add comment