herd 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG.md +5 -0
- data/README.md +22 -11
- data/lib/herd.rb +18 -10
- data/lib/herd/version.rb +1 -1
- data/spec/herd_spec.rb +6 -1
- data/spec/spec_helper.rb +6 -31
- data/spec/support/character.rb +4 -0
- data/spec/support/characters.rb +5 -0
- data/spec/support/director.rb +4 -0
- data/spec/support/directors.rb +3 -0
- data/spec/support/movie.rb +6 -0
- data/spec/support/movies.rb +9 -0
- metadata +17 -5
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -42,6 +42,8 @@ Using Herd, you can define a separate class to declare collection concerns:
|
|
42
42
|
|
43
43
|
```ruby
|
44
44
|
class Movie < ActiveRecord::Base
|
45
|
+
herded_by Movies
|
46
|
+
|
45
47
|
def release_year
|
46
48
|
released_at.year
|
47
49
|
end
|
@@ -52,6 +54,8 @@ class Movie < ActiveRecord::Base
|
|
52
54
|
end
|
53
55
|
|
54
56
|
class Movies < Herd::Base
|
57
|
+
model Movie # optional
|
58
|
+
|
55
59
|
scope :recent, where('released_at > ?', 6.months.ago)
|
56
60
|
scope :profitable, where('gross > budget')
|
57
61
|
|
@@ -75,15 +79,6 @@ Or install it yourself as:
|
|
75
79
|
|
76
80
|
$ gem install herd
|
77
81
|
|
78
|
-
You also need to preload all the Herd collections. The simplest way to
|
79
|
-
do this is to add a Rails initializer with the following:
|
80
|
-
|
81
|
-
config/initializers/herd.rb
|
82
|
-
|
83
|
-
```ruby
|
84
|
-
Herd.load_collections
|
85
|
-
```
|
86
|
-
|
87
82
|
## Usage Example
|
88
83
|
|
89
84
|
Using Herd is as easy as inheriting from `Herd::Base`. Declaring the
|
@@ -92,13 +87,14 @@ name.
|
|
92
87
|
|
93
88
|
```ruby
|
94
89
|
class Movie < ActiveRecord::Base
|
90
|
+
herded_by Movies
|
91
|
+
|
95
92
|
has_and_belongs_to_many :directors
|
96
93
|
has_many :characters, :dependent => :destroy
|
97
94
|
end
|
98
95
|
|
99
96
|
class Movies < Herd::Base
|
100
|
-
#
|
101
|
-
model Movie
|
97
|
+
model Movie # optional
|
102
98
|
|
103
99
|
scope :failures, where("revenue < '10000000'")
|
104
100
|
|
@@ -108,6 +104,21 @@ class Movies < Herd::Base
|
|
108
104
|
end
|
109
105
|
```
|
110
106
|
|
107
|
+
Then you can use the collection methods as if they were defined in the
|
108
|
+
model class:
|
109
|
+
|
110
|
+
```ruby
|
111
|
+
Movie.where('released_at < 2012-06-01').failures
|
112
|
+
|
113
|
+
Director.first.movies.failures
|
114
|
+
```
|
115
|
+
|
116
|
+
You can also use the Herd classes directly:
|
117
|
+
|
118
|
+
```ruby
|
119
|
+
Movies.failures
|
120
|
+
```
|
121
|
+
|
111
122
|
## Contributing
|
112
123
|
|
113
124
|
1. Fork it
|
data/lib/herd.rb
CHANGED
@@ -2,12 +2,6 @@ require "herd/version"
|
|
2
2
|
|
3
3
|
module Herd
|
4
4
|
|
5
|
-
# Load collection classes from a Rails initializer
|
6
|
-
# @param [String] path
|
7
|
-
def self.load_collections(path=Rails.root.join('app/collections'))
|
8
|
-
Dir.glob(File.join(path, '*.rb')).each {|f| require f }
|
9
|
-
end
|
10
|
-
|
11
5
|
class Base
|
12
6
|
class_attribute :model_class
|
13
7
|
|
@@ -15,10 +9,6 @@ module Herd
|
|
15
9
|
# @param [Class] model the class that the collection contains
|
16
10
|
#
|
17
11
|
# @example
|
18
|
-
# # ActiveRecord model
|
19
|
-
# class Movie < ActiveRecord::Base
|
20
|
-
# end
|
21
|
-
#
|
22
12
|
# # Herd collection
|
23
13
|
# class Movies < Herd::Base
|
24
14
|
# model Movie
|
@@ -56,4 +46,22 @@ module Herd
|
|
56
46
|
end
|
57
47
|
end
|
58
48
|
end
|
49
|
+
|
50
|
+
module ActiveRecord
|
51
|
+
# Specify the Herd collection class that the model is a member of
|
52
|
+
# @param [Class] herd_collection the Herd class that the model is a member of
|
53
|
+
#
|
54
|
+
# @example
|
55
|
+
# # ActiveRecord model
|
56
|
+
# class Movie < ActiveRecord::Base
|
57
|
+
# herded_by Movies
|
58
|
+
# end
|
59
|
+
def herded_by(herd_collection)
|
60
|
+
raise ArgumentError, 'collection must inherit from Herd::Base' unless herd_collection.ancestors.include? Herd::Base
|
61
|
+
|
62
|
+
herd_collection
|
63
|
+
end
|
64
|
+
end
|
59
65
|
end
|
66
|
+
|
67
|
+
ActiveRecord::Base.extend Herd::ActiveRecord
|
data/lib/herd/version.rb
CHANGED
data/spec/herd_spec.rb
CHANGED
@@ -4,7 +4,6 @@ describe Movies do
|
|
4
4
|
let!(:movie1) { Movie.create!(:name => 'The Goonies', :revenue => 100_000_000) }
|
5
5
|
let!(:movie2) { Movie.create!(:name => 'Gigli', :revenue => 0) }
|
6
6
|
|
7
|
-
|
8
7
|
after do
|
9
8
|
Movie.destroy_all
|
10
9
|
Director.destroy_all
|
@@ -48,3 +47,9 @@ describe Directors do
|
|
48
47
|
-> { Directors.no_method }.should raise_error(NoMethodError)
|
49
48
|
end
|
50
49
|
end
|
50
|
+
|
51
|
+
describe 'associating model to collection' do
|
52
|
+
it 'should raise an error if the collection is not Herd::Base' do
|
53
|
+
-> { Director.herded_by Director}.should raise_error(ArgumentError)
|
54
|
+
end
|
55
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -34,34 +34,9 @@ ActiveRecord::Schema.define(:version => 1) do
|
|
34
34
|
end
|
35
35
|
end
|
36
36
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
model Movie
|
44
|
-
|
45
|
-
scope :failures, where("revenue < '10000000'")
|
46
|
-
|
47
|
-
def self.directed_by(director)
|
48
|
-
where(directors: {name: director}).joins(:directors)
|
49
|
-
end
|
50
|
-
end
|
51
|
-
|
52
|
-
class Character < ActiveRecord::Base
|
53
|
-
belongs_to :movie
|
54
|
-
end
|
55
|
-
|
56
|
-
class Characters < Herd::Base
|
57
|
-
model Character
|
58
|
-
|
59
|
-
scope :fat, where(name: 'chunk')
|
60
|
-
end
|
61
|
-
|
62
|
-
class Director < ActiveRecord::Base
|
63
|
-
has_and_belongs_to_many :movies
|
64
|
-
end
|
65
|
-
|
66
|
-
class Directors < Herd::Base
|
67
|
-
end
|
37
|
+
autoload :Movie, 'support/movie'
|
38
|
+
autoload :Movies, 'support/movies'
|
39
|
+
autoload :Character, 'support/character'
|
40
|
+
autoload :Characters, 'support/characters'
|
41
|
+
autoload :Director, 'support/director'
|
42
|
+
autoload :Directors, 'support/directors'
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: herd
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-06-
|
12
|
+
date: 2012-06-08 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
@@ -94,6 +94,12 @@ files:
|
|
94
94
|
- lib/herd/version.rb
|
95
95
|
- spec/herd_spec.rb
|
96
96
|
- spec/spec_helper.rb
|
97
|
+
- spec/support/character.rb
|
98
|
+
- spec/support/characters.rb
|
99
|
+
- spec/support/director.rb
|
100
|
+
- spec/support/directors.rb
|
101
|
+
- spec/support/movie.rb
|
102
|
+
- spec/support/movies.rb
|
97
103
|
homepage: ''
|
98
104
|
licenses: []
|
99
105
|
post_install_message:
|
@@ -108,7 +114,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
108
114
|
version: '0'
|
109
115
|
segments:
|
110
116
|
- 0
|
111
|
-
hash:
|
117
|
+
hash: 3970260330302346971
|
112
118
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
113
119
|
none: false
|
114
120
|
requirements:
|
@@ -117,13 +123,19 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
117
123
|
version: '0'
|
118
124
|
segments:
|
119
125
|
- 0
|
120
|
-
hash:
|
126
|
+
hash: 3970260330302346971
|
121
127
|
requirements: []
|
122
128
|
rubyforge_project:
|
123
|
-
rubygems_version: 1.8.
|
129
|
+
rubygems_version: 1.8.23
|
124
130
|
signing_key:
|
125
131
|
specification_version: 3
|
126
132
|
summary: Organize ActiveRecord collection functionality into separate classes
|
127
133
|
test_files:
|
128
134
|
- spec/herd_spec.rb
|
129
135
|
- spec/spec_helper.rb
|
136
|
+
- spec/support/character.rb
|
137
|
+
- spec/support/characters.rb
|
138
|
+
- spec/support/director.rb
|
139
|
+
- spec/support/directors.rb
|
140
|
+
- spec/support/movie.rb
|
141
|
+
- spec/support/movies.rb
|