Module: ByStar::Normalization

Defined in:
lib/by_star/normalization.rb

Class Method Summary collapse

Class Method Details

.cweek(value, options = {}) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/by_star/normalization.rb', line 45

def cweek(value, options={})
  _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, options)
end

.extrapolate_year(value) ⇒ Object



109
110
111
112
113
114
115
116
117
118
# File 'lib/by_star/normalization.rb', line 109

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



54
55
56
57
58
59
60
# File 'lib/by_star/normalization.rb', line 54

def fortnight(value, options={})
  value = try_string_to_int(value)
  case value
    when Integer then fortnight_integer(value, options)
    else time(value)
  end
end

.fortnight_integer(value, options = {}) ⇒ Object

Raises:



62
63
64
65
66
# File 'lib/by_star/normalization.rb', line 62

def fortnight_integer(value, options={})
  raise ParseError, 'Fortnight number must be between 0 and 26' unless value.in?(0..26)
  time = Time.zone.local(options[:year] || Time.zone.now.year)
  time + (value * 2).weeks
end

.month(value, options = {}) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/by_star/normalization.rb', line 82

def month(value, options={})
  value = try_string_to_int(value)
  case value
    when Integer, String then month_integer(value, options)
    else time(value)
  end
end

.month_integer(value, options = {}) ⇒ Object



90
91
92
93
94
95
# File 'lib/by_star/normalization.rb', line 90

def month_integer(value, options={})
  year = options[: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



68
69
70
71
72
73
74
# File 'lib/by_star/normalization.rb', line 68

def quarter(value, options={})
  value = try_string_to_int(value)
  case value
    when Integer then quarter_integer(value, options)
    else time(value)
  end
end

.quarter_integer(value, options = {}) ⇒ Object

Raises:



76
77
78
79
80
# File 'lib/by_star/normalization.rb', line 76

def quarter_integer(value, options={})
  raise ParseError, 'Quarter number must be between 1 and 4' unless value.in?(1..4)
  time = Time.zone.local(options[: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.in_time_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



120
121
122
123
124
# File 'lib/by_star/normalization.rb', line 120

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, options={})
  value = try_string_to_int(value)
  case value
    when Integer then week_integer(value, options)
    else time(value)
  end
end

.week_integer(value, options = {}) ⇒ Object

Raises:



39
40
41
42
43
# File 'lib/by_star/normalization.rb', line 39

def week_integer(value, options={})
  raise ParseError, 'Week number must be between 0 and 52' unless value.in?(0..52)
  time = Time.zone.local(options[:year] || Time.zone.now.year)
  time.beginning_of_year + value.to_i.weeks
end

.year(value, options = {}) ⇒ Object



97
98
99
100
101
102
103
# File 'lib/by_star/normalization.rb', line 97

def year(value, options={})
  value = try_string_to_int(value)
  case value
    when Integer then year_integer(value)
    else time(value)
  end
end

.year_integer(value) ⇒ Object



105
106
107
# File 'lib/by_star/normalization.rb', line 105

def year_integer(value)
  Time.zone.local(extrapolate_year(value))
end