zoneless_time 0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README ADDED
@@ -0,0 +1,13 @@
1
+ ZonelessTime
2
+ ============
3
+
4
+ Introduction goes here.
5
+
6
+
7
+ Example
8
+ =======
9
+
10
+ Example goes here.
11
+
12
+
13
+ Copyright (c) 2011 Shaun Mangelsdorf, released under the MIT license
@@ -0,0 +1,22 @@
1
+ module ActiveRecord
2
+ module ZonelessTimeSupport
3
+ def zoneless_time(sym)
4
+ instance_eval do
5
+ self.composed_of sym, :class_name => 'ZonelessTime::TimeWithoutZone',
6
+ :mapping => [[sym, 'time']], :constructor => :at
7
+ self.skip_time_zone_conversion_for_attributes << sym
8
+ define_method "#{sym}=" do |val|
9
+ if val.is_a? ZonelessTime::TimeWithoutZone
10
+ super val
11
+ else
12
+ super ZonelessTime::TimeWithoutZone.at(val)
13
+ end
14
+ end
15
+ end
16
+
17
+ if time_zone_aware_attributes
18
+ require 'zoneless_time/warning'
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,10 @@
1
+ module ZonelessTime
2
+ end
3
+
4
+ require 'active_record/zoneless_time_support'
5
+ require 'zoneless_time/time_without_zone'
6
+ require 'zoneless_time/equality_with_zoneless_time'
7
+
8
+ Time.send :include, ZonelessTime::EqualityWithZonelessTime
9
+
10
+ require 'zoneless_time/railtie' if defined? Rails
@@ -0,0 +1,11 @@
1
+ module ZonelessTime
2
+ module EqualityWithZonelessTime
3
+ def ==(other)
4
+ if other.is_a? self.class
5
+ ZonelessTime::TimeWithoutZone.at(self).matches? other
6
+ else
7
+ super(other)
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ require 'zoneless_time'
2
+ require 'rails'
3
+
4
+ module ZonelessTime
5
+ class Railtie < Rails::Railtie
6
+ initializer 'zoneless_time.configure_rails_initialization' do
7
+ ActiveSupport::TimeWithZone.send :include, EqualityWithZonelessTime
8
+ ActiveRecord::Base.send :extend, ActiveRecord::ZonelessTimeSupport
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,81 @@
1
+ module ZonelessTime
2
+ class TimeWithoutZone
3
+ attr_accessor :date, :hour, :min, :sec, :usec
4
+
5
+ RFC2822_DAY_NAME = Time::RFC2822_DAY_NAME
6
+ RFC2822_MONTH_NAME = Time::RFC2822_MONTH_NAME
7
+
8
+ def initialize(year,month,day,hour,min,sec,usec=0)
9
+ @date = Date.new(year,month,day)
10
+ @hour = hour
11
+ @min = min
12
+ @sec = sec
13
+ @usec = usec
14
+ end
15
+
16
+ def year
17
+ date.year
18
+ end
19
+ def month
20
+ date.month
21
+ end
22
+ def day
23
+ date.day
24
+ end
25
+ def wday
26
+ date.wday
27
+ end
28
+
29
+ def to_s(type=nil)
30
+ if type == :db
31
+ "#{year}-#{two_char month}-#{two_char day} #{two_char hour}:#{two_char min}:#{two_char sec}"
32
+ else
33
+ "#{RFC2822_DAY_NAME[wday]} #{RFC2822_MONTH_NAME[month-1]} #{day} #{two_char hour}:#{two_char min}:#{two_char sec} #{year}"
34
+ end
35
+ end
36
+
37
+ def inspect
38
+ to_s
39
+ end
40
+
41
+ def matches?(other)
42
+ [:year, :month, :day, :hour, :min, :sec].all? { |sym| other.send(sym) == self.send(sym) }
43
+ end
44
+
45
+ def time
46
+ Time.local(year, month, day, hour, min, sec, usec)
47
+ end
48
+
49
+ def ==(other)
50
+ matches? other
51
+ end
52
+
53
+ def acts_like?(sym)
54
+ [:date, :time].include? sym
55
+ end
56
+
57
+ private
58
+ def two_char(s)
59
+ "00#{s}"[-2..-1]
60
+ end
61
+
62
+ class <<self
63
+ def at(time)
64
+ if time.is_a? self
65
+ time
66
+ elsif time.acts_like? :time
67
+ from_time time
68
+ else
69
+ from_time Time.at(time)
70
+ end
71
+ end
72
+
73
+ private
74
+ def from_time(time)
75
+ args = [time.year, time.month, time.day, time.hour, time.min, time.sec]
76
+ args << time.usec if time.respond_to? :usec
77
+ new *args
78
+ end
79
+ end
80
+ end
81
+ end
@@ -0,0 +1 @@
1
+ puts "WARNING: zoneless_time will not behave completely as expected with ActiveRecord::Base#time_zone_aware_attributes enabled."
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: zoneless_time
3
+ version: !ruby/object:Gem::Version
4
+ hash: 15
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 2
9
+ version: "0.2"
10
+ platform: ruby
11
+ authors:
12
+ - Shaun Mangelsdorf
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-07-22 00:00:00 Z
18
+ dependencies: []
19
+
20
+ description:
21
+ email:
22
+ executables: []
23
+
24
+ extensions: []
25
+
26
+ extra_rdoc_files:
27
+ - README
28
+ files:
29
+ - lib/active_record/zoneless_time_support.rb
30
+ - lib/zoneless_time.rb
31
+ - lib/zoneless_time/equality_with_zoneless_time.rb
32
+ - lib/zoneless_time/railtie.rb
33
+ - lib/zoneless_time/time_without_zone.rb
34
+ - lib/zoneless_time/warning.rb
35
+ - README
36
+ homepage:
37
+ licenses: []
38
+
39
+ post_install_message:
40
+ rdoc_options: []
41
+
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ none: false
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ hash: 3
50
+ segments:
51
+ - 0
52
+ version: "0"
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ hash: 3
59
+ segments:
60
+ - 0
61
+ version: "0"
62
+ requirements: []
63
+
64
+ rubyforge_project:
65
+ rubygems_version: 1.8.5
66
+ signing_key:
67
+ specification_version: 3
68
+ summary: TimeWithoutZone support for ActiveRecord
69
+ test_files: []
70
+