not_only_but_also 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +6 -46
- data/init.rb +1 -0
- data/lib/not_only_but_also.rb +9 -3
- data/lib/not_only_but_also/version.rb +3 -0
- data/not_only_but_also.gemspec +18 -0
- data/spec/not_only_but_also_spec.rb +62 -13
- data/spec/rails_root/app/models/post/bar.rb +4 -0
- data/spec/rails_root/app/models/post/foo.rb +4 -0
- data/spec/rails_root/app/models/post/stuff.rb +1 -4
- data/spec/rails_root/app/models/post/validations.rb +1 -4
- data/spec/spec_helper.rb +2 -0
- metadata +26 -15
data/Rakefile
CHANGED
@@ -1,43 +1,16 @@
|
|
1
|
-
require '
|
1
|
+
require 'bundler'
|
2
|
+
Bundler::GemHelper.install_tasks
|
3
|
+
|
2
4
|
require 'rake/rdoctask'
|
3
|
-
require 'rake/gempackagetask'
|
4
|
-
require 'rubygems/specification'
|
5
5
|
require 'rspec/core/rake_task'
|
6
6
|
|
7
|
-
GEM_NAME = "not_only_but_also"
|
8
|
-
GEM_VERSION = '1.0.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,generators}/**/*")
|
30
|
-
end
|
31
|
-
|
32
|
-
desc 'Default: run specs.'
|
33
|
-
task :default => :spec
|
34
|
-
|
35
7
|
desc "Run specs"
|
36
8
|
RSpec::Core::RakeTask.new(:spec)
|
37
9
|
|
38
10
|
desc "Generate code coverage"
|
39
11
|
RSpec::Core::RakeTask.new(:coverage) do |t|
|
40
12
|
t.rcov = true
|
13
|
+
t.rcov_opts = ['--exclude', 'spec']
|
41
14
|
end
|
42
15
|
|
43
16
|
desc 'Generate documentation for plugin.'
|
@@ -49,18 +22,5 @@ Rake::RDocTask.new(:rdoc) do |rdoc|
|
|
49
22
|
rdoc.rdoc_files.include('lib/**/*.rb')
|
50
23
|
end
|
51
24
|
|
52
|
-
|
53
|
-
|
54
|
-
end
|
55
|
-
|
56
|
-
desc "install the gem locally"
|
57
|
-
task :install => [:package] do
|
58
|
-
sh %{sudo gem install pkg/#{GEM_NAME}-#{GEM_VERSION}}
|
59
|
-
end
|
60
|
-
|
61
|
-
desc "create a gemspec file"
|
62
|
-
task :make_spec do
|
63
|
-
File.open("#{GEM_NAME}.gemspec", "w") do |file|
|
64
|
-
file.puts spec.to_ruby
|
65
|
-
end
|
66
|
-
end
|
25
|
+
desc 'Default: run specs.'
|
26
|
+
task :default => :spec
|
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'not_only_but_also'
|
data/lib/not_only_but_also.rb
CHANGED
@@ -3,11 +3,14 @@ module NotOnlyButAlso
|
|
3
3
|
module ClassMethods
|
4
4
|
|
5
5
|
def not_only_but_also(*contexts, &block)
|
6
|
+
@_not_only_but_also_contexts ||= {}
|
6
7
|
if block_given?
|
7
|
-
|
8
|
+
context = contexts.first || NotOnlyButAlso::Helpers.context_name_from_file(caller.first)
|
9
|
+
@_not_only_but_also_contexts[context] = block
|
8
10
|
else
|
9
11
|
contexts.each do |context|
|
10
12
|
NotOnlyButAlso::Helpers.require_context_file(name, context)
|
13
|
+
class_eval &@_not_only_but_also_contexts[context]
|
11
14
|
end
|
12
15
|
end
|
13
16
|
end
|
@@ -19,10 +22,13 @@ module NotOnlyButAlso
|
|
19
22
|
|
20
23
|
def self.require_context_file(class_name, context)
|
21
24
|
require_dependency "#{class_name.underscore}/#{context}"
|
22
|
-
rescue MissingSourceFile =>
|
23
|
-
raise
|
25
|
+
rescue MissingSourceFile => ex
|
26
|
+
raise ex, "NotOnlyButAlso could not find a file for #{class_name} using context #{context}"
|
24
27
|
end
|
25
28
|
|
29
|
+
def self.context_name_from_file(filename)
|
30
|
+
filename.split('/').last.split('.').first.to_sym
|
31
|
+
end
|
26
32
|
end
|
27
33
|
|
28
34
|
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "not_only_but_also/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "not_only_but_also"
|
7
|
+
s.version = NotOnlyButAlso::VERSION
|
8
|
+
s.authors = ["Adam Meehan"]
|
9
|
+
s.description = %q{Rails plugin to split large models into separate files of concern. Like concerned_with but more convenient.}
|
10
|
+
s.summary = s.description
|
11
|
+
s.email = %q{adam.meehan@gmail.com}
|
12
|
+
s.homepage = %q{http://github.com/adzap/not_only_but_also}
|
13
|
+
|
14
|
+
s.require_paths = ["lib"]
|
15
|
+
s.files = `git ls-files`.split("\n") - %w{ .gitignore .rspec Gemfile Gemfile.lock autotest/discover.rb }
|
16
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
17
|
+
s.extra_rdoc_files = ["README.rdoc"]
|
18
|
+
end
|
@@ -5,44 +5,93 @@ describe NotOnlyButAlso do
|
|
5
5
|
Object.send(:remove_const, :Post) if Object.const_defined?(:Post)
|
6
6
|
Object.send(:remove_const, :Comment) if Object.const_defined?(:Comment)
|
7
7
|
ActiveSupport::Dependencies.loaded = []
|
8
|
+
Post.class_eval do
|
9
|
+
class << self
|
10
|
+
attr_accessor :_not_only_but_also_contexts
|
11
|
+
end
|
12
|
+
end
|
8
13
|
end
|
9
14
|
|
10
15
|
describe ".not_only_but_also" do
|
11
|
-
context "with array of symbols" do
|
12
|
-
it 'should require symbols as
|
16
|
+
context "with array of symbols and no block" do
|
17
|
+
it 'should require symbols as files' do
|
13
18
|
NotOnlyButAlso::Helpers.should_receive(:require_context_file).with('Post', :validations)
|
19
|
+
Post._not_only_but_also_contexts = {:validations => lambda { } }
|
20
|
+
|
14
21
|
Post.also_has :validations
|
15
22
|
end
|
16
|
-
end
|
17
23
|
|
18
|
-
context "with block" do
|
19
24
|
it 'should evaluate the block in the class context' do
|
20
|
-
Post
|
21
|
-
|
22
|
-
|
25
|
+
NotOnlyButAlso::Helpers.stub(:require_context_file).with('Post', :validations)
|
26
|
+
Post._not_only_but_also_contexts = {:validations => lambda { def self.method_in_block; end } }
|
27
|
+
Post.also_has :validations
|
28
|
+
|
23
29
|
Post.should respond_to(:method_in_block)
|
24
30
|
end
|
25
31
|
end
|
32
|
+
|
33
|
+
context "with a block" do
|
34
|
+
|
35
|
+
context "and a context name" do
|
36
|
+
it 'should store the block using context name provided' do
|
37
|
+
Post.also_has(:validations) { }
|
38
|
+
|
39
|
+
Post._not_only_but_also_contexts[:validations].should be_kind_of(Proc)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
context "and no context name" do
|
44
|
+
it 'should determine context name from file' do
|
45
|
+
Dir.mktmpdir do |dir|
|
46
|
+
filename = "#{dir}/validations.rb"
|
47
|
+
File.open(filename, 'w+') { |f|
|
48
|
+
f.puts "Post.also_has { raise }"
|
49
|
+
}
|
50
|
+
|
51
|
+
load filename
|
52
|
+
|
53
|
+
Post._not_only_but_also_contexts.should have_key(:validations)
|
54
|
+
Post._not_only_but_also_contexts[:validations].should be_kind_of(Proc)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
26
60
|
end
|
27
61
|
|
28
62
|
describe NotOnlyButAlso::Helpers do
|
29
63
|
describe ".require_context_file" do
|
30
64
|
it 'should require context file in folder with underscored class name' do
|
31
65
|
NotOnlyButAlso::Helpers.should_receive(:require_dependency).with('post/validations')
|
66
|
+
Post._not_only_but_also_contexts = {:validations => lambda { } }
|
67
|
+
|
32
68
|
Post.also_has :validations
|
33
69
|
end
|
34
70
|
end
|
71
|
+
|
72
|
+
describe ".context_name_from_file" do
|
73
|
+
it 'should return a symbol from the filename without the file extension' do
|
74
|
+
NotOnlyButAlso::Helpers.context_name_from_file(__FILE__).should == :not_only_but_also_spec
|
75
|
+
end
|
76
|
+
end
|
35
77
|
end
|
36
78
|
|
37
79
|
context "integration" do
|
38
|
-
|
39
|
-
|
40
|
-
|
80
|
+
before do
|
81
|
+
# Forced load all files to simulate no autoloading.
|
82
|
+
Dir['spec/rails_root/app/models/post/*.rb'].each {|file| require file }
|
83
|
+
end
|
84
|
+
|
85
|
+
it 'should load files from named contexts' do
|
86
|
+
Post.should_not respond_to(:class_method_from_validations)
|
87
|
+
Post.should_not respond_to(:class_method_from_foo)
|
88
|
+
Post.should_not respond_to(:class_method_from_bar)
|
41
89
|
|
42
|
-
Post.also_has :validations, :
|
90
|
+
Post.also_has :validations, :foo
|
43
91
|
|
44
|
-
Post.should respond_to(:
|
45
|
-
Post.should respond_to(:
|
92
|
+
Post.should respond_to(:class_method_from_validations)
|
93
|
+
Post.should respond_to(:class_method_from_foo)
|
94
|
+
Post.should_not respond_to(:class_method_from_bar)
|
46
95
|
end
|
47
96
|
end
|
48
97
|
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,25 +1,25 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: not_only_but_also
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
5
|
-
prerelease:
|
4
|
+
hash: 19
|
5
|
+
prerelease:
|
6
6
|
segments:
|
7
7
|
- 1
|
8
|
+
- 1
|
8
9
|
- 0
|
9
|
-
|
10
|
-
version: 1.0.0
|
10
|
+
version: 1.1.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Adam Meehan
|
14
|
-
autorequire:
|
14
|
+
autorequire:
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-
|
18
|
+
date: 2011-06-14 00:00:00 +10:00
|
19
19
|
default_executable:
|
20
20
|
dependencies: []
|
21
21
|
|
22
|
-
description: Rails plugin to split large models into separate files of concern. Like concerned_with but
|
22
|
+
description: Rails plugin to split large models into separate files of concern. Like concerned_with but more convenient.
|
23
23
|
email: adam.meehan@gmail.com
|
24
24
|
executables: []
|
25
25
|
|
@@ -31,14 +31,19 @@ files:
|
|
31
31
|
- MIT-LICENSE
|
32
32
|
- README.rdoc
|
33
33
|
- Rakefile
|
34
|
+
- generators/not_only_but_also/not_only_but_also_generator.rb
|
35
|
+
- generators/not_only_but_also/templates/context.erb
|
36
|
+
- init.rb
|
34
37
|
- lib/not_only_but_also.rb
|
38
|
+
- lib/not_only_but_also/version.rb
|
39
|
+
- not_only_but_also.gemspec
|
35
40
|
- spec/not_only_but_also_spec.rb
|
41
|
+
- spec/rails_root/app/models/post.rb
|
42
|
+
- spec/rails_root/app/models/post/bar.rb
|
43
|
+
- spec/rails_root/app/models/post/foo.rb
|
36
44
|
- spec/rails_root/app/models/post/stuff.rb
|
37
45
|
- spec/rails_root/app/models/post/validations.rb
|
38
|
-
- spec/rails_root/app/models/post.rb
|
39
46
|
- spec/spec_helper.rb
|
40
|
-
- generators/not_only_but_also/not_only_but_also_generator.rb
|
41
|
-
- generators/not_only_but_also/templates/context.erb
|
42
47
|
has_rdoc: true
|
43
48
|
homepage: http://github.com/adzap/not_only_but_also
|
44
49
|
licenses: []
|
@@ -68,10 +73,16 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
68
73
|
version: "0"
|
69
74
|
requirements: []
|
70
75
|
|
71
|
-
rubyforge_project:
|
72
|
-
rubygems_version: 1.
|
76
|
+
rubyforge_project:
|
77
|
+
rubygems_version: 1.5.2
|
73
78
|
signing_key:
|
74
79
|
specification_version: 3
|
75
|
-
summary: Rails plugin to split large models into separate files of concern. Like concerned_with but
|
76
|
-
test_files:
|
77
|
-
|
80
|
+
summary: Rails plugin to split large models into separate files of concern. Like concerned_with but more convenient.
|
81
|
+
test_files:
|
82
|
+
- spec/not_only_but_also_spec.rb
|
83
|
+
- spec/rails_root/app/models/post.rb
|
84
|
+
- spec/rails_root/app/models/post/bar.rb
|
85
|
+
- spec/rails_root/app/models/post/foo.rb
|
86
|
+
- spec/rails_root/app/models/post/stuff.rb
|
87
|
+
- spec/rails_root/app/models/post/validations.rb
|
88
|
+
- spec/spec_helper.rb
|