herd 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.md CHANGED
@@ -1,5 +1,10 @@
1
1
  # Herd Changelog
2
2
 
3
+ ## v0.0.2
4
+
5
+ Removes `Herd.load_collections` in favor of using `herded_by Collection`
6
+ in ActiveRecord model
7
+
3
8
  ## v0.0.1
4
9
 
5
10
  Initial Release
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
- # Optional
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
@@ -1,3 +1,3 @@
1
1
  module Herd
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
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
- class Movie < ActiveRecord::Base
38
- has_and_belongs_to_many :directors
39
- has_many :characters, :dependent => :destroy
40
- end
41
-
42
- class Movies < Herd::Base
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'
@@ -0,0 +1,4 @@
1
+ class Character < ActiveRecord::Base
2
+ herded_by Characters
3
+ belongs_to :movie
4
+ end
@@ -0,0 +1,5 @@
1
+ class Characters < Herd::Base
2
+ model Character
3
+
4
+ scope :fat, where(name: 'chunk')
5
+ end
@@ -0,0 +1,4 @@
1
+ class Director < ActiveRecord::Base
2
+ herded_by Directors
3
+ has_and_belongs_to_many :movies
4
+ end
@@ -0,0 +1,3 @@
1
+ class Directors < Herd::Base
2
+ model Director
3
+ end
@@ -0,0 +1,6 @@
1
+ class Movie < ActiveRecord::Base
2
+ herded_by Movies
3
+
4
+ has_and_belongs_to_many :directors
5
+ has_many :characters, :dependent => :destroy
6
+ end
@@ -0,0 +1,9 @@
1
+ class Movies < Herd::Base
2
+ model Movie
3
+
4
+ scope :failures, where("revenue < '10000000'")
5
+
6
+ def self.directed_by(director)
7
+ where(directors: {name: director}).joins(:directors)
8
+ end
9
+ end
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.1
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-07 00:00:00.000000000Z
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: -4311655531898936104
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: -4311655531898936104
126
+ hash: 3970260330302346971
121
127
  requirements: []
122
128
  rubyforge_project:
123
- rubygems_version: 1.8.19
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