not_only_but_also 0.1.0

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/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Adam Meehan
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
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.
data/README.rdoc ADDED
@@ -0,0 +1,63 @@
1
+ = Not Only... But Also
2
+
3
+ The Rails 'fat model' pattern is a good one but can result in very large model classes. It can be
4
+ unwieldy to work in such files as they begin to look a bit messy. Assuming you have judiciously
5
+ appraised the model for opportunities to extract out pieces into libraries, it still may leave you with
6
+ a lot of code. I find this is particularly the case for validations for models with a large number of columns.
7
+
8
+ This is where allowing for multiple files to define a class can help and is what NotOnlyButAlso does. It defines a
9
+ convention to help organise multiple files as well as avoiding the need for repititive code when using modules as
10
+ containers or re-opening the classes.
11
+
12
+ You might recognise this style of solution as used by the concerned_with plugin (http://github.com/jakehow/concerned_with/).
13
+ I wrote this a long time ago before I knew of the concerned_with plugin. I have released it because its got a simpler interface
14
+ and I love the name.
15
+
16
+ For interest here are some discussions of this type of solution in regards to concerned_with:
17
+
18
+ http://m.onkey.org/2008/9/15/active-record-tips-and-tricks
19
+
20
+ http://paulbarry.com/articles/2008/08/30/concerned-with-skinny-controller-skinny-model
21
+
22
+ For extra credit:
23
+
24
+ http://en.wikipedia.org/wiki/Not_Only..._But_Also
25
+
26
+ == Install
27
+
28
+ From Gemcutter:
29
+
30
+ gem install not_only_but_also
31
+
32
+ Or as plugin:
33
+
34
+ ./script/install plugin git://github.com/adzap/not_only_but_also.git
35
+
36
+ == Usage
37
+
38
+ For example you want to extract out the validations to a separate file:
39
+
40
+ class Post
41
+ not_only_but_also :validations
42
+ end
43
+
44
+ # in app/models/post/validations.rb
45
+
46
+ Post.not_only_but_also do
47
+ validates_presence_of :title
48
+ end
49
+
50
+ OR the shorthand version:
51
+
52
+ class Post
53
+ also_has :validations
54
+ end
55
+
56
+ # in app/models/post/validations.rb
57
+
58
+ Post.also_has do
59
+ validates_presence_of :title
60
+ end
61
+
62
+
63
+ Copyright (c) 2009 Adam Meehan, released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,73 @@
1
+ require 'rubygems'
2
+ require 'rake/rdoctask'
3
+ require 'rake/gempackagetask'
4
+ require 'rubygems/specification'
5
+ require 'spec/rake/spectask'
6
+
7
+ GEM_NAME = "not_only_but_also"
8
+ GEM_VERSION = '0.1.0'
9
+ AUTHOR = "Adam Meehan"
10
+ EMAIL = "adam.meehan@gmail.com"
11
+ HOMEPAGE = "http://github.com/adzap/not_only_but_also"
12
+ SUMMARY = "Rails plugin to split large models into separate files of concern. Like concerned_with but a little more convenient."
13
+
14
+ spec = Gem::Specification.new do |s|
15
+ s.name = GEM_NAME
16
+ s.version = GEM_VERSION
17
+ s.platform = Gem::Platform::RUBY
18
+ s.rubyforge_project = "not_only_but_also"
19
+ s.has_rdoc = true
20
+ s.extra_rdoc_files = ["README.rdoc"]
21
+ s.summary = SUMMARY
22
+ s.description = s.summary
23
+ s.author = AUTHOR
24
+ s.email = EMAIL
25
+ s.homepage = HOMEPAGE
26
+
27
+ s.require_path = 'lib'
28
+ s.autorequire = GEM_NAME
29
+ s.files = %w(MIT-LICENSE README.rdoc Rakefile) + Dir.glob("{lib,spec}/**/*")
30
+ end
31
+
32
+ desc 'Default: run specs.'
33
+ task :default => :spec
34
+
35
+ spec_files = Rake::FileList["spec/**/*_spec.rb"]
36
+
37
+ desc "Run specs"
38
+ Spec::Rake::SpecTask.new do |t|
39
+ t.spec_files = spec_files
40
+ t.spec_opts = ["-c"]
41
+ end
42
+
43
+ desc "Generate code coverage"
44
+ Spec::Rake::SpecTask.new(:coverage) do |t|
45
+ t.spec_files = spec_files
46
+ t.rcov = true
47
+ t.rcov_opts = ['--exclude', 'spec,/var/lib/gems']
48
+ end
49
+
50
+ desc 'Generate documentation for plugin.'
51
+ Rake::RDocTask.new(:rdoc) do |rdoc|
52
+ rdoc.rdoc_dir = 'rdoc'
53
+ rdoc.title = 'NotOnlyButAlso'
54
+ rdoc.options << '--line-numbers' << '--inline-source'
55
+ rdoc.rdoc_files.include('README')
56
+ rdoc.rdoc_files.include('lib/**/*.rb')
57
+ end
58
+
59
+ Rake::GemPackageTask.new(spec) do |pkg|
60
+ pkg.gem_spec = spec
61
+ end
62
+
63
+ desc "install the gem locally"
64
+ task :install => [:package] do
65
+ sh %{sudo gem install pkg/#{GEM_NAME}-#{GEM_VERSION}}
66
+ end
67
+
68
+ desc "create a gemspec file"
69
+ task :make_spec do
70
+ File.open("#{GEM_NAME}.gemspec", "w") do |file|
71
+ file.puts spec.to_ruby
72
+ end
73
+ end
@@ -0,0 +1,34 @@
1
+ module NotOnlyButAlso
2
+
3
+ module ClassMethods
4
+
5
+ def not_only_but_also(*contexts, &block)
6
+ if block_given?
7
+ self.class_eval(&block)
8
+ else
9
+ contexts.each do |context|
10
+ NotOnlyButAlso::Helpers.require_context_file(name, context)
11
+ end
12
+ end
13
+ end
14
+ alias also_has not_only_but_also
15
+
16
+ end
17
+
18
+ module Helpers
19
+
20
+ class << self
21
+
22
+ def require_context_file(class_name, context)
23
+ require_dependency "#{class_name.underscore}/#{context}"
24
+ rescue MissingSourceFile => e
25
+ raise e, "NotOnlyButAlso could not find a file for #{class_name} using context #{context}"
26
+ end
27
+
28
+ end
29
+
30
+ end
31
+
32
+ end
33
+
34
+ ActiveRecord::Base.extend NotOnlyButAlso::ClassMethods
@@ -0,0 +1,27 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe NotOnlyButAlso do
4
+
5
+ before do
6
+ Object.send(:remove_const, :Post) if Object.const_defined?(:Post)
7
+ Object.send(:remove_const, :Comment) if Object.const_defined?(:Comment)
8
+ ActiveSupport::Dependencies.loaded = []
9
+ end
10
+
11
+ it 'should include only named context' do
12
+ Post.class_eval do
13
+ also_has :validations
14
+ end
15
+ Post.should respond_to(:test_validation_method)
16
+ Post.should_not respond_to(:test_stuff_method)
17
+ end
18
+
19
+ it 'should include all named contexts' do
20
+ Post.class_eval do
21
+ also_has :validations, :stuff
22
+ end
23
+ Post.should respond_to(:test_validation_method)
24
+ Post.should respond_to(:test_stuff_method)
25
+ end
26
+
27
+ end
@@ -0,0 +1,4 @@
1
+ Post.also_has do
2
+ def self.test_stuff_method
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ Post.also_has do
2
+ def self.test_validation_method
3
+ end
4
+ end
@@ -0,0 +1,2 @@
1
+ class Post < ActiveRecord::Base
2
+ end
@@ -0,0 +1,22 @@
1
+ $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $:.unshift(File.join(File.dirname(__FILE__), '..', 'spec'))
3
+
4
+ require 'rubygems'
5
+ require 'spec/autorun'
6
+
7
+ require 'active_record'
8
+ require 'active_support'
9
+ require 'not_only_but_also'
10
+
11
+ RAILS_ROOT = File.join(File.dirname(__FILE__), 'rails_root')
12
+
13
+ ActiveRecord::Migration.verbose = false
14
+ ActiveRecord::Base.establish_connection({:adapter => 'sqlite3', :database => ':memory:'})
15
+
16
+ ActiveRecord::Schema.define(:version => 1) do
17
+ create_table :posts, :force => true do |t|
18
+ t.string :title
19
+ end
20
+ end
21
+
22
+ ActiveSupport::Dependencies.load_paths = %W( #{RAILS_ROOT}/app #{RAILS_ROOT}/app/models )
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: not_only_but_also
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Adam Meehan
8
+ autorequire: not_only_but_also
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-11-06 00:00:00 +11:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Rails plugin to split large models into separate files of concern. Like concerned_with but a little more convenient.
17
+ email: adam.meehan@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README.rdoc
24
+ files:
25
+ - MIT-LICENSE
26
+ - README.rdoc
27
+ - Rakefile
28
+ - lib/not_only_but_also.rb
29
+ - spec/not_only_but_also_spec.rb
30
+ - spec/spec_helper.rb
31
+ - spec/rails_root/app/models/post/stuff.rb
32
+ - spec/rails_root/app/models/post/validations.rb
33
+ - spec/rails_root/app/models/post.rb
34
+ has_rdoc: true
35
+ homepage: http://github.com/adzap/not_only_but_also
36
+ licenses: []
37
+
38
+ post_install_message:
39
+ rdoc_options: []
40
+
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: "0"
48
+ version:
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "0"
54
+ version:
55
+ requirements: []
56
+
57
+ rubyforge_project: not_only_but_also
58
+ rubygems_version: 1.3.4
59
+ signing_key:
60
+ specification_version: 3
61
+ summary: Rails plugin to split large models into separate files of concern. Like concerned_with but a little more convenient.
62
+ test_files: []
63
+