Created
February 21, 2017 08:56
-
-
Save sirdsoriano/3677c8793a22104083f55aead99e3527 to your computer and use it in GitHub Desktop.
Time Calculations Abbreviations in a hash, for select boxes
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
| # Use cases: TimeCalcAbbrv::ABBREVIATIONS['2d'].since, TimeCalcAbbrv::ABBREVIATIONS['3m'].ago | |
| module TimeCalcAbbrv | |
| def self.d(n) | |
| n.days | |
| end | |
| def self.w(n) | |
| n.weeks | |
| end | |
| def self.s(n) | |
| n.weeks | |
| end | |
| def self.m(n) | |
| n.months | |
| end | |
| def self.a(n) | |
| n.years | |
| end | |
| def self.y(n) | |
| n.years | |
| end | |
| def self.abbrv_method_name(_number,_period) | |
| "#{_number}#{_period}" | |
| end | |
| class AbbrevHash < Hash | |
| def [](p1) | |
| v = super(p1) | |
| v.call() unless v.nil? | |
| end | |
| end | |
| ABBREVIATIONS = ['1','2','3','4','6','12'].product(['d','w','s','m','a','y']).inject(AbbrevHash.new) do |result, number_period | | |
| _number = number_period[0].to_i | |
| _period = number_period[1] | |
| result[TimeCalcAbbrv.abbrv_method_name( _number, _period)] = Proc.new { TimeCalcAbbrv.send(_period.to_sym,_number) } | |
| result | |
| end.freeze | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment