Module: ByStar::Normalization
- Defined in:
- lib/by_star/normalization.rb
Class Method Summary collapse
- .apply_offset_end(time, offset) ⇒ Object
- .apply_offset_start(time, offset) ⇒ Object
- .cweek(value, options = {}) ⇒ Object
- .date(value) ⇒ Object
- .extract_range(args) ⇒ Object
- .extrapolate_year(value) ⇒ Object
- .fortnight(value, options = {}) ⇒ Object
- .fortnight_integer(value, options = {}) ⇒ Object
- .month(value, options = {}) ⇒ Object
- .month_integer(value, options = {}) ⇒ Object
- .quarter(value, options = {}) ⇒ Object
- .quarter_integer(value, options = {}) ⇒ Object
- .time(value) ⇒ Object
- .time_in_units(seconds) ⇒ Object
- .try_string_to_int(value) ⇒ Object
- .week(value, options = {}) ⇒ Object
- .week_integer(value, options = {}) ⇒ Object
- .year(value, options = {}) ⇒ Object
- .year_integer(value) ⇒ Object
Class Method Details
.apply_offset_end(time, offset) ⇒ Object
127 128 129 130 131 |
# File 'lib/by_star/normalization.rb', line 127 def apply_offset_end(time, offset) units = time_in_units(offset) time += units.delete(:days).days (time + 1.day).change(units) - 1.second end |
.apply_offset_start(time, offset) ⇒ Object
121 122 123 124 125 |
# File 'lib/by_star/normalization.rb', line 121 def apply_offset_start(time, offset) units = time_in_units(offset) time += units.delete(:days).days time.change(units) end |
.cweek(value, options = {}) ⇒ Object
34 35 36 37 38 39 40 41 |
# File 'lib/by_star/normalization.rb', line 34 def cweek(value, ={}) _value = value if _value.is_a?(Integer) raise ParseError, 'cweek number must be between 1 and 53' unless value.in?(1..53) _value -= 1 end week(_value, ) end |
.date(value) ⇒ Object
9 10 11 12 13 |
# File 'lib/by_star/normalization.rb', line 9 def date(value) value = parse_time(value) if value.is_a?(String) value = value.try(:in_time_zone) unless value.is_a?(Date) value.try(:to_date) end |
.extract_range(args) ⇒ Object
133 134 135 136 137 138 |
# File 'lib/by_star/normalization.rb', line 133 def extract_range(args) case args[0] when Array, Range then [args[0].first, args[0].last] else args[0..1] end end |
.extrapolate_year(value) ⇒ Object
98 99 100 101 102 103 104 105 106 107 |
# File 'lib/by_star/normalization.rb', line 98 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
43 44 45 46 47 48 49 |
# File 'lib/by_star/normalization.rb', line 43 def fortnight(value, ={}) value = try_string_to_int(value) case value when Integer then fortnight_integer(value, ) else date(value) end end |
.fortnight_integer(value, options = {}) ⇒ Object
51 52 53 54 55 |
# File 'lib/by_star/normalization.rb', line 51 def fortnight_integer(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
71 72 73 74 75 76 77 |
# File 'lib/by_star/normalization.rb', line 71 def month(value, ={}) value = try_string_to_int(value) case value when Integer, String then month_integer(value, ) else date(value) end end |
.month_integer(value, options = {}) ⇒ Object
79 80 81 82 83 84 |
# File 'lib/by_star/normalization.rb', line 79 def month_integer(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
57 58 59 60 61 62 63 |
# File 'lib/by_star/normalization.rb', line 57 def quarter(value, ={}) value = try_string_to_int(value) case value when Integer then quarter_integer(value, ) else date(value) end end |
.quarter_integer(value, options = {}) ⇒ Object
65 66 67 68 69 |
# File 'lib/by_star/normalization.rb', line 65 def quarter_integer(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
15 16 17 18 |
# File 'lib/by_star/normalization.rb', line 15 def time(value) value = parse_time(value) if value.is_a?(String) value.try(:in_time_zone) end |
.time_in_units(seconds) ⇒ Object
115 116 117 118 119 |
# File 'lib/by_star/normalization.rb', line 115 def time_in_units(seconds) days = seconds / 1.day time = Time.at(seconds).utc { days: days, hour: time.hour, min: time.min, sec: time.sec } end |
.try_string_to_int(value) ⇒ Object
109 110 111 112 113 |
# File 'lib/by_star/normalization.rb', line 109 def try_string_to_int(value) value.is_a?(String) ? Integer(value) : value rescue value end |
.week(value, options = {}) ⇒ Object
20 21 22 23 24 25 26 |
# File 'lib/by_star/normalization.rb', line 20 def week(value, ={}) value = try_string_to_int(value) case value when Integer then week_integer(value, ) else date(value) end end |
.week_integer(value, options = {}) ⇒ Object
28 29 30 31 32 |
# File 'lib/by_star/normalization.rb', line 28 def week_integer(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
86 87 88 89 90 91 92 |
# File 'lib/by_star/normalization.rb', line 86 def year(value, ={}) value = try_string_to_int(value) case value when Integer then year_integer(value) else date(value) end end |
.year_integer(value) ⇒ Object
94 95 96 |
# File 'lib/by_star/normalization.rb', line 94 def year_integer(value) Time.zone.local(extrapolate_year(value)) end |