Module: Weatherzone::Helpers::DateParser

Included in:
AlmanacPeriod, BuoyObservation, Image, NewsItem, Tide, Warning
Defined in:
lib/weatherzone/helpers/date_parser.rb

Class Method Summary collapse

Class Method Details

.included(klass) ⇒ 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
# File 'lib/weatherzone/helpers/date_parser.rb', line 4

def self.included(klass)
  klass.class_eval do

    def self.interpret_as_date(*methods)
      methods.each do |method_name|
        define_method method_name do
          if value = instance_variable_get("@#{method_name}")
            Date.parse(value)
          end
        end
      end
    end

    def self.interpret_as_time(*methods)
      methods.each do |method_name|
        has_attribute :tz, :on_elements => methods
        define_method "#{method_name}_utc" do
          if (tz_identifier = send("#{method_name}_tz")) && (value = instance_variable_get("@#{method_name}"))
            tz = TZInfo::Timezone.get("Australia/#{tz_identifier}")
            tz.local_to_utc(Time.parse(value))
          end
        end
        define_method method_name do
          if (tz_identifier = send("#{method_name}_tz")) && (value = send("#{method_name}_utc"))
            tz = TZInfo::Timezone.get("Australia/#{tz_identifier}")
            tz.utc_to_local value
          end
        end
      end
    end
    
  end
end