Created
June 2, 2012 17:29
-
-
Save lukelex/2859271 to your computer and use it in GitHub Desktop.
WeekDays Metaprogrammed example
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
| class Week | |
| WEEKDAYS = %w( :segunda_feira, :terca_feira, :quarta_feira, :quinta_feira, :sexta_feira ) | |
| def custom_attr_accessor(attr) | |
| self.class_eval("def #{attr};@#{attr};end") | |
| self.class_eval("def #{attr}=(val);@#{attr}=val;end") | |
| end | |
| WEEKDAYS.each do |day| | |
| custom_attr_accessor day | |
| end | |
| def initialize(args) | |
| args.each do |key, value| | |
| self.send(key, value) | |
| end | |
| end | |
| def dias_true | |
| "[" + WEEKDAYS.select { |day| self.send(day) == true }.join(",") + "]" | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment