momomoto 0.1.9 → 0.1.10

Sign up to get free protection for your applications and to get access to all the features.
@@ -189,7 +189,7 @@ module Momomoto
189
189
  new_row[ key ] = true
190
190
  elsif value.default == "false"
191
191
  new_row[ key ] = false
192
- elsif m = value.default.match( /^'([^']+)'::(text|interval|timestamp with time(out)? zone|time with(out)? time zone)$/ )
192
+ elsif m = value.default.match( /^'([^']+)'::(text|interval|time(stamp)? with(out)? time zone)$/ )
193
193
  new_row[ key ] = m[1]
194
194
  end
195
195
  end
@@ -295,7 +295,7 @@ module Momomoto
295
295
  raise Error, "Primary key fields must not be empty!" if not row.send( field_name )
296
296
  conditions[field_name] = row.send( field_name )
297
297
  end
298
- sql = "DELETE FROM #{table_name} #{compile_where(conditions)};"
298
+ sql = "DELETE FROM #{full_name} #{compile_where(conditions)};"
299
299
  row.new_record = true
300
300
  database.execute( sql )
301
301
  end
@@ -3,23 +3,24 @@ require 'date'
3
3
 
4
4
  class TimeInterval
5
5
 
6
+ include Comparable
7
+
8
+ class ParseError < StandardError; end
9
+
6
10
  attr_reader :hour, :min, :sec
7
11
 
8
12
  class << self
9
13
 
10
14
  def parse( interval )
11
- d = Date._parse( interval, false)
12
- if d.empty? && interval.length > 0
13
- raise "Could not parse interval `#{interval}`"
14
- end
15
- if !(d.keys - [:hour,:min,:sec]).empty?
16
- raise "Could not parse interval `#{interval}`"
17
- end
18
- TimeInterval.new( d )
15
+ TimeInterval.new( interval )
19
16
  end
20
17
 
21
18
  end
22
19
 
20
+ def <=>( other )
21
+ self.to_i <=> other.to_i
22
+ end
23
+
23
24
  def strftime( fmt = "%H:%M:%S" )
24
25
  fmt.gsub( /%(.)/ ) do | match |
25
26
  case match[1,1]
@@ -27,7 +28,7 @@ class TimeInterval
27
28
  when 'M' then sprintf('%02d',@min)
28
29
  when 'S' then sprintf('%02d',@sec)
29
30
  when '%' then '%'
30
- else '%' + match
31
+ else match
31
32
  end
32
33
  end
33
34
  end
@@ -43,9 +44,24 @@ class TimeInterval
43
44
  end
44
45
 
45
46
  def initialize( d = {} )
46
- @hour = d[:hour] || 0
47
- @min = d[:min] || 0
48
- @sec = d[:sec] || 0
47
+ case d
48
+ when Hash then
49
+ @hour = d[:hour] || 0
50
+ @min = d[:min] || 0
51
+ @sec = d[:sec] || 0
52
+ when Integer then
53
+ @hour = d/3600
54
+ @min = (d/60)%60
55
+ @sec = d%60
56
+ when String then
57
+ parsed = Date._parse( d, false)
58
+ if ( parsed.empty? && d.length > 0 ) || !(parsed.keys - [:hour,:min,:sec]).empty?
59
+ raise ParseError, "Could not parse interval `#{d}`"
60
+ end
61
+ @hour = parsed[:hour] || 0
62
+ @min = parsed[:min] || 0
63
+ @sec = parsed[:sec] || 0
64
+ end
49
65
  end
50
66
 
51
67
  end
@@ -1,12 +1,13 @@
1
1
 
2
2
  class TestTimeInterval < Test::Unit::TestCase
3
3
 
4
-
5
4
  def test_parse
6
5
  ["00:00:00","00:05:00","00:00:23","00:05:23","05:00:00","42:00:00","42:05:23"].each do | number |
7
6
  i = TimeInterval.parse( number )
8
7
  assert_equal( i.to_s, number )
9
8
  end
9
+ assert_raise( TimeInterval::ParseError ) do TimeInterval.parse("u") end
10
+ assert_raise( TimeInterval::ParseError ) do TimeInterval.parse("2023-05-05 23:05:00") end
10
11
  end
11
12
 
12
13
  def test_to_i
@@ -18,7 +19,22 @@ class TestTimeInterval < Test::Unit::TestCase
18
19
  end
19
20
  end
20
21
 
22
+ def test_strftime
23
+ i = TimeInterval.parse( "23:00:05" )
24
+ assert_equal( "23", i.strftime("%H"))
25
+ assert_equal( "00", i.strftime("%M"))
26
+ assert_equal( "05", i.strftime("%S"))
27
+ assert_equal( "%", i.strftime("%%"))
28
+ assert_equal( "%A", i.strftime("%A"))
29
+ end
21
30
 
31
+ def test_comparison
32
+ assert( TimeInterval.new( 0 ) < TimeInterval.new( 2 ) )
33
+ assert( TimeInterval.new( 42 ) > TimeInterval.new( 5 ) )
34
+ assert( TimeInterval.new( 23 ) == TimeInterval.new( 23 ) )
35
+ assert( TimeInterval.new( "0:00:00" ) == TimeInterval.new( 0 ) )
36
+ assert( TimeInterval.new( "01:00:00" ) == TimeInterval.new( "1:00:00" ) )
37
+ end
22
38
 
23
39
  end
24
40
 
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.4
3
3
  specification_version: 1
4
4
  name: momomoto
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.1.9
7
- date: 2007-10-25 00:00:00 +02:00
6
+ version: 0.1.10
7
+ date: 2007-11-01 00:00:00 +01:00
8
8
  summary: Momomoto is an object relational mapper for PostgreSQL.
9
9
  require_paths:
10
10
  - lib