Module: Timeframeable

Defined in:
lib/timeframeable.rb,
lib/timeframeable/scope.rb,
lib/timeframeable/version.rb,
lib/timeframeable/controller.rb

Defined Under Namespace

Modules: Controller, Scope Classes: Timeframe

Constant Summary collapse

VERSION =
'0.1.0'

Class Method Summary collapse

Class Method Details

.parse_date(param, extrapolate = nil) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/timeframeable.rb', line 16

def self.parse_date(param, extrapolate=nil)
  return unless param

  if param.kind_of? Hash
    parts = [:year, :month, :day].map do |key|
      value = param[key].to_i
      if value == 0
        value = case key
          when :year
            Date.today.year
          else
            1
        end
      end
      value
    end

    begin
      result = DateTime.new(*parts)
    rescue
      return
    end

    case extrapolate
      when :end
        if param[:day].to_i == 0
          result = result.end_of_month
        end
        result = result.end_of_day
      else
    end

    result.utc
  else
    begin
      DateTime.parse(param.to_s).utc
    rescue
      return
    end
  end
end