activerecord-time 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9726749c08e7f21a192df63e9969d9ac2e6b6ed9
4
- data.tar.gz: a6ea5592bc0fdfc1988468ed78f97e9546dffa73
3
+ metadata.gz: c8de8d80f318e113ff121248a76f69f6f0a5773f
4
+ data.tar.gz: 66a667a4adb6325e81d65786fd8faf97c62d7775
5
5
  SHA512:
6
- metadata.gz: cd921d9c1954b63dc1d885ed2eb526ac2317879ba530425423f574fe341d5d3232b5cf50723c9860d125a0651487b10632f3d3526ef172830767c2a21c10296a
7
- data.tar.gz: efb1a223cb7bbc0cb1b4cdfb07f363f7de4faa7af412e7fdaf6990f3091b6c38c732c9fcd7ce9b5117b50f037af1e862cc82cfe1116716826ac81c9114729f8b
6
+ metadata.gz: a2d38d767ee3e5a1550e78750e5c7417704bbb8aa74ab026bb1b0e5b8a8a25092472d28c3057b761870c73e62ce43a985d30f492d4f34f97ce91d2fcd3ecdd88
7
+ data.tar.gz: 6e1a694bc9c45bdff91664e3e9e3ea93f83f249b0b798a867c3a81942f2fb90717ad599e7f79c0d27fcf260dfc5c711f2ba8579111f9dd24f4fdf516d58b03ca
data/.gitignore CHANGED
@@ -15,6 +15,7 @@ lib/bundler/man
15
15
  pkg
16
16
  rdoc
17
17
  spec/reports
18
+ test/test.log
18
19
  test/tmp
19
20
  test/version_tmp
20
21
  tmp
data/.travis ADDED
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - "1.9.3"
4
+ - jruby-19mode # JRuby in 1.9 mode
5
+ - rbx
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2013 Uwe Kubosch
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/Rakefile CHANGED
@@ -2,6 +2,8 @@ require 'rubygems'
2
2
  require 'bundler/gem_tasks'
3
3
  require 'rake/testtask'
4
4
 
5
+ task default: :test
6
+
5
7
  Rake::TestTask.new do |t|
6
8
  t.libs << 'test'
7
9
  t.test_files = FileList['test/*_test.rb']
@@ -4,18 +4,29 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
  require 'activerecord-time/version'
5
5
 
6
6
  Gem::Specification.new do |gem|
7
- gem.name = 'activerecord-time'
8
- gem.version = Activerecord::Time::VERSION
9
- gem.authors = ['Uwe Kubosch']
10
- gem.email = %w(uwe@kubosch.no)
11
- gem.description = %q{A handler for storing TimeOfDay objects in ActiveRecord objects as sql time values.}
12
- gem.summary = %q{A handler for storing TimeOfDay objects in ActiveRecord objects as sql time values.}
13
- gem.homepage = 'https://github.com/donv/activerecord-time'
7
+ gem.name = 'activerecord-time'
8
+ gem.version = Activerecord::Time::VERSION
9
+ gem.authors = ['Uwe Kubosch']
10
+ gem.email = %w(uwe@kubosch.no)
11
+ gem.description = %q{A handler for storing TimeOfDay objects in ActiveRecord objects as sql time values.}
12
+ gem.summary = %q{A handler for storing TimeOfDay objects in ActiveRecord objects as sql time values.}
13
+ gem.homepage = 'https://github.com/donv/activerecord-time'
14
+ gem.license = 'MIT'
14
15
 
15
- gem.files = `git ls-files`.split($/)
16
- gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
- gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
16
+ gem.files = `git ls-files`.split($/)
17
+ gem.executables = gem.files.grep(%r{^bin/}).map { |f| File.basename(f) }
18
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
19
  gem.require_paths = %w(lib)
19
20
 
20
- gem.add_development_dependency 'activerecord-jdbcsqlite3-adapter'
21
+ gem.add_runtime_dependency 'activerecord'
22
+
23
+ if defined? JRUBY_VERSION
24
+ gem.add_development_dependency 'activerecord-jdbcpostgresql-adapter'
25
+ gem.add_development_dependency 'activerecord-jdbcsqlite3-adapter'
26
+ else
27
+ gem.add_development_dependency 'pg'
28
+ gem.add_development_dependency 'sqlite3'
29
+ end
30
+ gem.add_development_dependency 'minitest-reporters'
31
+ gem.add_development_dependency 'rake'
21
32
  end
@@ -1,3 +1,17 @@
1
1
  require 'activerecord-time/version'
2
2
  require 'time_of_day'
3
- require 'activerecord-time/extension'
3
+ if defined?(JRUBY_VERSION)
4
+ require 'activerecord-time/extension_arjdbc'
5
+ else
6
+ require 'activerecord-time/extension_mri'
7
+ end
8
+
9
+ module Arel
10
+ module Visitors
11
+ class Arel::Visitors::Visitor
12
+ def visit_TimeOfDay o, a
13
+ "'#{o.to_s(:db)}'"
14
+ end
15
+ end
16
+ end
17
+ end
@@ -5,8 +5,9 @@ module ActiveRecord
5
5
  return TimeOfDay if :time === type
6
6
  klass_without_time_of_day
7
7
  end
8
+
8
9
  alias_method_chain :klass, :time_of_day
9
-
10
+
10
11
  def self.string_to_dummy_time(string)
11
12
  return string if string.is_a? TimeOfDay
12
13
  return nil if string.empty?
@@ -18,8 +19,9 @@ module ActiveRecord
18
19
  return "'#{quoted_time(value)}'" if TimeOfDay === value
19
20
  quote_without_time_of_day(value, column)
20
21
  end
22
+
21
23
  alias_method_chain :quote, :time_of_day
22
-
24
+
23
25
  def quoted_time(value)
24
26
  value.to_s
25
27
  end
@@ -0,0 +1,48 @@
1
+ require 'active_record/connection_adapters/abstract/quoting'
2
+
3
+ module ActiveRecord
4
+ module ConnectionAdapters
5
+ class Column
6
+ # FIXME(uwe): Not necessary?
7
+ def klass_with_time_of_day
8
+ return TimeOfDay if :time === type
9
+ klass_without_time_of_day
10
+ end
11
+
12
+ alias_method_chain :klass, :time_of_day
13
+ # EMXIF
14
+
15
+ # FIXME(uwe): Not necessary?
16
+ def self.string_to_dummy_time(string)
17
+ return string if string.is_a? TimeOfDay
18
+ return nil if string.empty?
19
+ TimeOfDay.parse(string)
20
+ end
21
+ # EMXIF
22
+
23
+ end
24
+ end
25
+ end
26
+ module ActiveRecord
27
+ module ConnectionAdapters # :nodoc:
28
+ module Quoting
29
+ def type_cast_with_time_of_day(value, column)
30
+ return value.to_s if TimeOfDay === value
31
+ type_cast_without_time_of_day(value, column)
32
+ end
33
+
34
+ alias_method_chain :type_cast, :time_of_day
35
+
36
+ def quote_with_time_of_day(value, column = nil)
37
+ if column && column.type == :time && Integer === value
38
+ value = TimeOfDay.new(value / 3600, (value % 3600) / 60, value % 60)
39
+ end
40
+ return "'#{value.to_s(:db)}'" if TimeOfDay === value
41
+ quote_without_time_of_day(value, column)
42
+ end
43
+
44
+ alias_method_chain :quote, :time_of_day
45
+
46
+ end
47
+ end
48
+ end
@@ -1,5 +1,5 @@
1
1
  module Activerecord
2
2
  module Time
3
- VERSION = '0.2.0'
3
+ VERSION = '0.3.0'
4
4
  end
5
5
  end
data/lib/time_of_day.rb CHANGED
@@ -1,7 +1,11 @@
1
+ require 'yaml'
2
+
1
3
  class TimeOfDay
2
- yaml_as "tag:yaml.org,2002:timestamp#hms"
3
- yaml_as "tag:yaml.org,2002:time"
4
- yaml_as "tag:ruby.yaml.org,2002:time"
4
+ # FIXME(uwe): Not necessary?
5
+ def to_yaml_type
6
+ '!tag:yaml.org,2002:time'
7
+ end
8
+ # EMXIF
5
9
 
6
10
  attr_accessor :hour # 0 - 23
7
11
  attr_accessor :minute # 0 - 59
@@ -14,10 +18,12 @@ class TimeOfDay
14
18
  @hour, @minute, @second = hour, minute, second
15
19
  end
16
20
 
21
+ # FIXME(uwe): Not necessary?
17
22
  def init_with(coder)
18
23
  parts = self.class.parse_parts(coder.scalar)
19
24
  initialize(*parts)
20
25
  end
26
+ # EMXIF
21
27
 
22
28
  def self.now
23
29
  Time.now.time_of_day
@@ -28,11 +34,13 @@ class TimeOfDay
28
34
  self.new(*parse_parts(string))
29
35
  end
30
36
 
37
+ # FIXME(uwe): Not necessary?
31
38
  def self.parse_parts(string)
32
39
  string.strip!
33
40
  raise "Illegal time format: '#{string}'" unless string =~ /^(\d{1,2}):?(\d{2})?(?::(\d{1,2}))?$/
34
41
  [$1.to_i, $2.to_i, $3.to_i]
35
42
  end
43
+ # EMXIF
36
44
 
37
45
  def on(date)
38
46
  Time.local(date.year, date.month, date.day, hour, minute, second)
@@ -85,25 +93,29 @@ class TimeOfDay
85
93
 
86
94
  def to_s(with_seconds = true)
87
95
  if with_seconds
88
- "%02d:%02d:%02d" % [@hour, @minute, @second]
96
+ '%02d:%02d:%02d' % [@hour, @minute, @second]
89
97
  else
90
- "%02d:%02d" % [@hour, @minute]
98
+ '%02d:%02d' % [@hour, @minute]
91
99
  end
92
100
  end
93
101
 
102
+ # FIXME(uwe): Not necessary?
94
103
  def self.yaml_new(klass, tag, val)
95
104
  if String === val
96
105
  self.parse val
97
106
  else
98
- raise YAML::TypeError, "Invalid Time: " + val.inspect
107
+ raise YAML::TypeError, 'Invalid Time: ' + val.inspect
99
108
  end
100
109
  end
110
+ # EMXIF
101
111
 
112
+ # FIXME(uwe): Not necessary?
102
113
  def to_yaml(opts = {})
103
114
  YAML::quick_emit(nil, opts) do |out|
104
115
  out.scalar('tag:yaml.org,2002:time', self.to_s, :plain)
105
116
  end
106
117
  end
118
+ # EMXIF
107
119
 
108
120
  end
109
121
 
@@ -120,3 +132,5 @@ class Date
120
132
  end
121
133
 
122
134
  end
135
+
136
+ YAML.add_tag 'tag:yaml.org,2002:time', TimeOfDay
data/test/database.yml CHANGED
@@ -4,6 +4,7 @@ sqlite:
4
4
  sqlite3:
5
5
  :adapter: sqlite3
6
6
  :dbfile: ":memory:"
7
+ :database: ":memory:"
7
8
  postgresql:
8
9
  :adapter: postgresql
9
10
  :username: postgres
data/test/schema.rb CHANGED
@@ -1,10 +1,6 @@
1
- ActiveRecord::Schema.define(:version => 1) do
2
-
3
- # Create tables for testing your plugin
4
-
5
- create_table :things do |t|
6
- t.column :name, :string
7
- t.column :price_in_cents, :integer
8
- end
9
-
1
+ ActiveRecord::Schema.define(version: 1) do
2
+ create_table :events do |t|
3
+ t.string :name
4
+ t.time :start_at
5
+ end
10
6
  end
data/test/test_helper.rb CHANGED
@@ -2,7 +2,10 @@ $:.unshift(File.dirname(__FILE__) + '/../lib')
2
2
  RAILS_ROOT = File.dirname(__FILE__)
3
3
 
4
4
  require 'rubygems'
5
- require 'test/unit'
5
+ require 'minitest/autorun'
6
+ require 'minitest/reporters'
7
+ MiniTest::Reporters.use!
8
+
6
9
  require 'active_record'
7
10
  require 'activerecord-time'
8
11
  require 'logger'
@@ -10,6 +13,6 @@ require 'fileutils'
10
13
 
11
14
  FileUtils.rm_rf File.expand_path(':memory:', File.dirname(__FILE__))
12
15
  config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
13
- ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + '/debug.log')
16
+ ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + '/test.log')
14
17
  ActiveRecord::Base.establish_connection(config[ENV['DB'] || 'sqlite3'])
15
18
  load(File.dirname(__FILE__) + '/schema.rb') if File.exist?(File.dirname(__FILE__) + '/schema.rb')
@@ -1,11 +1,15 @@
1
1
  require File.expand_path('test_helper', File.dirname(__FILE__))
2
2
 
3
- class TimeOfDayTest < Test::Unit::TestCase
3
+ class Event < ActiveRecord::Base
4
+ end
5
+
6
+ class TimeOfDayTest < Minitest::Test
4
7
  def setup
5
8
  @twelve_o_clock = TimeOfDay.parse '12:00:00'
6
9
  end
7
10
 
8
11
  def teardown
12
+ @twelve_o_clock = nil
9
13
  end
10
14
 
11
15
  def test_parse_full
@@ -20,4 +24,29 @@ class TimeOfDayTest < Test::Unit::TestCase
20
24
  assert_equal @twelve_o_clock, TimeOfDay.parse('12')
21
25
  end
22
26
 
27
+ def test_yaml_load
28
+ hash = YAML.load("start_at: !!time 12:34:56")
29
+ assert_equal({'start_at' => TimeOfDay.parse('12:34:56')}, hash)
30
+ end
31
+
32
+ def test_yaml_dump
33
+ string = YAML.dump({'start_at' => TimeOfDay.parse('12:34:56')})
34
+ assert_equal("---\nstart_at: !!time 12:34:56\n", string)
35
+ end
36
+
37
+ def test_activerecord
38
+ Event.create! name: 'Bored meeting', start_at: @twelve_o_clock
39
+ t = Event.where(start_at: '12:00:00').first
40
+ assert_equal t, Event.where(start_at: @twelve_o_clock).first
41
+ assert_equal t, Event.where('start_at = ?', @twelve_o_clock).first
42
+ assert_equal t, Event.where('start_at >= ?', @twelve_o_clock).first
43
+ assert_equal t, Event.where('start_at <= ?', @twelve_o_clock).first
44
+ assert_nil Event.where('start_at <> ?', @twelve_o_clock).first
45
+ assert_nil Event.where('start_at < ?', @twelve_o_clock).first
46
+ assert_nil Event.where('start_at > ?', @twelve_o_clock).first
47
+
48
+ t.update! start_at: TimeOfDay.new(13)
49
+ t.destroy
50
+ end
51
+
23
52
  end
metadata CHANGED
@@ -1,27 +1,83 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-time
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Uwe Kubosch
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-11-29 00:00:00.000000000 Z
11
+ date: 2014-12-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: activerecord-jdbcsqlite3-adapter
14
+ name: activerecord
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - '>='
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: pg
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: sqlite3
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest-reporters
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
18
74
  - !ruby/object:Gem::Version
19
75
  version: '0'
20
76
  type: :development
21
77
  prerelease: false
22
78
  version_requirements: !ruby/object:Gem::Requirement
23
79
  requirements:
24
- - - '>='
80
+ - - ">="
25
81
  - !ruby/object:Gem::Version
26
82
  version: '0'
27
83
  description: A handler for storing TimeOfDay objects in ActiveRecord objects as sql
@@ -32,14 +88,17 @@ executables: []
32
88
  extensions: []
33
89
  extra_rdoc_files: []
34
90
  files:
35
- - .gitignore
91
+ - ".gitignore"
92
+ - ".travis"
36
93
  - Gemfile
94
+ - LICENSE
37
95
  - LICENSE.txt
38
96
  - README.md
39
97
  - Rakefile
40
98
  - activerecord-time.gemspec
41
99
  - lib/activerecord-time.rb
42
- - lib/activerecord-time/extension.rb
100
+ - lib/activerecord-time/extension_arjdbc.rb
101
+ - lib/activerecord-time/extension_mri.rb
43
102
  - lib/activerecord-time/version.rb
44
103
  - lib/time_of_day.rb
45
104
  - test/database.yml
@@ -47,7 +106,8 @@ files:
47
106
  - test/test_helper.rb
48
107
  - test/time_of_day_test.rb
49
108
  homepage: https://github.com/donv/activerecord-time
50
- licenses: []
109
+ licenses:
110
+ - MIT
51
111
  metadata: {}
52
112
  post_install_message:
53
113
  rdoc_options: []
@@ -55,17 +115,17 @@ require_paths:
55
115
  - lib
56
116
  required_ruby_version: !ruby/object:Gem::Requirement
57
117
  requirements:
58
- - - '>='
118
+ - - ">="
59
119
  - !ruby/object:Gem::Version
60
120
  version: '0'
61
121
  required_rubygems_version: !ruby/object:Gem::Requirement
62
122
  requirements:
63
- - - '>='
123
+ - - ">="
64
124
  - !ruby/object:Gem::Version
65
125
  version: '0'
66
126
  requirements: []
67
127
  rubyforge_project:
68
- rubygems_version: 2.0.6
128
+ rubygems_version: 2.4.3
69
129
  signing_key:
70
130
  specification_version: 4
71
131
  summary: A handler for storing TimeOfDay objects in ActiveRecord objects as sql time
@@ -75,4 +135,3 @@ test_files:
75
135
  - test/schema.rb
76
136
  - test/test_helper.rb
77
137
  - test/time_of_day_test.rb
78
- has_rdoc: