ohm-contrib 0.0.28 → 0.0.29

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -12,7 +12,7 @@ begin
12
12
  gem.authors = ["Cyril David"]
13
13
  gem.add_development_dependency "contest", ">= 0"
14
14
  gem.add_development_dependency "redis", ">= 0"
15
- gem.add_development_dependency "ohm", ">= 0"
15
+ # gem.add_development_dependency "ohm", ">= 0"
16
16
  gem.add_development_dependency "timecop", ">= 0"
17
17
  gem.add_development_dependency "mocha", ">= 0"
18
18
  gem.add_development_dependency "lunar", ">= 0"
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.28
1
+ 0.0.29
@@ -21,11 +21,19 @@ module Ohm
21
21
 
22
22
  module ClassMethods
23
23
  def first(opts = {})
24
- find(opts).sort(:start => 0, :limit => 1).first
24
+ if opts.any?
25
+ find(opts).first
26
+ else
27
+ all.first(opts)
28
+ end
25
29
  end
26
30
 
27
31
  def last(opts = {})
28
- find(opts).sort(:start => 0, :limit => 1, :order => "DESC").first
32
+ if opts.any?
33
+ find(opts).first(:order => "DESC")
34
+ else
35
+ all.first(:order => "DESC")
36
+ end
29
37
  end
30
38
  end
31
39
  end
@@ -23,6 +23,8 @@ module Ohm
23
23
  # assert_url :url
24
24
  # assert_email :author_email
25
25
  # assert_date :birthday
26
+ # assert_min_length :slug, 10
27
+ # assert_max_length :slug, 25
26
28
  # end
27
29
  # end
28
30
  #
@@ -30,11 +32,12 @@ module Ohm
30
32
  # post.valid?
31
33
  # post.errors
32
34
  # # [[:price, :not_decimal], [:state, :not_member], [:ipaddr, :not_ipaddr],
33
- # # [:url, :not_url], [:author_email, :not_email]]
35
+ # # [:url, :not_url], [:author_email, :not_email],[:slug, :too_short]]
34
36
  module ExtraValidations
35
37
  include NumberValidations
36
38
  include WebValidations
37
39
  include DateValidations
40
+ include LengthValidations
38
41
 
39
42
  protected
40
43
  def assert_member(att, set, error = [att, :not_member])
@@ -0,0 +1,31 @@
1
+ module Ohm
2
+ module LengthValidations
3
+
4
+ def assert_min_length(att, length, error = [att, :too_short])
5
+ if assert_present(att, error)
6
+ m = send(att).to_s
7
+ assert is_long_enough?(m, length), error
8
+ end
9
+ end
10
+
11
+ def assert_max_length(att, length, error = [att, :too_long])
12
+ if assert_present(att, error)
13
+ m = send(att).to_s
14
+ assert is_too_long?(m, length), error
15
+ end
16
+ end
17
+
18
+ private
19
+ def is_too_long?(string, length)
20
+ string.size <= length
21
+ rescue ArgumentError
22
+ return false
23
+ end
24
+
25
+ def is_long_enough?(string, length)
26
+ string.size >= length
27
+ rescue ArgumentError
28
+ return false
29
+ end
30
+ end
31
+ end
@@ -29,11 +29,11 @@ module Ohm
29
29
  #
30
30
  # @see Model#mutex
31
31
  def lock!(wait = 0.1)
32
- until db.setnx(key(:_lock), lock_timeout)
33
- next unless lock = db.get(key(:_lock))
32
+ until key[:_lock].setnx(lock_timeout)
33
+ next unless lock = key[:_lock].get
34
34
  sleep(wait) and next unless lock_expired?(lock)
35
35
 
36
- break unless lock = db.getset(key(:_lock), lock_timeout)
36
+ break unless lock = key[:_lock].getset(lock_timeout)
37
37
  break if lock_expired?(lock)
38
38
  end
39
39
  end
@@ -41,7 +41,7 @@ module Ohm
41
41
  # Release the lock.
42
42
  # @see Model#mutex
43
43
  def unlock!
44
- db.del(key(:_lock))
44
+ key[:_lock].del
45
45
  end
46
46
 
47
47
  def lock_timeout
@@ -39,7 +39,11 @@ module Ohm
39
39
  self.class.lunar_fields(type).each do |field|
40
40
  value = send(field)
41
41
 
42
- i.send type, field, value unless value.to_s.empty?
42
+ if type == :text and value.kind_of?(Enumerable)
43
+ i.text field, value.join(' ') unless value.empty?
44
+ else
45
+ i.send type, field, value unless value.to_s.empty?
46
+ end
43
47
  end
44
48
  end
45
49
  end
@@ -111,8 +111,17 @@ module Ohm
111
111
  class Time < Primitive
112
112
  delegate_to ::Time
113
113
 
114
+ YYYY_MM_DD = /\A([0-9]{4})-([01]?[0-9])-([0123]?[0-9])\z/
115
+ YYYY_MM_DD_HH_MM_SS = /\A([0-9]{4})-([01]?[0-9])-([0123]?[0-9]) ([\d]{1,2}):([\d]{1,2})(:([\d]{1,2}))?\z/
116
+
114
117
  def object
115
- ::Time.parse(@raw)
118
+ if @raw =~ YYYY_MM_DD
119
+ ::Time.utc($1, $2, $3)
120
+ elsif @raw =~ YYYY_MM_DD_HH_MM_SS
121
+ ::Time.utc($1, $2, $3, $4, $5, $7)
122
+ else
123
+ ::Time.parse(@raw)
124
+ end
116
125
  end
117
126
  end
118
127
 
data/lib/ohm/contrib.rb CHANGED
@@ -1,11 +1,12 @@
1
1
  module Ohm
2
2
  module Contrib
3
- VERSION = '0.0.28'
3
+ VERSION = '0.0.29'
4
4
  end
5
5
 
6
6
  autoload :Boundaries, "ohm/contrib/boundaries"
7
7
  autoload :Timestamping, "ohm/contrib/timestamping"
8
8
  autoload :ToHash, "ohm/contrib/to_hash"
9
+ autoload :LengthValidations, "ohm/contrib/length_validations"
9
10
  autoload :WebValidations, "ohm/contrib/web_validations"
10
11
  autoload :NumberValidations, "ohm/contrib/number_validations"
11
12
  autoload :DateValidations, "ohm/contrib/date_validations"
data/ohm-contrib.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{ohm-contrib}
8
- s.version = "0.0.28"
8
+ s.version = "0.0.29"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Cyril David"]
12
- s.date = %q{2010-07-02}
12
+ s.date = %q{2010-07-27}
13
13
  s.description = %q{Highly decoupled drop-in functionality for Ohm models}
14
14
  s.email = %q{cyx.ucron@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -28,6 +28,7 @@ Gem::Specification.new do |s|
28
28
  "lib/ohm/contrib/callbacks.rb",
29
29
  "lib/ohm/contrib/date_validations.rb",
30
30
  "lib/ohm/contrib/extra_validations.rb",
31
+ "lib/ohm/contrib/length_validations.rb",
31
32
  "lib/ohm/contrib/locking.rb",
32
33
  "lib/ohm/contrib/lunar_macros.rb",
33
34
  "lib/ohm/contrib/number_validations.rb",
@@ -43,6 +44,7 @@ Gem::Specification.new do |s|
43
44
  "test/test_ohm_contrib_callbacks.rb",
44
45
  "test/test_ohm_date_validations.rb",
45
46
  "test/test_ohm_extra_validations.rb",
47
+ "test/test_ohm_length_validations.rb",
46
48
  "test/test_ohm_lunar_macros.rb",
47
49
  "test/test_ohm_number_validations.rb",
48
50
  "test/test_ohm_slug.rb",
@@ -54,7 +56,7 @@ Gem::Specification.new do |s|
54
56
  s.homepage = %q{http://labs.sinefunc.com/ohm-contrib}
55
57
  s.rdoc_options = ["--charset=UTF-8"]
56
58
  s.require_paths = ["lib"]
57
- s.rubygems_version = %q{1.3.6}
59
+ s.rubygems_version = %q{1.3.7}
58
60
  s.summary = %q{A collection of ohm related modules}
59
61
  s.test_files = [
60
62
  "test/helper.rb",
@@ -63,6 +65,7 @@ Gem::Specification.new do |s|
63
65
  "test/test_ohm_contrib_callbacks.rb",
64
66
  "test/test_ohm_date_validations.rb",
65
67
  "test/test_ohm_extra_validations.rb",
68
+ "test/test_ohm_length_validations.rb",
66
69
  "test/test_ohm_lunar_macros.rb",
67
70
  "test/test_ohm_number_validations.rb",
68
71
  "test/test_ohm_slug.rb",
@@ -76,17 +79,15 @@ Gem::Specification.new do |s|
76
79
  current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
77
80
  s.specification_version = 3
78
81
 
79
- if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
82
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
80
83
  s.add_development_dependency(%q<contest>, [">= 0"])
81
84
  s.add_development_dependency(%q<redis>, [">= 0"])
82
- s.add_development_dependency(%q<ohm>, [">= 0"])
83
85
  s.add_development_dependency(%q<timecop>, [">= 0"])
84
86
  s.add_development_dependency(%q<mocha>, [">= 0"])
85
87
  s.add_development_dependency(%q<lunar>, [">= 0"])
86
88
  else
87
89
  s.add_dependency(%q<contest>, [">= 0"])
88
90
  s.add_dependency(%q<redis>, [">= 0"])
89
- s.add_dependency(%q<ohm>, [">= 0"])
90
91
  s.add_dependency(%q<timecop>, [">= 0"])
91
92
  s.add_dependency(%q<mocha>, [">= 0"])
92
93
  s.add_dependency(%q<lunar>, [">= 0"])
@@ -94,7 +95,6 @@ Gem::Specification.new do |s|
94
95
  else
95
96
  s.add_dependency(%q<contest>, [">= 0"])
96
97
  s.add_dependency(%q<redis>, [">= 0"])
97
- s.add_dependency(%q<ohm>, [">= 0"])
98
98
  s.add_dependency(%q<timecop>, [">= 0"])
99
99
  s.add_dependency(%q<mocha>, [">= 0"])
100
100
  s.add_dependency(%q<lunar>, [">= 0"])
@@ -60,14 +60,14 @@ class TestOhmBoundaries < Test::Unit::TestCase
60
60
  context "when searching name => linus" do
61
61
  test "linus is first and last" do
62
62
  assert_equal @linus, Person.first(:name => "linus")
63
- assert_equal @linus, Person.last(:name => "linus")
63
+ assert_equal @linus, Person.last(:name => "linus")
64
64
  end
65
65
  end
66
66
 
67
67
  context "when searching name => quentin" do
68
68
  should "have nil as first and last" do
69
69
  assert_nil Person.first(:name => "quentin")
70
- assert_nil Person.last(:name => "quentin")
70
+ assert_nil Person.last(:name => "quentin")
71
71
  end
72
72
  end
73
73
  end
@@ -0,0 +1,32 @@
1
+ require "helper"
2
+
3
+ class LengthValidationsTest < Test::Unit::TestCase
4
+ describe "length validation with a normal string column" do
5
+ class Person < Ohm::Model
6
+ include Ohm::LengthValidations
7
+
8
+ attribute :name
9
+
10
+ def validate
11
+ assert_min_length :name, 5
12
+ assert_max_length :name, 10
13
+ end
14
+ end
15
+
16
+ test "valid length name" do
17
+ assert Person.new(:name => "bob smith").valid?
18
+ end
19
+
20
+ test "also catches short strings" do
21
+ assert ! Person.new(:name => "bob").valid?
22
+ end
23
+
24
+ test "also catches long strings" do
25
+ assert ! Person.new(:name => "robert smithson").valid?
26
+ end
27
+
28
+ test "invalid when empty" do
29
+ assert ! Person.new(:name => "").valid?
30
+ end
31
+ end
32
+ end
@@ -289,6 +289,50 @@ class TestOhmTypecast < Test::Unit::TestCase
289
289
  end
290
290
  end
291
291
 
292
+ context "timezones and the Time type" do
293
+ class Post < Ohm::Model
294
+ include Ohm::Typecast
295
+
296
+ attribute :printed_at, Time
297
+ end
298
+
299
+ test "2010-08-07 is parsed as 2010-08-07 00:00:00 UTC" do
300
+ post = Post.new(:printed_at => "2010-08-07")
301
+ assert_equal Time.utc(2010, 8, 7).to_s, post.printed_at.utc.to_s
302
+
303
+ post.save
304
+ post = Post[post.id]
305
+ assert_equal Time.utc(2010, 8, 7).to_s, post.printed_at.utc.to_s
306
+ end
307
+
308
+ test "2010-08-07 00:00 is parsed as 2010-08-07 00:00:00 UTC" do
309
+ post = Post.new(:printed_at => "2010-08-07 00:00")
310
+ assert_equal Time.utc(2010, 8, 7).to_s, post.printed_at.utc.to_s
311
+
312
+ post.save
313
+ post = Post[post.id]
314
+ assert_equal Time.utc(2010, 8, 7).to_s, post.printed_at.utc.to_s
315
+ end
316
+
317
+ test "2010-08-07 18:29 is parsed as 2010-08-07 18:29:00 UTC" do
318
+ post = Post.new(:printed_at => "2010-08-07 18:29")
319
+ assert_equal Time.utc(2010, 8, 7, 18, 29).to_s, post.printed_at.utc.to_s
320
+
321
+ post.save
322
+ post = Post[post.id]
323
+ assert_equal Time.utc(2010, 8, 7, 18, 29).to_s, post.printed_at.utc.to_s
324
+ end
325
+
326
+ test "2010-08-07 18:29:31 is parsed as 2010-08-07 18:29:31 UTC" do
327
+ post = Post.new(:printed_at => "2010-08-07 18:29:31")
328
+ assert_equal Time.utc(2010, 8, 7, 18, 29, 31).to_s, post.printed_at.utc.to_s
329
+
330
+ post.save
331
+ post = Post[post.id]
332
+ assert_equal Time.utc(2010, 8, 7, 18, 29, 31).to_s, post.printed_at.utc.to_s
333
+ end
334
+ end
335
+
292
336
  context "when using a date" do
293
337
  class Post < Ohm::Model
294
338
  include Ohm::Typecast
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 28
9
- version: 0.0.28
8
+ - 29
9
+ version: 0.0.29
10
10
  platform: ruby
11
11
  authors:
12
12
  - Cyril David
@@ -14,13 +14,14 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-07-02 00:00:00 +08:00
17
+ date: 2010-07-27 00:00:00 +08:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: contest
22
22
  prerelease: false
23
23
  requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
24
25
  requirements:
25
26
  - - ">="
26
27
  - !ruby/object:Gem::Version
@@ -33,6 +34,7 @@ dependencies:
33
34
  name: redis
34
35
  prerelease: false
35
36
  requirement: &id002 !ruby/object:Gem::Requirement
37
+ none: false
36
38
  requirements:
37
39
  - - ">="
38
40
  - !ruby/object:Gem::Version
@@ -42,9 +44,10 @@ dependencies:
42
44
  type: :development
43
45
  version_requirements: *id002
44
46
  - !ruby/object:Gem::Dependency
45
- name: ohm
47
+ name: timecop
46
48
  prerelease: false
47
49
  requirement: &id003 !ruby/object:Gem::Requirement
50
+ none: false
48
51
  requirements:
49
52
  - - ">="
50
53
  - !ruby/object:Gem::Version
@@ -54,9 +57,10 @@ dependencies:
54
57
  type: :development
55
58
  version_requirements: *id003
56
59
  - !ruby/object:Gem::Dependency
57
- name: timecop
60
+ name: mocha
58
61
  prerelease: false
59
62
  requirement: &id004 !ruby/object:Gem::Requirement
63
+ none: false
60
64
  requirements:
61
65
  - - ">="
62
66
  - !ruby/object:Gem::Version
@@ -66,9 +70,10 @@ dependencies:
66
70
  type: :development
67
71
  version_requirements: *id004
68
72
  - !ruby/object:Gem::Dependency
69
- name: mocha
73
+ name: lunar
70
74
  prerelease: false
71
75
  requirement: &id005 !ruby/object:Gem::Requirement
76
+ none: false
72
77
  requirements:
73
78
  - - ">="
74
79
  - !ruby/object:Gem::Version
@@ -77,18 +82,6 @@ dependencies:
77
82
  version: "0"
78
83
  type: :development
79
84
  version_requirements: *id005
80
- - !ruby/object:Gem::Dependency
81
- name: lunar
82
- prerelease: false
83
- requirement: &id006 !ruby/object:Gem::Requirement
84
- requirements:
85
- - - ">="
86
- - !ruby/object:Gem::Version
87
- segments:
88
- - 0
89
- version: "0"
90
- type: :development
91
- version_requirements: *id006
92
85
  description: Highly decoupled drop-in functionality for Ohm models
93
86
  email: cyx.ucron@gmail.com
94
87
  executables: []
@@ -110,6 +103,7 @@ files:
110
103
  - lib/ohm/contrib/callbacks.rb
111
104
  - lib/ohm/contrib/date_validations.rb
112
105
  - lib/ohm/contrib/extra_validations.rb
106
+ - lib/ohm/contrib/length_validations.rb
113
107
  - lib/ohm/contrib/locking.rb
114
108
  - lib/ohm/contrib/lunar_macros.rb
115
109
  - lib/ohm/contrib/number_validations.rb
@@ -125,6 +119,7 @@ files:
125
119
  - test/test_ohm_contrib_callbacks.rb
126
120
  - test/test_ohm_date_validations.rb
127
121
  - test/test_ohm_extra_validations.rb
122
+ - test/test_ohm_length_validations.rb
128
123
  - test/test_ohm_lunar_macros.rb
129
124
  - test/test_ohm_number_validations.rb
130
125
  - test/test_ohm_slug.rb
@@ -142,6 +137,7 @@ rdoc_options:
142
137
  require_paths:
143
138
  - lib
144
139
  required_ruby_version: !ruby/object:Gem::Requirement
140
+ none: false
145
141
  requirements:
146
142
  - - ">="
147
143
  - !ruby/object:Gem::Version
@@ -149,6 +145,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
149
145
  - 0
150
146
  version: "0"
151
147
  required_rubygems_version: !ruby/object:Gem::Requirement
148
+ none: false
152
149
  requirements:
153
150
  - - ">="
154
151
  - !ruby/object:Gem::Version
@@ -158,7 +155,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
158
155
  requirements: []
159
156
 
160
157
  rubyforge_project:
161
- rubygems_version: 1.3.6
158
+ rubygems_version: 1.3.7
162
159
  signing_key:
163
160
  specification_version: 3
164
161
  summary: A collection of ohm related modules
@@ -169,6 +166,7 @@ test_files:
169
166
  - test/test_ohm_contrib_callbacks.rb
170
167
  - test/test_ohm_date_validations.rb
171
168
  - test/test_ohm_extra_validations.rb
169
+ - test/test_ohm_length_validations.rb
172
170
  - test/test_ohm_lunar_macros.rb
173
171
  - test/test_ohm_number_validations.rb
174
172
  - test/test_ohm_slug.rb