ohm-contrib 0.0.31 → 0.0.33

Sign up to get free protection for your applications and to get access to all the features.
Files changed (59) hide show
  1. data/LICENSE +2 -1
  2. data/README.markdown +2 -1
  3. data/Rakefile +4 -53
  4. data/lib/ohm/contrib.rb +2 -2
  5. data/lib/ohm/contrib/boundaries.rb +1 -0
  6. data/lib/ohm/contrib/callbacks.rb +5 -4
  7. data/lib/ohm/contrib/date_validations.rb +2 -1
  8. data/lib/ohm/contrib/extra_validations.rb +1 -0
  9. data/lib/ohm/contrib/length_validations.rb +1 -0
  10. data/lib/ohm/contrib/locking.rb +2 -1
  11. data/lib/ohm/contrib/lunar_macros.rb +5 -4
  12. data/lib/ohm/contrib/number_validations.rb +1 -0
  13. data/lib/ohm/contrib/slug.rb +1 -0
  14. data/lib/ohm/contrib/timestamping.rb +1 -0
  15. data/lib/ohm/contrib/typecast.rb +1 -0
  16. data/lib/ohm/contrib/web_validations.rb +2 -1
  17. data/test/autoload_test.rb +19 -0
  18. data/test/boundaries_test.rb +45 -0
  19. data/test/callbacks_lint.rb +105 -0
  20. data/test/date_validations_test.rb +29 -0
  21. data/test/helper.rb +19 -10
  22. data/test/instance_callbacks_test.rb +45 -0
  23. data/test/length_validations_test.rb +31 -0
  24. data/test/lunar_macros_test.rb +146 -0
  25. data/test/macro_callbacks_test.rb +57 -0
  26. data/test/membership_validation_test.rb +31 -0
  27. data/test/number_validations_test.rb +37 -0
  28. data/test/slug_test.rb +30 -0
  29. data/test/timestamping_test.rb +32 -0
  30. data/test/typecast_array_test.rb +154 -0
  31. data/test/typecast_boolean_test.rb +43 -0
  32. data/test/typecast_date_test.rb +77 -0
  33. data/test/typecast_decimal_test.rb +77 -0
  34. data/test/typecast_float_test.rb +52 -0
  35. data/test/typecast_hash_test.rb +117 -0
  36. data/test/typecast_integer_test.rb +66 -0
  37. data/test/typecast_string_test.rb +38 -0
  38. data/test/typecast_time_test.rb +67 -0
  39. data/test/typecast_timezone_test.rb +37 -0
  40. data/test/web_validations_test.rb +79 -0
  41. metadata +47 -56
  42. data/.document +0 -5
  43. data/.gitignore +0 -25
  44. data/VERSION +0 -1
  45. data/lib/ohm/contrib/to_hash.rb +0 -39
  46. data/ohm-contrib.gemspec +0 -103
  47. data/test/test_ohm_boundaries.rb +0 -74
  48. data/test/test_ohm_contrib.rb +0 -70
  49. data/test/test_ohm_contrib_callbacks.rb +0 -375
  50. data/test/test_ohm_date_validations.rb +0 -30
  51. data/test/test_ohm_extra_validations.rb +0 -32
  52. data/test/test_ohm_length_validations.rb +0 -32
  53. data/test/test_ohm_lunar_macros.rb +0 -165
  54. data/test/test_ohm_number_validations.rb +0 -59
  55. data/test/test_ohm_slug.rb +0 -29
  56. data/test/test_ohm_timestamping.rb +0 -64
  57. data/test/test_ohm_to_hash.rb +0 -67
  58. data/test/test_ohm_typecast.rb +0 -707
  59. data/test/test_ohm_web_validations.rb +0 -114
data/LICENSE CHANGED
@@ -17,4 +17,5 @@ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
17
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
18
  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
19
  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
+
data/README.markdown CHANGED
@@ -198,4 +198,5 @@ Note on Patches/Pull Requests
198
198
 
199
199
  Copyright
200
200
  ---------
201
- Copyright (c) 2010 Cyril David. See LICENSE for details.
201
+ Copyright (c) 2010 Cyril David. See LICENSE for details.
202
+
data/Rakefile CHANGED
@@ -1,57 +1,8 @@
1
- require 'rubygems'
2
- require 'rake'
1
+ require 'cutest'
3
2
 
4
- begin
5
- require 'jeweler'
6
- Jeweler::Tasks.new do |gem|
7
- gem.name = "ohm-contrib"
8
- gem.summary = %Q{A collection of ohm related modules}
9
- gem.description = %Q{Highly decoupled drop-in functionality for Ohm models}
10
- gem.email = "cyx.ucron@gmail.com"
11
- gem.homepage = "http://labs.sinefunc.com/ohm-contrib"
12
- gem.authors = ["Cyril David"]
13
- gem.add_development_dependency "contest", ">= 0"
14
- gem.add_development_dependency "redis", ">= 0"
15
- # gem.add_development_dependency "ohm", ">= 0"
16
- gem.add_development_dependency "timecop", ">= 0"
17
- gem.add_development_dependency "mocha", ">= 0"
18
- gem.add_development_dependency "lunar", ">= 0"
19
- end
20
- Jeweler::GemcutterTasks.new
21
- rescue LoadError
22
- puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
23
- end
24
-
25
- require 'rake/testtask'
26
- Rake::TestTask.new(:test) do |test|
27
- test.libs << 'lib' << 'test'
28
- test.pattern = 'test/**/test_*.rb'
29
- test.verbose = true
30
- end
3
+ task :test do
4
+ $:.unshift('./lib', './test')
31
5
 
32
- begin
33
- require 'rcov/rcovtask'
34
- Rcov::RcovTask.new do |test|
35
- test.libs << 'test'
36
- test.pattern = 'test/**/test_*.rb'
37
- test.verbose = true
38
- end
39
- rescue LoadError
40
- task :rcov do
41
- abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
42
- end
6
+ Cutest.run(Dir['./test/*_test.rb'])
43
7
  end
44
8
 
45
- task :test => :check_dependencies
46
-
47
- task :default => :test
48
-
49
- require 'rake/rdoctask'
50
- Rake::RDocTask.new do |rdoc|
51
- version = File.exist?('VERSION') ? File.read('VERSION') : ""
52
-
53
- rdoc.rdoc_dir = 'rdoc'
54
- rdoc.title = "ohm-contrib #{version}"
55
- rdoc.rdoc_files.include('README*')
56
- rdoc.rdoc_files.include('lib/**/*.rb')
57
- end
data/lib/ohm/contrib.rb CHANGED
@@ -1,11 +1,10 @@
1
1
  module Ohm
2
2
  module Contrib
3
- VERSION = '0.0.31'
3
+ VERSION = '0.0.33'
4
4
  end
5
5
 
6
6
  autoload :Boundaries, "ohm/contrib/boundaries"
7
7
  autoload :Timestamping, "ohm/contrib/timestamping"
8
- autoload :ToHash, "ohm/contrib/to_hash"
9
8
  autoload :LengthValidations, "ohm/contrib/length_validations"
10
9
  autoload :WebValidations, "ohm/contrib/web_validations"
11
10
  autoload :NumberValidations, "ohm/contrib/number_validations"
@@ -17,3 +16,4 @@ module Ohm
17
16
  autoload :LunarMacros, "ohm/contrib/lunar_macros"
18
17
  autoload :Slug, "ohm/contrib/slug"
19
18
  end
19
+
@@ -38,3 +38,4 @@ module Ohm
38
38
  end
39
39
  end
40
40
  end
41
+
@@ -12,7 +12,7 @@ module Ohm
12
12
  # - after_save
13
13
  # - before_delete
14
14
  # - after_delete
15
- #
15
+ #
16
16
  # If you prefer to do a class level declaration that is also possible.
17
17
  #
18
18
  # @example
@@ -49,7 +49,7 @@ module Ohm
49
49
  # # do something with the ids
50
50
  # end
51
51
  # end
52
-
52
+
53
53
  module Callbacks
54
54
  def self.included(base)
55
55
  base.extend Macros
@@ -124,7 +124,7 @@ module Ohm
124
124
  @callbacks ||= Hash.new { |h, k| h[k] = Hash.new { |h, k| h[k] = [] }}
125
125
  end
126
126
  end
127
-
127
+
128
128
  # This module is for class method overrides. As of now
129
129
  # it only overrides Ohm::Model::create to force calling save
130
130
  # instead of calling create so that Model.create will call
@@ -190,7 +190,7 @@ module Ohm
190
190
  execute_callback(:after, :delete) if is_deleted
191
191
  end
192
192
  end
193
-
193
+
194
194
  protected
195
195
  def before_validate() end
196
196
  def after_validate() end
@@ -211,3 +211,4 @@ module Ohm
211
211
  end
212
212
  end
213
213
  end
214
+
@@ -3,7 +3,7 @@ require 'date'
3
3
  module Ohm
4
4
  module DateValidations
5
5
  DATE_REGEX = /\A([0-9]{4})-([01]?[0-9])-([0123]?[0-9])\z/
6
-
6
+
7
7
  def assert_date(att, error = [att, :not_date])
8
8
  if assert_format att, DATE_REGEX, error
9
9
  m = send(att).to_s.match(DATE_REGEX)
@@ -21,3 +21,4 @@ module Ohm
21
21
  end
22
22
  end
23
23
  end
24
+
@@ -45,3 +45,4 @@ module Ohm
45
45
  end
46
46
  end
47
47
  end
48
+
@@ -29,3 +29,4 @@ module Ohm
29
29
  end
30
30
  end
31
31
  end
32
+
@@ -52,4 +52,5 @@ module Ohm
52
52
  lock.to_f < Time.now.to_f
53
53
  end
54
54
  end
55
- end
55
+ end
56
+
@@ -19,11 +19,11 @@ module Ohm
19
19
  def text(*atts) lunar_fields(:text, *atts) end
20
20
  def number(*atts) lunar_fields(:number, *atts) end
21
21
  def sortable(*atts) lunar_fields(:sortable, *atts) end
22
-
22
+
23
23
  def lunar_fields(type, *atts)
24
24
  @lunar_fields ||= Hash.new { |h, k| h[k] = [] }
25
25
 
26
- atts.each { |att|
26
+ atts.each { |att|
27
27
  @lunar_fields[type] << att unless @lunar_fields[type].include?(att)
28
28
  }
29
29
 
@@ -34,11 +34,11 @@ module Ohm
34
34
  def update_lunar_index
35
35
  Lunar.index self.class do |i|
36
36
  i.id id
37
-
37
+
38
38
  [:fuzzy, :text, :number, :sortable].each do |type|
39
39
  self.class.lunar_fields(type).each do |field|
40
40
  value = send(field)
41
-
41
+
42
42
  if type == :text and value.kind_of?(Enumerable)
43
43
  i.text field, value.join(' ') unless value.empty?
44
44
  else
@@ -55,3 +55,4 @@ module Ohm
55
55
  end
56
56
  end
57
57
  end
58
+
@@ -11,3 +11,4 @@ module Ohm
11
11
  end
12
12
  end
13
13
  end
14
+
@@ -20,3 +20,4 @@ module Ohm
20
20
  end
21
21
  end
22
22
  end
23
+
@@ -35,3 +35,4 @@ module Ohm
35
35
  end
36
36
  end
37
37
  end
38
+
@@ -316,3 +316,4 @@ module Ohm
316
316
  end
317
317
  end
318
318
  end
319
+
@@ -48,4 +48,5 @@ module Ohm
48
48
  assert_ipv4(att, error)
49
49
  end
50
50
  end
51
- end
51
+ end
52
+
@@ -0,0 +1,19 @@
1
+ # encoding: UTF-8
2
+
3
+ require File.expand_path("./helper", File.dirname(__FILE__))
4
+
5
+ test "autoloading of all libraries" do
6
+ assert_nothing_raised NameError, LoadError do
7
+ Ohm::Boundaries
8
+ Ohm::Timestamping
9
+ Ohm::WebValidations
10
+ Ohm::NumberValidations
11
+ Ohm::Typecast
12
+ Ohm::Locking
13
+ Ohm::ExtraValidations
14
+ Ohm::DateValidations
15
+ Ohm::LunarMacros
16
+ Ohm::Slug
17
+ end
18
+ end
19
+
@@ -0,0 +1,45 @@
1
+ # encoding: UTF-8
2
+
3
+ require File.expand_path("./helper", File.dirname(__FILE__))
4
+
5
+ class Person < Ohm::Model
6
+ include Ohm::Boundaries
7
+
8
+ attribute :name
9
+ index :name
10
+ end
11
+
12
+ test "first / last are nil when no records" do
13
+ assert nil == Person.first
14
+ assert nil == Person.last
15
+ end
16
+
17
+ test "first / last returns the only record when just 1 record" do
18
+ matz = Person.create(:name => "matz")
19
+
20
+ assert matz == Person.first
21
+ assert matz == Person.last
22
+ end
23
+
24
+ test "has chronological order by default" do
25
+ matz = Person.create(:name => "matz")
26
+ linus = Person.create(:name => "linus")
27
+
28
+ assert matz == Person.first
29
+ assert linus == Person.last
30
+ end
31
+
32
+ test "respects filters passed in" do
33
+ matz = Person.create(:name => "matz")
34
+ linus = Person.create(:name => "linus")
35
+
36
+ assert matz == Person.first(:name => "matz")
37
+ assert matz == Person.last(:name => "matz")
38
+
39
+ assert linus == Person.first(:name => "linus")
40
+ assert linus == Person.last(:name => "linus")
41
+
42
+ assert nil == Person.first(:name => "quentin")
43
+ assert nil == Person.last(:name => "quentin")
44
+ end
45
+
@@ -0,0 +1,105 @@
1
+ test "save on invalid" do
2
+ post = Post.new
3
+ post.save
4
+
5
+ assert post.did?(:do_before_validate)
6
+ assert post.did?(:do_after_validate)
7
+
8
+ assert ! post.did?(:do_before_create)
9
+ assert ! post.did?(:do_after_create)
10
+ assert ! post.did?(:do_before_save)
11
+ assert ! post.did?(:do_after_save)
12
+ end
13
+
14
+ test "saving a valid model" do
15
+ post = Post.new(:body => "The Body")
16
+ post.save
17
+
18
+ assert post.did?(:do_before_validate)
19
+ assert post.did?(:do_after_validate)
20
+ assert post.did?(:do_before_create)
21
+ assert post.did?(:do_after_create)
22
+ assert post.did?(:do_before_save)
23
+ assert post.did?(:do_after_save)
24
+ end
25
+
26
+ test "ensure create / save callbacks are only called once" do
27
+ post = Post.new(:body => "The Body")
28
+ post.save
29
+
30
+ assert 1 == post.count(:do_before_create)
31
+ assert 1 == post.count(:do_after_create)
32
+ assert 1 == post.count(:do_before_save)
33
+ assert 1 == post.count(:do_after_create)
34
+ end
35
+
36
+ test "using Post::create on a valid model invokes all callbacks" do
37
+ post = Post.create(:body => "The Body")
38
+
39
+ assert post.did?(:do_before_validate)
40
+ assert post.did?(:do_after_validate)
41
+ assert post.did?(:do_before_create)
42
+ assert post.did?(:do_after_create)
43
+ assert post.did?(:do_before_save)
44
+ assert post.did?(:do_after_save)
45
+ end
46
+
47
+ test "ensure Post::create only invokes save / create once" do
48
+ post = Post.create(:body => "The Body")
49
+
50
+ assert 1 == post.count(:do_before_create)
51
+ assert 1 == post.count(:do_after_create)
52
+ assert 1 == post.count(:do_before_save)
53
+ assert 1 == post.count(:do_after_create)
54
+ end
55
+
56
+ test "on successful save of existing record" do
57
+ post = Post.create(:body => "The Body")
58
+ post = Post[post.id]
59
+ post.save
60
+
61
+ assert ! post.did?(:do_before_create)
62
+ assert ! post.did?(:do_after_create)
63
+
64
+ assert post.did?(:do_before_validate)
65
+ assert post.did?(:do_after_validate)
66
+ assert post.did?(:do_before_save)
67
+ assert post.did?(:do_after_save)
68
+
69
+ assert 1 == post.count(:do_before_save)
70
+ assert 1 == post.count(:do_after_save)
71
+ end
72
+
73
+ test "on failed save of existing record" do
74
+ post = Post.create(:body => "The Body")
75
+ post = Post[post.id]
76
+
77
+ post.body = nil
78
+ post.save
79
+
80
+ assert post.did?(:do_before_validate)
81
+ assert post.did?(:do_after_validate)
82
+
83
+ assert ! post.did?(:do_before_create)
84
+ assert ! post.did?(:do_after_create)
85
+
86
+ assert ! post.did?(:do_before_save)
87
+ assert ! post.did?(:do_after_save)
88
+ end
89
+
90
+ test "on delete" do
91
+ post = Post.create(:body => "The Body")
92
+ post = Post[post.id]
93
+ post.delete
94
+
95
+ assert 1 == post.count(:do_before_delete)
96
+ assert 1 == post.count(:do_after_delete)
97
+
98
+ assert ! post.did?(:do_before_validate)
99
+ assert ! post.did?(:do_after_validate)
100
+ assert ! post.did?(:do_before_create)
101
+ assert ! post.did?(:do_after_create)
102
+ assert ! post.did?(:do_before_save)
103
+ assert ! post.did?(:do_after_save)
104
+ end
105
+
@@ -0,0 +1,29 @@
1
+ # encoding: UTF-8
2
+
3
+ require File.expand_path("./helper", File.dirname(__FILE__))
4
+
5
+ class Person < Ohm::Model
6
+ include Ohm::DateValidations
7
+
8
+ attribute :birthday
9
+
10
+ def validate
11
+ assert_date :birthday
12
+ end
13
+ end
14
+
15
+ test "accepts all canonical dates" do
16
+ assert Person.new(:birthday => "2010-05-05").valid?
17
+ assert Person.new(:birthday => "2010-5-5").valid?
18
+ assert Person.new(:birthday => "2010-05-5").valid?
19
+ assert Person.new(:birthday => "2010-5-05").valid?
20
+ end
21
+
22
+ test "also catches invalid dates" do
23
+ assert ! Person.new(:birthday => "2010-02-29").valid?
24
+ end
25
+
26
+ test "invalid when empty" do
27
+ assert ! Person.new(:birthday => "").valid?
28
+ end
29
+