Module: Chronological::RelativeTimeframe::ClassMethods

Defined in:
lib/chronological/relative_timeframe.rb

Instance Method Summary collapse

Instance Method Details

#relative_timeframe(options = {}) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/chronological/relative_timeframe.rb', line 4

def relative_timeframe(options = {})
  base_time_field        = options[:base_utc] || options[:base]
  base_time_field_is_utc = options.has_key? :base_utc
  time_field_utc_suffix  = base_time_field_is_utc ? 'utc' : nil

  start_time_field       = ['started_at', time_field_utc_suffix].compact.join('_')
  end_time_field         = ['ended_at',   time_field_utc_suffix].compact.join('_')
  start_date_field       = ['started_on', time_field_utc_suffix].compact.join('_')
  end_date_field         = ['ended_on',   time_field_utc_suffix].compact.join('_')

  class_eval do
    columns_hash[start_time_field] = ActiveRecord::ConnectionAdapters::Column.new(start_time_field, nil, "datetime")
    columns_hash[end_time_field]   = ActiveRecord::ConnectionAdapters::Column.new(end_time_field, nil, "datetime")
  end

  unless base_time_field_is_utc
    define_method("#{start_time_field}_utc") do
      return nil unless send(start_time_field).present?

      send(start_time_field).in_time_zone('UTC')
    end

    define_method("#{end_time_field}_utc") do
      return nil unless send(end_time_field).present?

      send(end_time_field).in_time_zone('UTC')
    end
  end

  define_method(start_time_field) do
    return nil unless send(base_time_field).present? && send(options[:start]).present?

    send(base_time_field) - send(options[:start])
  end

  define_method(end_time_field) do
    return nil unless send(base_time_field).present? && send(options[:end]).present?

    send(base_time_field) - send(options[:end])
  end

  define_method(:scheduled?) do
    send(base_time_field).present? && send(options[:start]).present? && send(options[:end]).present?
  end

  define_method(:partially_scheduled?) do
    send(base_time_field).present? || send(options[:start]).present? || send(options[:end]).present?
  end

  ###
  # Scopes
  #
  self.class.send(:define_method, :in_progress) do
    all.select(&:in_progress?)
  end

  self.class.send(:define_method, :in_progress?) do
    all.any?(&:in_progress?)
  end

  ###
  # Aliases
  #
  # Aliasing date methods to make code more readable
  instance_eval do
    alias active? in_progress?
    alias active  in_progress
  end

  base_timeframe  start_date_field: start_date_field,
                  start_time_field: start_time_field,
                  end_date_field:   end_date_field,
                  end_time_field:   end_time_field

private
  define_method(:has_absolute_timeframe?) do
    scheduled?
  end

  define_method(:duration_in_seconds) do
    return nil unless send(options[:start]).present? && send(options[:end]).present?

    send(options[:start]) - send(options[:end])
  end
end