ohm-contrib 0.0.35 → 0.0.36

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown CHANGED
@@ -2,19 +2,19 @@ ohm-contrib
2
2
  ===========
3
3
 
4
4
  A collection of drop-in modules for Ohm. Read the full documentation at
5
- [http://labs.sinefunc.com/ohm-contrib](http://labs.sinefunc.com/ohm-contrib).
5
+ [http://cyx.github.com/ohm-contrib](http://cyx.github.com/ohm-contrib).
6
6
 
7
7
  List of modules
8
8
  ---------------
9
- 1. [`Ohm::Boundaries`](http://labs.sinefunc.com/ohm-contrib/doc/Ohm/Boundaries.html)
10
- 2. [`Ohm::Callbacks`](http://labs.sinefunc.com/ohm-contrib/doc/Ohm/Callbacks.html)
11
- 3. [`Ohm::Timestamping`](http://labs.sinefunc.com/ohm-contrib/doc/Ohm/Timestamping.html)
12
- 4. [`Ohm::ToHash`](http://labs.sinefunc.com/ohm-contrib/doc/Ohm/ToHash.html)
13
- 5. [`Ohm::WebValidations`](http://labs.sinefunc.com/ohm-contrib/doc/Ohm/WebValidations.html)
14
- 6. [`Ohm::NumberValidations`](http://labs.sinefunc.com/ohm-contrib/doc/Ohm/NumberValidations.html)
15
- 7. [`Ohm::ExtraValidations`](http://labs.sinefunc.com/ohm-contrib/doc/Ohm/ExtraValidations.html)
16
- 8. [`Ohm::Typecast`](http://labs.sinefunc.com/ohm-contrib/doc/Ohm/Typecast.html)
17
- 9. [`Ohm::Locking`](http://labs.sinefunc.com/ohm-contrib/doc/Ohm/Locking.html)
9
+ 1. [`Ohm::Boundaries`](http://cyx.github.com/ohm-contrib/doc/Ohm/Boundaries.html)
10
+ 2. [`Ohm::Callbacks`](http://cyx.github.com/ohm-contrib/doc/Ohm/Callbacks.html)
11
+ 3. [`Ohm::Timestamping`](http://cyx.github.com/ohm-contrib/doc/Ohm/Timestamping.html)
12
+ 4. [`Ohm::ToHash`](http://cyx.github.com/ohm-contrib/doc/Ohm/ToHash.html)
13
+ 5. [`Ohm::WebValidations`](http://cyx.github.com/ohm-contrib/doc/Ohm/WebValidations.html)
14
+ 6. [`Ohm::NumberValidations`](http://cyx.github.com/ohm-contrib/doc/Ohm/NumberValidations.html)
15
+ 7. [`Ohm::ExtraValidations`](http://cyx.github.com/ohm-contrib/doc/Ohm/ExtraValidations.html)
16
+ 8. [`Ohm::Typecast`](http://cyx.github.com/ohm-contrib/doc/Ohm/Typecast.html)
17
+ 9. [`Ohm::Locking`](http://cyx.github.com/ohm-contrib/doc/Ohm/Locking.html)
18
18
 
19
19
  Example usage
20
20
  -------------
@@ -0,0 +1,29 @@
1
+ module Ohm
2
+ module Scope
3
+ def self.included(base)
4
+ unless defined?(base::DefinedScopes)
5
+ base.const_set(:DefinedScopes, Module.new)
6
+ end
7
+
8
+ base.extend Macros
9
+ end
10
+
11
+ module Macros
12
+ def scope(scope = nil, &block)
13
+ self::DefinedScopes.module_eval(&block) if block_given?
14
+ self::DefinedScopes.send(:include, scope) if scope
15
+ end
16
+ end
17
+
18
+ module OverloadedSet
19
+ def initialize(*args)
20
+ super
21
+
22
+ extend model::DefinedScopes if defined?(model::DefinedScopes)
23
+ end
24
+ end
25
+
26
+ Ohm::Model::Set.send :include, OverloadedSet
27
+ end
28
+ end
29
+
data/lib/ohm/contrib.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  module Ohm
2
2
  module Contrib
3
- VERSION = '0.0.35'
3
+ VERSION = '0.0.36'
4
4
  end
5
5
 
6
6
  autoload :Boundaries, "ohm/contrib/boundaries"
@@ -15,5 +15,6 @@ module Ohm
15
15
  autoload :Callbacks, "ohm/contrib/callbacks"
16
16
  autoload :LunarMacros, "ohm/contrib/lunar_macros"
17
17
  autoload :Slug, "ohm/contrib/slug"
18
+ autoload :Scope, "ohm/contrib/scope"
18
19
  end
19
20
 
@@ -0,0 +1,76 @@
1
+ # encoding: UTF-8
2
+
3
+ require File.expand_path("./helper", File.dirname(__FILE__))
4
+
5
+ class Post < Ohm::Model
6
+ set :comments, Comment
7
+ end
8
+
9
+ class Video < Ohm::Model
10
+ list :comments, Comment
11
+ end
12
+
13
+ module Finders
14
+ def rejected
15
+ find(:status => "rejected")
16
+ end
17
+ end
18
+
19
+ class Comment < Ohm::Model
20
+ include Ohm::Scope
21
+
22
+ attribute :status
23
+ index :status
24
+
25
+ scope do
26
+ def approved
27
+ find(:status => "approved")
28
+ end
29
+ end
30
+
31
+ scope Finders
32
+ end
33
+
34
+ test "has a predefined scope" do
35
+ assert defined?(Comment::DefinedScopes)
36
+ end
37
+
38
+ test "allows custom methods for the defined scopes" do
39
+ post = Post.create
40
+ comment = Comment.create(:status => "approved")
41
+ post.comments << comment
42
+
43
+ assert post.comments.approved.is_a?(Ohm::Model::Set)
44
+ assert post.comments.approved.include?(comment)
45
+ end
46
+
47
+ test "allows custom methods to be included from a module" do
48
+ post = Post.create
49
+ comment = Comment.create(:status => "rejected")
50
+ post.comments << comment
51
+
52
+ assert post.comments.rejected.is_a?(Ohm::Model::Set)
53
+ assert post.comments.rejected.include?(comment)
54
+ end
55
+
56
+ test "works with the main Comment.all collection as well" do
57
+ approved = Comment.create(:status => "approved")
58
+ rejected = Comment.create(:status => "rejected")
59
+
60
+
61
+ assert Comment.all.approved.include?(approved)
62
+ assert Comment.all.rejected.include?(rejected)
63
+ end
64
+
65
+ test "isolated from List" do
66
+ video = Video.create
67
+
68
+ assert_raise NoMethodError do
69
+ video.comments.approved
70
+ end
71
+
72
+ assert_raise NoMethodError do
73
+ video.comments.rejected
74
+ end
75
+ end
76
+
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 35
9
- version: 0.0.35
8
+ - 36
9
+ version: 0.0.36
10
10
  platform: ruby
11
11
  authors:
12
12
  - Cyril David
@@ -99,6 +99,7 @@ files:
99
99
  - lib/ohm/contrib/locking.rb
100
100
  - lib/ohm/contrib/lunar_macros.rb
101
101
  - lib/ohm/contrib/number_validations.rb
102
+ - lib/ohm/contrib/scope.rb
102
103
  - lib/ohm/contrib/slug.rb
103
104
  - lib/ohm/contrib/timestamping.rb
104
105
  - lib/ohm/contrib/typecast.rb
@@ -118,6 +119,7 @@ files:
118
119
  - test/macro_callbacks_test.rb
119
120
  - test/membership_validation_test.rb
120
121
  - test/number_validations_test.rb
122
+ - test/scope_test.rb
121
123
  - test/slug_test.rb
122
124
  - test/timestamping_test.rb
123
125
  - test/typecast_array_test.rb