Created
March 14, 2012 12:38
-
-
Save alvinsj/2036201 to your computer and use it in GitHub Desktop.
Mask days of week into binary, save to integer field
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
DAYS = %w[0 1 2 3 4 5 6 7] | |
DAYS_HUMAN = %w[Sun Mon Tue Wed Thur Fri Sat] | |
def days=(days) | |
self.days_mask = (days & DAYS).map { |r| 2**DAYS.index(r) }.sum | |
end | |
def days | |
DAYS.reject do |r| | |
((days_mask || 0) & 2**DAYS.index(r)).zero? | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment