greenwich 0.0.5 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,85 +0,0 @@
1
- require 'greenwich'
2
-
3
- module Greenwich #:nodoc:
4
- module Conversion #:nodoc:
5
- def self.included(base) #:nodoc:
6
- base.extend ClassMethods
7
- end
8
-
9
- module ClassMethods
10
- def time_with_custom_time_zone(name, options = {})
11
- time_zone_field = options[:time_zone] || Greenwich::Utilities.get_time_zone_field(name, column_names)
12
- time_field = options[:time_field] || Greenwich::Utilities.get_time_field(name, column_names)
13
-
14
- skip_time_zone_conversion_for_attributes << time_field
15
-
16
- mapping = [ [time_field, 'truncated_time_as_string'], [time_zone_field, 'time_zone_name'] ]
17
-
18
- composed_of name,
19
- :class_name => 'ActiveSupport::TimeWithZone',
20
- :mapping => mapping,
21
- :allow_nil => true,
22
- :constructor => Proc.new { |time, time_zone|
23
- time_zone = ActiveSupport::TimeZone.new(time_zone) unless time_zone.is_a? ActiveSupport::TimeZone
24
- time = time.to_time
25
-
26
- ActiveSupport::TimeWithZone.new(nil, time_zone, time)
27
- },
28
- :converter => Proc.new { |value|
29
- value[1] = Greenwich::Utilities.get_time_zone_from(value[1])
30
- value[0] = value[0].to_time
31
-
32
- ActiveSupport::TimeWithZone.new(nil, value[1], value[0])
33
- }
34
-
35
- define_method "#{time_field}=" do |time|
36
- instance_eval do
37
- write_attribute(time_field, time.to_s)
38
-
39
- time_zone = read_attribute(time_zone)
40
- self.send("#{name}=".to_sym, [time, time_zone]) if time && time_zone
41
- end
42
- end
43
-
44
- time_zone "#{name}_time_zone".to_sym, :for => name.to_sym if options[:time_zone] == time_zone_field
45
- end
46
-
47
- def time_with_static_time_zone(name, options = {})
48
- define_method "#{name}" do
49
- time_field = Greenwich::Utilities.get_time_field(name, self.class.column_names)
50
-
51
- instance_eval do
52
- time = send(time_field.to_sym)
53
- time_zone = Greenwich::Utilities.get_time_zone_from(send(options[:time_zone]))
54
-
55
- ActiveSupport::TimeWithZone.new(nil, time_zone, time)
56
- end
57
- end
58
- end
59
-
60
- def time_zone(name, options = {})
61
- options[:for] = [options[:for]].compact unless options[:for].is_a? Array
62
- options[:for].map! { |v| [v, Greenwich::Utilities.get_time_field(v, column_names)] }
63
-
64
- define_method "#{name}" do
65
- time_zone_name = read_attribute(name)
66
- ActiveSupport::TimeZone.new(time_zone_name) unless time_zone_name.nil?
67
- end
68
-
69
- define_method "#{name}=" do |time_zone_string|
70
- instance_eval do
71
- time_zone = Greenwich::Utilities.get_time_zone_from(time_zone_string).try(:name)
72
- write_attribute(name, time_zone)
73
-
74
- options[:for].each do |composed_field, time_field|
75
- time = read_attribute(time_field)
76
- self.send("#{composed_field}=".to_sym, [time, time_zone]) if time && time_zone
77
- end
78
- end
79
- end
80
- end
81
- end
82
- end
83
- end
84
-
85
- ActiveRecord::Base.send :include, Greenwich::Conversion
@@ -1,30 +0,0 @@
1
- module ActiveSupport
2
- class TimeWithZone
3
- def time_zone=(new_zone)
4
- ActiveSupport::TimeWithZone.new(nil, ActiveSupport::TimeZone.new(new_zone), time)
5
- end
6
-
7
- def time_zone_name
8
- time_zone.name
9
- end
10
-
11
- def truncated_time
12
- ActiveSupport::TimeWithZone.new(nil, ActiveSupport::TimeZone.new('UTC'), time)
13
- end
14
-
15
- def truncated_time_as_string
16
- truncated_time.to_s
17
- end
18
- end
19
-
20
- class TimeZone
21
- def to_s
22
- name
23
- end
24
-
25
- def freeze
26
- tzinfo; utc_offset;
27
- super
28
- end
29
- end
30
- end