dm-zone-types 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,3 @@
1
+ .bundle
2
+ *.gem
3
+
File without changes
data/Gemfile ADDED
@@ -0,0 +1,17 @@
1
+ source :rubygems
2
+
3
+ gem 'dm-core'
4
+ gem 'activesupport', '>=3.0.0.beta3'
5
+ gem 'tzinfo'
6
+
7
+ group :development do
8
+ gem "tumbler", ">= 0"
9
+ gem 'nanotest'
10
+ gem 'nanotest_extensions'
11
+ gem 'watchr'
12
+ gem 'rake'
13
+ gem 'dm-sqlite-adapter'
14
+ gem 'dm-migrations'
15
+ gem 'i18n'
16
+ end
17
+
@@ -0,0 +1,97 @@
1
+ ---
2
+ dependencies:
3
+ tzinfo:
4
+ group:
5
+ - :default
6
+ version: ">= 0"
7
+ rake:
8
+ group:
9
+ - :development
10
+ version: ">= 0"
11
+ tumbler:
12
+ group:
13
+ - :development
14
+ version: ">= 0"
15
+ watchr:
16
+ group:
17
+ - :development
18
+ version: ">= 0"
19
+ dm-core:
20
+ group:
21
+ - :default
22
+ version: ">= 0"
23
+ nanotest_extensions:
24
+ group:
25
+ - :development
26
+ version: ">= 0"
27
+ dm-sqlite-adapter:
28
+ group:
29
+ - :development
30
+ version: ">= 0"
31
+ i18n:
32
+ group:
33
+ - :development
34
+ version: ">= 0"
35
+ nanotest:
36
+ group:
37
+ - :development
38
+ version: ">= 0"
39
+ activesupport:
40
+ group:
41
+ - :default
42
+ version: ">= 3.0.0.beta3"
43
+ dm-migrations:
44
+ group:
45
+ - :development
46
+ version: ">= 0"
47
+ specs:
48
+ - rake:
49
+ version: 0.8.7
50
+ - activesupport:
51
+ version: 3.0.0.beta4
52
+ - addressable:
53
+ version: 2.1.2
54
+ - blockenspiel:
55
+ version: 0.4.1
56
+ - bundler:
57
+ version: 0.9.26
58
+ - callsite:
59
+ version: 0.0.2
60
+ - data_objects:
61
+ version: 0.10.2
62
+ - extlib:
63
+ version: 0.9.15
64
+ - dm-core:
65
+ version: 1.0.0
66
+ - dm-do-adapter:
67
+ version: 1.0.0
68
+ - dm-migrations:
69
+ version: 1.0.0
70
+ - do_sqlite3:
71
+ version: 0.10.2
72
+ - dm-sqlite-adapter:
73
+ version: 1.0.0
74
+ - i18n:
75
+ version: 0.4.1
76
+ - json:
77
+ version: 1.4.3
78
+ - nanotest:
79
+ version: 0.9.4.1
80
+ - nanotest_extensions:
81
+ version: 0.6.3
82
+ - rainbow:
83
+ version: "1.1"
84
+ - thor:
85
+ version: 0.13.7
86
+ - versionomy:
87
+ version: 0.4.0
88
+ - tumbler:
89
+ version: 0.0.16
90
+ - tzinfo:
91
+ version: 0.3.22
92
+ - watchr:
93
+ version: "0.6"
94
+ hash: 9d371af4c0abbf9dd0cff9cf922f7448268590cc
95
+ sources:
96
+ - Rubygems:
97
+ uri: http://gemcutter.org
@@ -0,0 +1,25 @@
1
+ h1. dm-zone-types
2
+
3
+ Provides time zone aware data types for data mapper.
4
+
5
+ Whenver the property is set, it will be typecast to the Time.zone set in active support and be stored in the database as the timezone set in DataMapper::Zone::Types.storage_zone
6
+
7
+ %h2. Installation
8
+
9
+ <pre><code>gem install dm-zone-types</code></pre>
10
+
11
+ %h2. Usage
12
+
13
+ <pre><code>
14
+ require 'dm-zone-types'
15
+
16
+ DataMapper::Zone::Types.storage_zone = "Melbourne" # default UTC
17
+
18
+ class MyClass
19
+ include DataMapper::Resource
20
+
21
+ property :id, Serial
22
+ property :some_time, ZonedTime
23
+ end
24
+
25
+
@@ -0,0 +1,33 @@
1
+ require 'tumbler'
2
+ Tumbler.use_rake_tasks
3
+
4
+ # --------------------------------------------------
5
+ # Tests
6
+ # --------------------------------------------------
7
+ namespace(:test) do
8
+
9
+ desc "run framework compatibility tests"
10
+ task(:all) do
11
+ paths = Dir['test/**/test_*.rb'] - ['test/test_helper.rb']
12
+
13
+ paths.each do |path|
14
+ cmd = "ruby -rubygems -I.:lib #{path}"
15
+ puts(cmd) if ENV['VERBOSE']
16
+ system(cmd)
17
+ end
18
+ end
19
+
20
+ desc "Run all tests on multiple ruby versions (requires rvm)"
21
+ task(:portability) do
22
+ versions = %w( 1.8.6 1.8.7 1.9 1.9.2 jruby )
23
+ versions.each do |version|
24
+ system <<-BASH
25
+ bash -c 'source ~/.rvm/scripts/rvm;
26
+ rvm use #{version};
27
+ echo "--------- #{version} ----------\n";
28
+ rake -s test:all'
29
+ BASH
30
+ end
31
+ end
32
+ end
33
+
data/Tumbler ADDED
@@ -0,0 +1,6 @@
1
+ gem_name "dm-zone-types"
2
+
3
+ use_color true
4
+
5
+ version_file 'lib/dm-zone-types/version.rb'
6
+ changelog_file "CHANGELOG"
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'tumbler'
4
+ tumbler = Tumbler::GemspecHelper.new
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = tumbler.name
8
+ s.version = tumbler.version
9
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
10
+ s.rubyforge_project = tumbler.name
11
+ s.authors = ["Daniel Neighman"]
12
+ s.date = tumbler.date
13
+ s.description = %q{Timezone aware datatypes for DataMapper}
14
+ s.email = "has.sox@gmail.com"
15
+ s.files = tumbler.files
16
+ s.homepage = %q{http://github.com/hassox/dm-zone-types}
17
+ s.rdoc_options = ["--charset=UTF-8"]
18
+ s.require_paths = ["lib"]
19
+ s.rubygems_version = %q{1.3.7}
20
+ s.summary = %q{Timezone aware datatypes for DataMapper}
21
+ s.test_files = tumbler.files(/^test/)
22
+
23
+ # dependencies
24
+ tumbler.inject_dependencies(s)
25
+ end
@@ -0,0 +1,35 @@
1
+ require 'dm-zone-types/version'
2
+
3
+ require 'active_support/core_ext/time/zones'
4
+ require 'active_support/core_ext/time/calculations'
5
+ require 'active_support/core_ext/time/conversions'
6
+
7
+ require 'active_support/core_ext/date_time/zones'
8
+ require 'active_support/core_ext/date_time/calculations'
9
+ require 'active_support/core_ext/date_time/conversions'
10
+
11
+ require 'active_support/core_ext/date/conversions'
12
+
13
+ require 'dm-core'
14
+
15
+ module DataMapper
16
+ module Zone
17
+ module Types
18
+ def self.storage_zone
19
+ @storage_zone ||= ActiveSupport::TimeZone["UTC"]
20
+ end
21
+
22
+ def self.storage_zone=(zone)
23
+ @storage_zone = ActiveSupport::TimeZone[zone]
24
+ end
25
+ end
26
+ end
27
+
28
+ class Property
29
+ autoload :ZonedTime, 'dm-zone-types/zoned_time'
30
+ end
31
+ end
32
+
33
+
34
+ # For chronic
35
+ # Time.use_zone("Wellington"){ Time.zone.parse(t.strftime("%Y-%m-%d %H:%M:%S")) }
@@ -0,0 +1,8 @@
1
+ # encoding: utf-8
2
+ module DataMapper
3
+ module Zone
4
+ module Types #:nodoc
5
+ VERSION = '0.1.0'
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,36 @@
1
+ # encoding: utf-8
2
+ require 'dm-zone-types'
3
+
4
+ module DataMapper
5
+ class Property
6
+ class ZonedTime < Time
7
+ include DataMapper::Zone::Types
8
+
9
+ def load(value)
10
+ value.in_time_zone(::Time.zone)
11
+ end
12
+
13
+ def dump(value)
14
+ return nil unless value
15
+ value.in_time_zone(DataMapper::Zone::Types.storage_zone)
16
+ end
17
+
18
+ def typecast(value)
19
+ case value
20
+ when ::String
21
+ ::Time.zone.parse(value)
22
+ when ::DateTime
23
+ value.in_time_zone
24
+ when ::Time
25
+ value.in_time_zone
26
+ when ::Date
27
+ value.to_time.in_time_zone
28
+ when ActiveSupport::TimeWithZone
29
+ value.in_time_zone
30
+ when NilClass
31
+ nil
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,37 @@
1
+ # Run me with:
2
+ #
3
+ # $ watchr specs.watchr
4
+
5
+ # --------------------------------------------------
6
+ # Helpers
7
+ # --------------------------------------------------
8
+ def run(cmd)
9
+ puts(cmd)
10
+ system(cmd)
11
+ end
12
+
13
+ def run_all_tests
14
+ # see Rakefile for the definition of the test:all task
15
+ system( "rake -s test:all VERBOSE=true" )
16
+ end
17
+
18
+ # --------------------------------------------------
19
+ # Watchr Rules
20
+ # --------------------------------------------------
21
+ watch( '^test.*/test_.*\.rb' ) { |m| run( "ruby -rubygems -Ilib %s" % m[0] ) }
22
+ watch( '^lib/phocus\.rb' ) { |m| run( "ruby -rubygems -Ilib test/test_phocus.rb" ) }
23
+ watch( '^lib/phocus/autodetect\.rb' ) { run_all_tests }
24
+ watch( '^test/test_helper\.rb' ) { run_all_tests }
25
+
26
+ # --------------------------------------------------
27
+ # Signal Handling
28
+ # --------------------------------------------------
29
+ # Ctrl-\
30
+ Signal.trap('QUIT') do
31
+ puts " --- Running all tests ---\n\n"
32
+ run_all_tests
33
+ end
34
+
35
+ # Ctrl-C
36
+ Signal.trap('INT') { abort("\n") }
37
+
@@ -0,0 +1,90 @@
1
+ require 'test/test_helper'
2
+
3
+ context do
4
+ setup do
5
+ DataMapper.auto_migrate!
6
+ end
7
+
8
+ test do
9
+ assert "The gem constant is defined" do
10
+ defined?(DataMapper::Zone::Types)
11
+ end
12
+
13
+ %w(Lima Greenland UTC).each do |saved_tz|
14
+ %w(Melbourne Wellington Hawaii).each do |tz|
15
+ # #typecast
16
+ context do
17
+ setup do
18
+ Time.zone = tz
19
+ DataMapper::Zone::Types.storage_zone = saved_tz
20
+ @table_name = SpecModel.storage_names[:default]
21
+ end
22
+
23
+ # should be nil when not set
24
+ test do
25
+ SpecModel.new.zoned_time.must == nil
26
+ end
27
+
28
+ # should typecast to a DateTime from a string
29
+ test do
30
+ model = SpecModel.new(:zoned_time => "2009-12-13 12:13:14")
31
+
32
+ assert { model.zoned_time.kind_of?(ActiveSupport::TimeWithZone) }
33
+
34
+ model.zoned_time.year.must == 2009
35
+ model.zoned_time.month.must == 12
36
+ model.zoned_time.day.must == 13
37
+ model.zoned_time.hour.must == 12
38
+ model.zoned_time.min.must == 13
39
+ model.zoned_time.sec.must == 14
40
+ end
41
+
42
+ # Should take a date in any time zone and convert it to the set time zone
43
+ test do
44
+ this_timezone = Time.zone.now
45
+ t = nil
46
+ Time.use_zone("Budapest"){
47
+ t = Time.zone.now
48
+ }
49
+ model = SpecModel.new(:zoned_time => t)
50
+ model.zoned_time.zone.must == this_timezone.zone
51
+ end
52
+
53
+ # should typecast to a DateTime from a time
54
+ test do
55
+ t = Time.zone.now
56
+
57
+ model = SpecModel.new(:zoned_time => t)
58
+ assert { model.save }
59
+ assert { model.zoned_time.kind_of?(ActiveSupport::TimeWithZone) }
60
+ assert { model.zoned_time.zone == t.zone }
61
+
62
+ sql = "SELECT * FROM \"#{@table_name}\""
63
+ r = repository.adapter.select(sql)
64
+
65
+ saved = DateTime.parse(r.first.zoned_time)
66
+ zoned = ActiveSupport::TimeZone[saved_tz].parse(r.first.zoned_time)
67
+ saved.year.must == zoned.year
68
+ saved.month.must == zoned.month
69
+ saved.day.must == zoned.day
70
+ saved.hour.must == zoned.hour
71
+ saved.min.must == zoned.min
72
+ saved.sec.must == zoned.sec
73
+ saved.utc_offset.must == zoned.utc_offset
74
+
75
+ repository.identity_map(SpecModel).clear
76
+ model = SpecModel.first
77
+ model.zoned_time.zone.must == t.zone
78
+ end
79
+
80
+ # should typecast to a DateTime from a date
81
+ test do
82
+ d = Date.today
83
+ model = SpecModel.new(:zoned_time => d)
84
+ assert { model.zoned_time.kind_of?(ActiveSupport::TimeWithZone) }
85
+ end
86
+ end
87
+ end # tz's
88
+ end # saved_tz's
89
+ end
90
+ end
@@ -0,0 +1,3 @@
1
+ require 'test/test_helper'
2
+
3
+
@@ -0,0 +1,22 @@
1
+ require 'nanotest'
2
+ require 'nanotest/contexts'
3
+ require 'nanotest/stats'
4
+ require 'nanotest/spec'
5
+ require 'nanotest/focus'
6
+
7
+ $:.unshift( File.expand_path(File.dirname(__FILE__)) )
8
+ $:.unshift( File.join(File.expand_path(File.dirname(__FILE__)), "..", "lib"))
9
+
10
+ require 'dm-zone-types'
11
+ require 'dm-migrations'
12
+
13
+ include Nanotest
14
+ include Nanotest::Contexts
15
+
16
+ DataMapper.setup(:default, 'sqlite::memory:')
17
+
18
+ class SpecModel
19
+ include DataMapper::Resource
20
+ property :id, Serial
21
+ property :zoned_time, ZonedTime
22
+ end
metadata ADDED
@@ -0,0 +1,239 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dm-zone-types
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Daniel Neighman
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-07-05 00:00:00 +10:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ version_requirements: &id001 !ruby/object:Gem::Requirement
23
+ none: false
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ hash: 3
28
+ segments:
29
+ - 0
30
+ version: "0"
31
+ name: dm-core
32
+ requirement: *id001
33
+ prerelease: false
34
+ type: :runtime
35
+ - !ruby/object:Gem::Dependency
36
+ version_requirements: &id002 !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ hash: -1848230021
42
+ segments:
43
+ - 3
44
+ - 0
45
+ - 0
46
+ - beta3
47
+ version: 3.0.0.beta3
48
+ name: activesupport
49
+ requirement: *id002
50
+ prerelease: false
51
+ type: :runtime
52
+ - !ruby/object:Gem::Dependency
53
+ version_requirements: &id003 !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
+ name: tzinfo
63
+ requirement: *id003
64
+ prerelease: false
65
+ type: :runtime
66
+ - !ruby/object:Gem::Dependency
67
+ version_requirements: &id004 !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ hash: 3
73
+ segments:
74
+ - 0
75
+ version: "0"
76
+ name: tumbler
77
+ requirement: *id004
78
+ prerelease: false
79
+ type: :development
80
+ - !ruby/object:Gem::Dependency
81
+ version_requirements: &id005 !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ hash: 3
87
+ segments:
88
+ - 0
89
+ version: "0"
90
+ name: nanotest
91
+ requirement: *id005
92
+ prerelease: false
93
+ type: :development
94
+ - !ruby/object:Gem::Dependency
95
+ version_requirements: &id006 !ruby/object:Gem::Requirement
96
+ none: false
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ hash: 3
101
+ segments:
102
+ - 0
103
+ version: "0"
104
+ name: nanotest_extensions
105
+ requirement: *id006
106
+ prerelease: false
107
+ type: :development
108
+ - !ruby/object:Gem::Dependency
109
+ version_requirements: &id007 !ruby/object:Gem::Requirement
110
+ none: false
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ hash: 3
115
+ segments:
116
+ - 0
117
+ version: "0"
118
+ name: watchr
119
+ requirement: *id007
120
+ prerelease: false
121
+ type: :development
122
+ - !ruby/object:Gem::Dependency
123
+ version_requirements: &id008 !ruby/object:Gem::Requirement
124
+ none: false
125
+ requirements:
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ hash: 3
129
+ segments:
130
+ - 0
131
+ version: "0"
132
+ name: rake
133
+ requirement: *id008
134
+ prerelease: false
135
+ type: :development
136
+ - !ruby/object:Gem::Dependency
137
+ version_requirements: &id009 !ruby/object:Gem::Requirement
138
+ none: false
139
+ requirements:
140
+ - - ">="
141
+ - !ruby/object:Gem::Version
142
+ hash: 3
143
+ segments:
144
+ - 0
145
+ version: "0"
146
+ name: dm-sqlite-adapter
147
+ requirement: *id009
148
+ prerelease: false
149
+ type: :development
150
+ - !ruby/object:Gem::Dependency
151
+ version_requirements: &id010 !ruby/object:Gem::Requirement
152
+ none: false
153
+ requirements:
154
+ - - ">="
155
+ - !ruby/object:Gem::Version
156
+ hash: 3
157
+ segments:
158
+ - 0
159
+ version: "0"
160
+ name: dm-migrations
161
+ requirement: *id010
162
+ prerelease: false
163
+ type: :development
164
+ - !ruby/object:Gem::Dependency
165
+ version_requirements: &id011 !ruby/object:Gem::Requirement
166
+ none: false
167
+ requirements:
168
+ - - ">="
169
+ - !ruby/object:Gem::Version
170
+ hash: 3
171
+ segments:
172
+ - 0
173
+ version: "0"
174
+ name: i18n
175
+ requirement: *id011
176
+ prerelease: false
177
+ type: :development
178
+ description: Timezone aware datatypes for DataMapper
179
+ email: has.sox@gmail.com
180
+ executables: []
181
+
182
+ extensions: []
183
+
184
+ extra_rdoc_files: []
185
+
186
+ files:
187
+ - .gitignore
188
+ - CHANGELOG
189
+ - Gemfile
190
+ - Gemfile.lock
191
+ - README.textile
192
+ - Rakefile
193
+ - Tumbler
194
+ - dm-zone-types.gemspec
195
+ - lib/dm-zone-types.rb
196
+ - lib/dm-zone-types/version.rb
197
+ - lib/dm-zone-types/zoned_time.rb
198
+ - specs.watchr
199
+ - test/dm-zone-types/test_zoned_time.rb
200
+ - test/test_dm-zone-types.rb
201
+ - test/test_helper.rb
202
+ has_rdoc: true
203
+ homepage: http://github.com/hassox/dm-zone-types
204
+ licenses: []
205
+
206
+ post_install_message:
207
+ rdoc_options:
208
+ - --charset=UTF-8
209
+ require_paths:
210
+ - lib
211
+ required_ruby_version: !ruby/object:Gem::Requirement
212
+ none: false
213
+ requirements:
214
+ - - ">="
215
+ - !ruby/object:Gem::Version
216
+ hash: 3
217
+ segments:
218
+ - 0
219
+ version: "0"
220
+ required_rubygems_version: !ruby/object:Gem::Requirement
221
+ none: false
222
+ requirements:
223
+ - - ">="
224
+ - !ruby/object:Gem::Version
225
+ hash: 3
226
+ segments:
227
+ - 0
228
+ version: "0"
229
+ requirements: []
230
+
231
+ rubyforge_project: dm-zone-types
232
+ rubygems_version: 1.3.7
233
+ signing_key:
234
+ specification_version: 3
235
+ summary: Timezone aware datatypes for DataMapper
236
+ test_files:
237
+ - test/dm-zone-types/test_zoned_time.rb
238
+ - test/test_dm-zone-types.rb
239
+ - test/test_helper.rb