Module: ByStar::Normalization
- Defined in:
- lib/by_star/normalization.rb
Class Method Summary collapse
- .extrapolate_year(value) ⇒ Object
- .fortnight(value, options = {}) ⇒ Object
- .fortnight_fixnum(value, options = {}) ⇒ Object
- .month(value, options = {}) ⇒ Object
- .month_fixnum(value, options = {}) ⇒ Object
- .quarter(value, options = {}) ⇒ Object
- .quarter_fixnum(value, options = {}) ⇒ Object
- .time(value) ⇒ Object
- .time_string(value) ⇒ Object
- .time_string_chronic(value) ⇒ Object
- .time_string_fallback(value) ⇒ Object
- .try_string_to_int(value) ⇒ Object
- .week(value, options = {}) ⇒ Object
- .week_fixnum(value, options = {}) ⇒ Object
- .year(value, options = {}) ⇒ Object
- .year_fixnum(value) ⇒ Object
Class Method Details
.extrapolate_year(value) ⇒ Object
100 101 102 103 104 105 106 107 108 109 |
# File 'lib/by_star/normalization.rb', line 100 def extrapolate_year(value) case value.to_i when 0..69 2000 + value when 70..99 1900 + value else value.to_i end end |
.fortnight(value, options = {}) ⇒ Object
45 46 47 48 49 50 51 |
# File 'lib/by_star/normalization.rb', line 45 def fortnight(value, ={}) value = try_string_to_int(value) case value when Fixnum then fortnight_fixnum(value, ) else time(value) end end |
.fortnight_fixnum(value, options = {}) ⇒ Object
53 54 55 56 57 |
# File 'lib/by_star/normalization.rb', line 53 def fortnight_fixnum(value, ={}) raise ParseError, 'Fortnight number must be between 0 and 26' unless value.in?(0..26) time = Time.zone.local([:year] || Time.zone.now.year) time + (value * 2).weeks end |
.month(value, options = {}) ⇒ Object
73 74 75 76 77 78 79 |
# File 'lib/by_star/normalization.rb', line 73 def month(value, ={}) value = try_string_to_int(value) case value when Fixnum, String then month_fixnum(value, ) else time(value) end end |
.month_fixnum(value, options = {}) ⇒ Object
81 82 83 84 85 86 |
# File 'lib/by_star/normalization.rb', line 81 def month_fixnum(value, ={}) year = [:year] || Time.zone.now.year Time.zone.parse "#{year}-#{value}-01" rescue raise ParseError, 'Month must be a number between 1 and 12 or a month name' end |
.quarter(value, options = {}) ⇒ Object
59 60 61 62 63 64 65 |
# File 'lib/by_star/normalization.rb', line 59 def quarter(value, ={}) value = try_string_to_int(value) case value when Fixnum then quarter_fixnum(value, ) else time(value) end end |
.quarter_fixnum(value, options = {}) ⇒ Object
67 68 69 70 71 |
# File 'lib/by_star/normalization.rb', line 67 def quarter_fixnum(value, ={}) raise ParseError, 'Quarter number must be between 1 and 4' unless value.in?(1..4) time = Time.zone.local([:year] || Time.zone.now.year) time.beginning_of_year + ((value - 1) * 3).months end |
.time(value) ⇒ Object
9 10 11 12 13 14 15 16 |
# File 'lib/by_star/normalization.rb', line 9 def time(value) case value when String then time_string(value) when DateTime then value.to_time when Date then value.to_time_in_current_zone else value end end |
.time_string(value) ⇒ Object
18 19 20 |
# File 'lib/by_star/normalization.rb', line 18 def time_string(value) defined?(Chronic) ? time_string_chronic(value) : time_string_fallback(value) end |
.time_string_chronic(value) ⇒ Object
22 23 24 25 |
# File 'lib/by_star/normalization.rb', line 22 def time_string_chronic(value) Chronic.time_class = Time.zone Chronic.parse(value) || raise(ByStar::ParseError, "Chronic could not parse String #{value.inspect}") end |
.time_string_fallback(value) ⇒ Object
27 28 29 |
# File 'lib/by_star/normalization.rb', line 27 def time_string_fallback(value) Time.zone.parse(value) || raise(ByStar::ParseError, "Cannot parse String #{value.inspect}") end |
.try_string_to_int(value) ⇒ Object
111 112 113 114 115 |
# File 'lib/by_star/normalization.rb', line 111 def try_string_to_int(value) value.is_a?(String) ? Integer(value) : value rescue value end |
.week(value, options = {}) ⇒ Object
31 32 33 34 35 36 37 |
# File 'lib/by_star/normalization.rb', line 31 def week(value, ={}) value = try_string_to_int(value) case value when Fixnum then week_fixnum(value, ) else time(value) end end |
.week_fixnum(value, options = {}) ⇒ Object
39 40 41 42 43 |
# File 'lib/by_star/normalization.rb', line 39 def week_fixnum(value, ={}) raise ParseError, 'Week number must be between 0 and 52' unless value.in?(0..52) time = Time.zone.local([:year] || Time.zone.now.year) time.beginning_of_year + value.to_i.weeks end |
.year(value, options = {}) ⇒ Object
88 89 90 91 92 93 94 |
# File 'lib/by_star/normalization.rb', line 88 def year(value, ={}) value = try_string_to_int(value) case value when Fixnum then year_fixnum(value) else time(value) end end |
.year_fixnum(value) ⇒ Object
96 97 98 |
# File 'lib/by_star/normalization.rb', line 96 def year_fixnum(value) Time.zone.local(extrapolate_year(value)) end |