corus 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,6 @@
1
+ pkg/*
2
+ *.gem
3
+ .bundle
4
+ .rvmrc
5
+ corus.db
6
+ README.html
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in tartarus.gemspec
4
+ gemspec
@@ -0,0 +1,34 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ corus (0.0.1)
5
+ activerecord (~> 2.3.12)
6
+
7
+ GEM
8
+ remote: http://rubygems.org/
9
+ specs:
10
+ activerecord (2.3.12)
11
+ activesupport (= 2.3.12)
12
+ activesupport (2.3.12)
13
+ diff-lcs (1.1.2)
14
+ mocha (0.9.12)
15
+ rake (0.9.2)
16
+ rspec (2.6.0)
17
+ rspec-core (~> 2.6.0)
18
+ rspec-expectations (~> 2.6.0)
19
+ rspec-mocks (~> 2.6.0)
20
+ rspec-core (2.6.4)
21
+ rspec-expectations (2.6.0)
22
+ diff-lcs (~> 1.1.2)
23
+ rspec-mocks (2.6.0)
24
+ sqlite3 (1.3.3)
25
+
26
+ PLATFORMS
27
+ ruby
28
+
29
+ DEPENDENCIES
30
+ corus!
31
+ mocha (~> 0.9.12)
32
+ rake (~> 0.9.2)
33
+ rspec (~> 2.6.0)
34
+ sqlite3 (~> 1.3.3)
data/LICENSE ADDED
@@ -0,0 +1,24 @@
1
+ This is free and unencumbered software released into the public domain.
2
+
3
+ Anyone is free to copy, modify, publish, use, compile, sell, or
4
+ distribute this software, either in source code form or as a compiled
5
+ binary, for any purpose, commercial or non-commercial, and by any
6
+ means.
7
+
8
+ In jurisdictions that recognize copyright laws, the author or authors
9
+ of this software dedicate any and all copyright interest in the
10
+ software to the public domain. We make this dedication for the benefit
11
+ of the public at large and to the detriment of our heirs and
12
+ successors. We intend this dedication to be an overt act of
13
+ relinquishment in perpetuity of all present and future rights to this
14
+ software under copyright law.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
+ IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20
+ OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21
+ ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ OTHER DEALINGS IN THE SOFTWARE.
23
+
24
+ For more information, please refer to <http://unlicense.org/>
@@ -0,0 +1,32 @@
1
+ # Corus
2
+
3
+ Use this gem to define what is not nullable related to an ActiveRecord backed class
4
+
5
+ ### Install
6
+
7
+ config.gem 'corus'
8
+
9
+ ### Usage
10
+
11
+ ```ruby
12
+ class Foo < ActiveRecord::Base
13
+ tartarus :bar
14
+ end
15
+
16
+ thing = Foo.create(:bar => :baz)
17
+ thing.bar = nil
18
+ thing.valid? #=> false
19
+
20
+ thing.save! #=> #<ActiveRecord::RecordInvalid: Validation failed: Bar can't be changed from 'baz' to nil>
21
+
22
+ thing.save #=> false
23
+
24
+ thing.bar = "widget"
25
+ thing.valid? #=> true
26
+
27
+ things.save #=> true
28
+ ```
29
+
30
+ ### License
31
+
32
+ See LICENSE for information.
@@ -0,0 +1,8 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require "rspec/core/rake_task"
5
+ RSpec::Core::RakeTask.new(:spec) do |t|
6
+ t.rspec_opts = %w( --color )
7
+ end
8
+ task :default => :spec
@@ -0,0 +1,28 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "corus/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "corus"
7
+ s.version = Corus::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Duncan Grazier"]
10
+ s.email = ["duncan@impossiblerocket.com"]
11
+ s.homepage = "https://github.com/itsmeduncan/corus"
12
+ s.summary = %q{Define what is not nullable related to an ActiveRecord backed class}
13
+ s.description = %q{Use this gem to define what is not nullable related to an ActiveRecord backed class}
14
+
15
+ s.rubyforge_project = "corus"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+
22
+ s.add_development_dependency("rake", ["~> 0.9.2"])
23
+ s.add_development_dependency("rspec", ["~> 2.6.0"])
24
+ s.add_development_dependency("mocha", ["~> 0.9.12"])
25
+ s.add_development_dependency("sqlite3", ["~> 1.3.3"])
26
+
27
+ s.add_dependency("activerecord", ["~> 2.3.12"])
28
+ end
@@ -0,0 +1,3 @@
1
+ require 'active_record'
2
+
3
+ require 'corus/base'
@@ -0,0 +1,35 @@
1
+ module Corus::Base
2
+ CLASS_VARIABLE_NAME = :@@_corus_fields
3
+
4
+ def self.included(base)
5
+ base.extend(ClassMethods)
6
+ end
7
+
8
+ module ClassMethods
9
+ def corus *fields
10
+ class_variable_set(Corus::Base::CLASS_VARIABLE_NAME, fields)
11
+
12
+ validate :_corus_nonnullablity
13
+ end
14
+ end
15
+
16
+ private
17
+
18
+ def _corus_nonnullablity
19
+ _corus_fields.each do |attribute|
20
+ original_value = self.send("#{attribute}_was".to_sym)
21
+ new_value = self.send(attribute)
22
+
23
+ if !original_value.nil? && new_value.nil?
24
+ errors.add(attribute, "can't be changed from '#{original_value}' to nil")
25
+ end
26
+ end
27
+ end
28
+
29
+ def _corus_fields
30
+ @_corus_fields ||= self.class.send(:class_variable_get, Corus::Base::CLASS_VARIABLE_NAME)
31
+ end
32
+
33
+ end
34
+
35
+ ActiveRecord::Base.send :include, Corus::Base
@@ -0,0 +1,3 @@
1
+ module Corus
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,54 @@
1
+ require 'spec_helper'
2
+
3
+ describe Corus::Base do
4
+
5
+ ActiveRecord::Base.connection.execute("DROP TABLE IF EXISTS 'foos'")
6
+ ActiveRecord::Base.connection.create_table(:foos) do |t|
7
+ t.string :foo
8
+ t.string :bar
9
+ t.string :widget
10
+ end
11
+
12
+ class Foo < ActiveRecord::Base
13
+ corus :foo, :bar, :widget
14
+ end
15
+
16
+ before(:each) do
17
+ ActiveRecord::Base.connection.increment_open_transactions
18
+ ActiveRecord::Base.connection.begin_db_transaction
19
+ end
20
+
21
+ after(:each) do
22
+ ActiveRecord::Base.connection.rollback_db_transaction
23
+ ActiveRecord::Base.connection.decrement_open_transactions
24
+ end
25
+
26
+ [:foo, :bar].each do |attribute|
27
+ it "is invalid when overwriting :#{attribute} with a null value" do
28
+ thing = Foo.create(attribute => "baz")
29
+ thing.send("#{attribute}=".to_sym, nil)
30
+ thing.should_not be_valid
31
+ end
32
+
33
+ it "is valid when :#{attribute} is nil at creation time" do
34
+ thing = Foo.new(attribute => "baz")
35
+ thing.should be_valid
36
+ end
37
+ end
38
+
39
+ it "is valid when changing :widget to null" do
40
+ thing = Foo.create(:widget => "baz")
41
+ thing.bar = nil
42
+ thing.should be_valid
43
+ end
44
+
45
+ it "gives a helpful message" do
46
+ thing = Foo.create(:foo => "baz")
47
+ thing.foo = nil
48
+
49
+ lambda {
50
+ thing.save!
51
+ }.should raise_error(ActiveRecord::RecordInvalid, "Validation failed: Foo can't be changed from 'baz' to nil")
52
+ end
53
+
54
+ end
@@ -0,0 +1,16 @@
1
+ require "bundler"
2
+ Bundler.setup
3
+
4
+ $:.unshift(File.dirname(__FILE__) + '/../lib')
5
+
6
+ require 'corus'
7
+
8
+ RSpec.configure do |config|
9
+ config.mock_with :mocha
10
+ end
11
+
12
+ root = File.expand_path(File.join(File.dirname(__FILE__), '..'))
13
+ ActiveRecord::Base.establish_connection(
14
+ :adapter => "sqlite3",
15
+ :database => "#{root}/tmp/corus.db"
16
+ )
File without changes
metadata ADDED
@@ -0,0 +1,160 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: corus
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Duncan Grazier
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-07-12 00:00:00 -04:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rake
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ hash: 63
30
+ segments:
31
+ - 0
32
+ - 9
33
+ - 2
34
+ version: 0.9.2
35
+ type: :development
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: rspec
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ hash: 23
46
+ segments:
47
+ - 2
48
+ - 6
49
+ - 0
50
+ version: 2.6.0
51
+ type: :development
52
+ version_requirements: *id002
53
+ - !ruby/object:Gem::Dependency
54
+ name: mocha
55
+ prerelease: false
56
+ requirement: &id003 !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ hash: 35
62
+ segments:
63
+ - 0
64
+ - 9
65
+ - 12
66
+ version: 0.9.12
67
+ type: :development
68
+ version_requirements: *id003
69
+ - !ruby/object:Gem::Dependency
70
+ name: sqlite3
71
+ prerelease: false
72
+ requirement: &id004 !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ hash: 29
78
+ segments:
79
+ - 1
80
+ - 3
81
+ - 3
82
+ version: 1.3.3
83
+ type: :development
84
+ version_requirements: *id004
85
+ - !ruby/object:Gem::Dependency
86
+ name: activerecord
87
+ prerelease: false
88
+ requirement: &id005 !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ~>
92
+ - !ruby/object:Gem::Version
93
+ hash: 27
94
+ segments:
95
+ - 2
96
+ - 3
97
+ - 12
98
+ version: 2.3.12
99
+ type: :runtime
100
+ version_requirements: *id005
101
+ description: Use this gem to define what is not nullable related to an ActiveRecord backed class
102
+ email:
103
+ - duncan@impossiblerocket.com
104
+ executables: []
105
+
106
+ extensions: []
107
+
108
+ extra_rdoc_files: []
109
+
110
+ files:
111
+ - .gitignore
112
+ - Gemfile
113
+ - Gemfile.lock
114
+ - LICENSE
115
+ - README.markdown
116
+ - Rakefile
117
+ - corus.gemspec
118
+ - lib/corus.rb
119
+ - lib/corus/base.rb
120
+ - lib/corus/version.rb
121
+ - spec/corus/base_spec.rb
122
+ - spec/spec_helper.rb
123
+ - tmp/.gitkeep
124
+ has_rdoc: true
125
+ homepage: https://github.com/itsmeduncan/corus
126
+ licenses: []
127
+
128
+ post_install_message:
129
+ rdoc_options: []
130
+
131
+ require_paths:
132
+ - lib
133
+ required_ruby_version: !ruby/object:Gem::Requirement
134
+ none: false
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ hash: 3
139
+ segments:
140
+ - 0
141
+ version: "0"
142
+ required_rubygems_version: !ruby/object:Gem::Requirement
143
+ none: false
144
+ requirements:
145
+ - - ">="
146
+ - !ruby/object:Gem::Version
147
+ hash: 3
148
+ segments:
149
+ - 0
150
+ version: "0"
151
+ requirements: []
152
+
153
+ rubyforge_project: corus
154
+ rubygems_version: 1.3.7
155
+ signing_key:
156
+ specification_version: 3
157
+ summary: Define what is not nullable related to an ActiveRecord backed class
158
+ test_files:
159
+ - spec/corus/base_spec.rb
160
+ - spec/spec_helper.rb