activerecord_null_object 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ .DS_Store
2
+ test.db
3
+ pkg
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 [name of plugin creator]
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 ADDED
@@ -0,0 +1,5 @@
1
+ = ActiveRecordWithNullObject
2
+
3
+ Implements the Null Object Pattern for nil values in ActiveRecord associations.
4
+
5
+ Copyright (c) 2009 West Arete Computing, Inc., released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,25 @@
1
+ require 'spec'
2
+ require 'spec/rake/spectask'
3
+ require 'init'
4
+
5
+ desc 'Default: run specs.'
6
+ task :default => :spec
7
+
8
+ Spec::Rake::SpecTask.new do |t|
9
+ t.spec_opts = ['--options', "\"#{File.dirname(__FILE__)}/spec/spec.opts\""]
10
+ t.spec_files = FileList['spec/**/*_spec.rb']
11
+ end
12
+
13
+ begin
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |s|
16
+ s.name = %q{activerecord_null_object}
17
+ s.summary = %q{Implements the Null Object Pattern for nil values in ActiveRecord associations.}
18
+ s.email = %q{info@westarete.com}
19
+ s.homepage = %q{http://github.com/westarete/activerecord_null_object/}
20
+ s.description = ""
21
+ s.authors = ["Scott Woods"]
22
+ end
23
+ rescue LoadError
24
+ puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
25
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,54 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{activerecord_null_object}
5
+ s.version = "0.1.0"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Scott Woods"]
9
+ s.date = %q{2009-09-02}
10
+ s.description = %q{}
11
+ s.email = %q{info@westarete.com}
12
+ s.extra_rdoc_files = [
13
+ "README"
14
+ ]
15
+ s.files = [
16
+ ".gitignore",
17
+ "MIT-LICENSE",
18
+ "README",
19
+ "Rakefile",
20
+ "VERSION",
21
+ "activerecord_null_object.gemspec",
22
+ "init.rb",
23
+ "lib/activerecord_null_object.rb",
24
+ "lib/null_object.rb",
25
+ "spec/belongs_to_spec.rb",
26
+ "spec/database.yml",
27
+ "spec/null_object_spec.rb",
28
+ "spec/schema.rb",
29
+ "spec/spec.opts",
30
+ "spec/spec_helper.rb"
31
+ ]
32
+ s.has_rdoc = true
33
+ s.homepage = %q{http://github.com/westarete/activerecord_null_object/}
34
+ s.rdoc_options = ["--charset=UTF-8"]
35
+ s.require_paths = ["lib"]
36
+ s.rubygems_version = %q{1.3.1}
37
+ s.summary = %q{Implements the Null Object Pattern for nil values in ActiveRecord associations.}
38
+ s.test_files = [
39
+ "spec/belongs_to_spec.rb",
40
+ "spec/null_object_spec.rb",
41
+ "spec/schema.rb",
42
+ "spec/spec_helper.rb"
43
+ ]
44
+
45
+ if s.respond_to? :specification_version then
46
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
47
+ s.specification_version = 2
48
+
49
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
50
+ else
51
+ end
52
+ else
53
+ end
54
+ end
data/init.rb ADDED
@@ -0,0 +1,5 @@
1
+ require 'rubygems'
2
+ require 'activerecord'
3
+ ActiveRecord::ActiveRecordError # Workaround for loading bug in AR 2.3.2.
4
+ require File.dirname(__FILE__) + '/lib/activerecord_null_object'
5
+ require File.dirname(__FILE__) + '/lib/null_object'
@@ -0,0 +1,46 @@
1
+ module ActiveRecord
2
+ module Associations
3
+ module ClassMethods
4
+
5
+ # Add a boolean :null_object option to belongs_to.
6
+ def belongs_to_with_null_object(association_id, options = {}) #:nodoc:
7
+ # Extract our custom option.
8
+ use_null_object = options.delete(:null_object)
9
+ # Call the real belongs_to so that the association gets defined.
10
+ belongs_to_without_null_object(association_id, options)
11
+ # Modify the association if need be.
12
+ if use_null_object
13
+
14
+ # Determine the class of the association.
15
+ association_class_name = options[:class_name] || association_id.to_s.classify
16
+ association_class = association_class_name.constantize
17
+
18
+ # Determine the null class for this association.
19
+ null_class_name = "Null" + association_class_name
20
+ if Object.const_defined?(null_class_name)
21
+ null_class = Object.const_get(null_class_name.to_sym)
22
+ else
23
+ # Define the null class as an ancestor of the association class.
24
+ null_class = Object.const_set(null_class_name, Class.new(association_class))
25
+ null_class.class_eval do
26
+ include Singleton
27
+ include ActiveRecordNullObject::NullObject
28
+ end
29
+ end
30
+
31
+ # Modify the "getter" of the belongs_to relationship to return an
32
+ # instance of the association's null object instead of returning nil.
33
+ class_eval do
34
+ define_method("#{association_id}_with_null_object".to_sym) do
35
+ send("#{association_id}_without_null_object".to_sym) || null_class.instance
36
+ end
37
+ alias_method_chain association_id, :null_object
38
+ end
39
+
40
+ end
41
+ end
42
+ alias_method_chain :belongs_to, :null_object
43
+
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,21 @@
1
+ module ActiveRecordNullObject
2
+ module NullObject
3
+
4
+ def id
5
+ nil
6
+ end
7
+
8
+ def nil?
9
+ true
10
+ end
11
+
12
+ def empty?
13
+ true
14
+ end
15
+
16
+ def validate
17
+ errors.add_to_base("is a null object and can't be saved")
18
+ end
19
+
20
+ end
21
+ end
@@ -0,0 +1,93 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ describe Comment do
4
+ before(:each) do
5
+ @comment = Comment.create!(:body => 'The body.')
6
+ end
7
+
8
+ it "should contain a body" do
9
+ @comment.body.should == 'The body.'
10
+ end
11
+
12
+ describe "without an author" do
13
+ it "should return a null object for the author" do
14
+ @comment.author.kind_of?(NullAuthor).should be_true
15
+ end
16
+
17
+ it "should be nil" do
18
+ @comment.author.should be_nil
19
+ end
20
+
21
+ it "should return true for comment.author.nil?" do
22
+ @comment.author.nil?.should be_true
23
+ end
24
+
25
+ it "should return false for comment.author.present?" do
26
+ @comment.author.present?.should be_false
27
+ end
28
+
29
+ it "should return nil for the author's name" do
30
+ @comment.author.name.should be_nil
31
+ end
32
+ end
33
+
34
+ describe "with an author" do
35
+ before(:each) do
36
+ @comment.author = Author.create!(:name => 'James Baker')
37
+ end
38
+
39
+ it "should return the author object" do
40
+ @comment.author.kind_of?(Author).should be_true
41
+ @comment.author.kind_of?(NullAuthor).should_not be_true
42
+ end
43
+
44
+ it "should return false for comment.author.nil?" do
45
+ @comment.author.nil?.should be_false
46
+ end
47
+
48
+ it "should return true for comment.author.present?" do
49
+ @comment.author.present?.should be_true
50
+ end
51
+
52
+ it "should return the author's name" do
53
+ @comment.author.name.should == 'James Baker'
54
+ end
55
+
56
+ it "should revert to a null object if assigned nil" do
57
+ @comment.author = nil
58
+ @comment.author.name.should be_nil
59
+ end
60
+ end
61
+ end
62
+
63
+ describe Post do
64
+ before(:each) do
65
+ @post = Post.create!(:body => 'The body.')
66
+ end
67
+
68
+ it "should contain a body" do
69
+ @post.body.should == 'The body.'
70
+ end
71
+
72
+ it "should not have null object support for the author" do
73
+ @post.author.should be_nil
74
+ @post.author.kind_of?(Author).should be_false
75
+ @post.author.respond_to?(:name).should be_false
76
+ end
77
+ end
78
+
79
+ describe Session do
80
+ before(:each) do
81
+ @session = Session.create!(:description => 'The description.')
82
+ end
83
+
84
+ it "should contain a description" do
85
+ @session.description.should == 'The description.'
86
+ end
87
+
88
+ it "should not have null object support for the author" do
89
+ @session.author.should be_nil
90
+ @session.author.kind_of?(Author).should be_false
91
+ @session.author.respond_to?(:name).should be_false
92
+ end
93
+ end
data/spec/database.yml ADDED
@@ -0,0 +1,3 @@
1
+ test:
2
+ :adapter: sqlite3
3
+ :database: test.db
@@ -0,0 +1,99 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ describe NullAuthor do
4
+
5
+ before(:each) do
6
+ @null_author = Comment.new.author
7
+ end
8
+
9
+ it "should be a kind of Author" do
10
+ @null_author.kind_of?(Author).should be_true
11
+ end
12
+
13
+ it "should be a singleton" do
14
+ lambda { NullAuthor.new }.should raise_error(NoMethodError)
15
+ end
16
+
17
+ it "should be nil" do
18
+ @null_author.nil?.should be_true
19
+ @null_author.should be_nil
20
+ end
21
+
22
+ it "should be empty" do
23
+ @null_author.empty?.should be_true
24
+ end
25
+
26
+ it "should be blank" do
27
+ @null_author.blank?.should be_true
28
+ end
29
+
30
+ it "should not be present" do
31
+ @null_author.present?.should be_false
32
+ end
33
+
34
+ describe "#id" do
35
+ it "should return nil" do
36
+ @null_author.id.should be_nil
37
+ end
38
+ end
39
+
40
+ describe "#new_record?" do
41
+ it "should return true" do
42
+ @null_author.new_record?.should be_true
43
+ end
44
+ end
45
+
46
+ describe "#save" do
47
+ it "should return false" do
48
+ @null_author.save.should be_false
49
+ end
50
+
51
+ it "should not create a new record" do
52
+ lambda { @null_author.save }.should_not change { NullAuthor.count }
53
+ end
54
+
55
+ it "should add an error" do
56
+ @null_author.errors.on(:base).should == "is a null object and can't be saved"
57
+ end
58
+ end
59
+
60
+ describe "#save!" do
61
+ it "should raise an exception" do
62
+ lambda { @null_author.save! }.should raise_error(ActiveRecord::RecordInvalid)
63
+ end
64
+
65
+ it "should not create a new record" do
66
+ lambda {
67
+ begin
68
+ @null_author.save!
69
+ rescue ActiveRecord::RecordInvalid
70
+ end
71
+ }.should_not change { NullAuthor.count }
72
+ end
73
+
74
+ it "should add an error" do
75
+ @null_author.errors.on(:base).should == "is a null object and can't be saved"
76
+ end
77
+ end
78
+
79
+ describe "#update_attributes" do
80
+ it "should return false" do
81
+ @null_author.update_attributes({}).should be_false
82
+ end
83
+
84
+ it "should not create a new record" do
85
+ lambda { @null_author.update_attributes({}) }.should_not change { NullAuthor.count }
86
+ end
87
+
88
+ it "should add an error" do
89
+ @null_author.errors.on(:base).should == "is a null object and can't be saved"
90
+ end
91
+ end
92
+
93
+ describe "#name" do
94
+ it "should return nil" do
95
+ @null_author.name.should be_nil
96
+ end
97
+ end
98
+
99
+ end
data/spec/schema.rb ADDED
@@ -0,0 +1,28 @@
1
+ ActiveRecord::Schema.define(:version => 0) do
2
+ create_table "authors", :force => true do |t|
3
+ t.string "name"
4
+ t.datetime "created_at"
5
+ t.datetime "updated_at"
6
+ end
7
+
8
+ create_table "comments", :force => true do |t|
9
+ t.text "body"
10
+ t.integer "author_id"
11
+ t.datetime "created_at"
12
+ t.datetime "updated_at"
13
+ end
14
+
15
+ create_table "posts", :force => true do |t|
16
+ t.text "body"
17
+ t.integer "author_id"
18
+ t.datetime "created_at"
19
+ t.datetime "updated_at"
20
+ end
21
+
22
+ create_table "sessions", :force => true do |t|
23
+ t.text "description"
24
+ t.integer "author_id"
25
+ t.datetime "created_at"
26
+ t.datetime "updated_at"
27
+ end
28
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,29 @@
1
+ require 'rubygems'
2
+ require 'activerecord'
3
+ require 'yaml'
4
+ require 'spec'
5
+ require File.dirname(__FILE__) + '/../init.rb'
6
+
7
+ # Establish database connection.
8
+ config = YAML::load(File.open(File.dirname(__FILE__) + '/database.yml'))
9
+ ActiveRecord::Base.establish_connection(config['test'])
10
+ load(File.dirname(__FILE__) + "/schema.rb")
11
+
12
+ # Define ActiveRecord classes to use while testing.
13
+ class Author < ActiveRecord::Base
14
+ has_many :posts
15
+ has_many :comments
16
+ has_many :sessions
17
+ end
18
+
19
+ class Comment < ActiveRecord::Base
20
+ belongs_to :author, :null_object => true
21
+ end
22
+
23
+ class Post < ActiveRecord::Base
24
+ belongs_to :author, :null_object => false
25
+ end
26
+
27
+ class Session < ActiveRecord::Base
28
+ belongs_to :author
29
+ end
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activerecord_null_object
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Scott Woods
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-09-02 00:00:00 -04:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: ""
17
+ email: info@westarete.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README
24
+ files:
25
+ - .gitignore
26
+ - MIT-LICENSE
27
+ - README
28
+ - Rakefile
29
+ - VERSION
30
+ - activerecord_null_object.gemspec
31
+ - init.rb
32
+ - lib/activerecord_null_object.rb
33
+ - lib/null_object.rb
34
+ - spec/belongs_to_spec.rb
35
+ - spec/database.yml
36
+ - spec/null_object_spec.rb
37
+ - spec/schema.rb
38
+ - spec/spec.opts
39
+ - spec/spec_helper.rb
40
+ has_rdoc: true
41
+ homepage: http://github.com/westarete/activerecord_null_object/
42
+ licenses: []
43
+
44
+ post_install_message:
45
+ rdoc_options:
46
+ - --charset=UTF-8
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "0"
54
+ version:
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: "0"
60
+ version:
61
+ requirements: []
62
+
63
+ rubyforge_project:
64
+ rubygems_version: 1.3.5
65
+ signing_key:
66
+ specification_version: 2
67
+ summary: Implements the Null Object Pattern for nil values in ActiveRecord associations.
68
+ test_files:
69
+ - spec/belongs_to_spec.rb
70
+ - spec/null_object_spec.rb
71
+ - spec/schema.rb
72
+ - spec/spec_helper.rb