with_model 0.1

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.
@@ -0,0 +1,5 @@
1
+ Autotest.add_hook :initialize do |at|
2
+ at.add_mapping(%r%^lib/.*\.rb$%, true) { |filename, _|
3
+ at.files_matching %r%^spec/.*_spec.rb$%
4
+ }
5
+ end
@@ -0,0 +1,5 @@
1
+ .idea
2
+ pkg/*
3
+ *.gem
4
+ .bundle
5
+ .rvmrc
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ puts <<-MESSAGE
2
+ This project uses multiple Gemfiles in subdirectories of ./gemfiles.
3
+ The rake tasks automatically install these bundles as necessary. See rake -T.
4
+ MESSAGE
5
+ exit 1
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2010 Case Commons, LLC
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
data/README ADDED
@@ -0,0 +1 @@
1
+ See spec/spec_helper.rb and spec/with_model_spec.rb for usage.
@@ -0,0 +1,35 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ environments = %w[rspec1 rspec2]
5
+
6
+ in_environment = lambda do |environment, command|
7
+ sh %Q{export BUNDLE_GEMFILE="gemfiles/#{environment}/Gemfile"; bundle --quiet && bundle exec #{command}}
8
+ end
9
+
10
+ in_all_environments = lambda do |command|
11
+ environments.each do |environment|
12
+ puts "\n---#{environment}---\n"
13
+ in_environment.call(environment, command)
14
+ end
15
+ end
16
+
17
+ autotest_styles = {
18
+ :rspec1 => 'rspec',
19
+ :rspec2 => 'rspec2'
20
+ }
21
+
22
+ desc "Run all specs against Rspec 1 and 2"
23
+ task "spec" do
24
+ in_environment.call('rspec1', 'spec spec')
25
+ in_environment.call('rspec2', 'rspec spec')
26
+ end
27
+
28
+ namespace "autotest" do
29
+ environments.each do |environment|
30
+ desc "Run autotest in #{environment}"
31
+ task environment do
32
+ in_environment.call(environment, "autotest -s #{autotest_styles[environment.to_sym]}")
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,8 @@
1
+ source :rubygems
2
+
3
+ # gemspec :path => File.join(File.dirname(__FILE__), '..')
4
+
5
+ group :test do
6
+ gem "autotest"
7
+ gem "sqlite3-ruby"
8
+ end
@@ -0,0 +1,5 @@
1
+ filename = File.join(File.dirname(__FILE__), '..', 'Gemfile.common')
2
+ eval(File.read(filename), binding, filename, 1)
3
+
4
+ gem "rspec", "~>1.0"
5
+ gem "activerecord", "~>2.3.5"
@@ -0,0 +1,18 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ activerecord (2.3.5)
5
+ activesupport (= 2.3.5)
6
+ activesupport (2.3.5)
7
+ autotest (4.4.2)
8
+ rspec (1.3.1)
9
+ sqlite3-ruby (1.3.2)
10
+
11
+ PLATFORMS
12
+ ruby
13
+
14
+ DEPENDENCIES
15
+ activerecord (~> 2.3.5)
16
+ autotest
17
+ rspec (~> 1.0)
18
+ sqlite3-ruby
@@ -0,0 +1,6 @@
1
+ filename = File.join(File.dirname(__FILE__), '..', 'Gemfile.common')
2
+ eval(File.read(filename), binding, filename, 1)
3
+
4
+ gem "rspec", "~>2.0"
5
+ gem "activerecord", "~>3.0.0"
6
+
@@ -0,0 +1,37 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ activemodel (3.0.3)
5
+ activesupport (= 3.0.3)
6
+ builder (~> 2.1.2)
7
+ i18n (~> 0.4)
8
+ activerecord (3.0.3)
9
+ activemodel (= 3.0.3)
10
+ activesupport (= 3.0.3)
11
+ arel (~> 2.0.2)
12
+ tzinfo (~> 0.3.23)
13
+ activesupport (3.0.3)
14
+ arel (2.0.4)
15
+ autotest (4.4.4)
16
+ builder (2.1.2)
17
+ diff-lcs (1.1.2)
18
+ i18n (0.4.2)
19
+ rspec (2.1.0)
20
+ rspec-core (~> 2.1.0)
21
+ rspec-expectations (~> 2.1.0)
22
+ rspec-mocks (~> 2.1.0)
23
+ rspec-core (2.1.0)
24
+ rspec-expectations (2.1.0)
25
+ diff-lcs (~> 1.1.2)
26
+ rspec-mocks (2.1.0)
27
+ sqlite3-ruby (1.3.2)
28
+ tzinfo (0.3.23)
29
+
30
+ PLATFORMS
31
+ ruby
32
+
33
+ DEPENDENCIES
34
+ activerecord (~> 3.0.0)
35
+ autotest
36
+ rspec (~> 2.0)
37
+ sqlite3-ruby
@@ -0,0 +1,19 @@
1
+ require "with_model/dsl"
2
+
3
+ module WithModel
4
+ def with_model(name, &block)
5
+ Dsl.new(name, self).instance_eval(&block)
6
+ end
7
+
8
+ def with_table(name, &block)
9
+ connection = ActiveRecord::Base.connection
10
+ before do
11
+ connection.drop_table(name) if connection.table_exists?(name)
12
+ connection.create_table(name, &block)
13
+ end
14
+
15
+ after do
16
+ connection.drop_table(name) rescue nil
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,50 @@
1
+ module WithModel
2
+ class Dsl
3
+ attr_reader :model_initialization
4
+
5
+ def initialize(name, example_group)
6
+ dsl = self
7
+
8
+ @example_group = example_group
9
+ @table_name = table_name = "with_model_#{name}_#{$$}"
10
+ @model_initialization = lambda {|*|}
11
+
12
+ const_name = name.to_s.camelize.to_sym
13
+
14
+ original_const_defined = Object.const_defined?(const_name)
15
+ original_const_value = Object.const_get(const_name) if original_const_defined
16
+
17
+ example_group.class_eval do
18
+ attr_accessor name
19
+ end
20
+
21
+ example_group.before do
22
+ model = Class.new(ActiveRecord::Base)
23
+ silence_warnings { Object.const_set(const_name, model) }
24
+ Object.const_get(const_name).class_eval do
25
+ set_table_name table_name
26
+ class << self
27
+ def with_model?
28
+ true
29
+ end
30
+ end
31
+ self.class_eval(&dsl.model_initialization)
32
+ end
33
+ send("#{name}=", model)
34
+ end
35
+
36
+ example_group.after do
37
+ Object.send(:remove_const, const_name)
38
+ Object.const_set(const_name, original_const_value) if original_const_defined
39
+ end
40
+ end
41
+
42
+ def table(&block)
43
+ @example_group.with_table(@table_name, &block)
44
+ end
45
+
46
+ def model(&block)
47
+ @model_initialization = block
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,3 @@
1
+ module WithModel
2
+ VERSION = "0.1"
3
+ end
@@ -0,0 +1,47 @@
1
+ require 'spec_helper'
2
+
3
+ describe "ActiveRecord behaviors" do
4
+ describe "a temporary ActiveRecord model created with with_model that has a named_scope" do
5
+ before do
6
+ class RegularModel < ActiveRecord::Base
7
+ named_scope :title_is_foo, :conditions => {:title => 'foo'}
8
+ end
9
+ RegularModel.connection.drop_table(RegularModel.table_name) rescue nil
10
+ RegularModel.connection.create_table(RegularModel.table_name) do |t|
11
+ t.string 'title'
12
+ t.text 'content'
13
+ t.timestamps
14
+ end
15
+ end
16
+
17
+ after do
18
+ RegularModel.connection.drop_table(@model.table_name) rescue nil
19
+ end
20
+
21
+ with_model :blog_post do
22
+ table do |t|
23
+ t.string 'title'
24
+ t.text 'content'
25
+ t.timestamps
26
+ end
27
+
28
+ model do
29
+ named_scope :title_is_foo, :conditions => {:title => 'foo'}
30
+ end
31
+ end
32
+
33
+ describe "the named scope" do
34
+ it "should work like a regular named scope" do
35
+ included = RegularModel.create!(:title => 'foo', :content => "Include me!")
36
+ excluded = RegularModel.create!(:title => 'bar', :content => "Include me!")
37
+
38
+ RegularModel.title_is_foo.should == [included]
39
+
40
+ included = BlogPost.create!(:title => 'foo', :content => "Include me!")
41
+ excluded = BlogPost.create!(:title => 'bar', :content => "Include me!")
42
+
43
+ BlogPost.title_is_foo.should == [included]
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,18 @@
1
+ require "active_record"
2
+ require "with_model"
3
+
4
+ if defined?(RSpec)
5
+ # For RSpec 2 users.
6
+ RSpec.configure do |config|
7
+ config.extend WithModel
8
+ end
9
+ else
10
+ # For RSpec 1 users.
11
+ Spec::Runner.configure do |config|
12
+ config.extend WithModel
13
+ end
14
+ end
15
+
16
+ # WithModel requires ActiveRecord::Base.connection to be established.
17
+ # If ActiveRecord already has a connection, as in a Rails app, this is unnecessary.
18
+ ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ":memory:")
@@ -0,0 +1,98 @@
1
+ require 'spec_helper'
2
+
3
+ describe "a temporary ActiveRecord model created with with_model" do
4
+ non_shadowing_example_ran = false
5
+
6
+ describe "which doesn't shadow an existing class" do
7
+ with_model :blog_post do
8
+ table do |t|
9
+ t.string 'title'
10
+ t.text 'content'
11
+ t.timestamps
12
+ end
13
+
14
+ model do
15
+ def fancy_title
16
+ "Title: #{title}"
17
+ end
18
+ end
19
+ end
20
+
21
+ after do
22
+ non_shadowing_example_ran = true
23
+ end
24
+
25
+ it "should act like a normal ActiveRecord model" do
26
+ record = blog_post.create!(:title => 'New blog post', :content => "Hello, world!")
27
+
28
+ record.reload
29
+
30
+ record.title.should == 'New blog post'
31
+ record.content.should == 'Hello, world!'
32
+ record.updated_at.should be_present
33
+
34
+ record.destroy
35
+
36
+ lambda {
37
+ record.reload
38
+ }.should raise_error(ActiveRecord::RecordNotFound)
39
+ end
40
+
41
+ it "should have methods defined in its model block" do
42
+ blog_post.new(:title => 'New blog post').fancy_title.should == "Title: New blog post"
43
+ end
44
+
45
+ it "should define a constant" do
46
+ BlogPost.should == blog_post
47
+ end
48
+
49
+ describe ".with_model?" do
50
+ it "should return true" do
51
+ BlogPost.with_model?.should be_true
52
+ end
53
+ end
54
+ end
55
+
56
+ context "after an example which uses with_model without shadowing an existing constant" do
57
+ it "should return the constant to its undefined state" do
58
+ non_shadowing_example_ran.should be_true
59
+ defined?(BlogPost).should be_false
60
+ end
61
+ end
62
+
63
+ ::MyConst = 1
64
+
65
+ shadowing_example_ran = false
66
+
67
+ describe "that shadows an existing constant" do
68
+ with_model :my_const do
69
+ table do
70
+ end
71
+ end
72
+
73
+ after do
74
+ shadowing_example_ran = true
75
+ end
76
+
77
+ it "should shadow that constant" do
78
+ MyConst.should == my_const
79
+ end
80
+ end
81
+
82
+ context "in later examples" do
83
+ it "should return the constant to its original value" do
84
+ shadowing_example_ran.should be_true
85
+ MyConst.should == 1
86
+ end
87
+ end
88
+
89
+ describe "with a plural name" do
90
+ with_model :blog_posts do
91
+ table {}
92
+ end
93
+
94
+ it "should not singularize the constant name" do
95
+ BlogPosts.should == blog_posts
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "with_model/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "with_model"
7
+ s.version = WithModel::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Case Commons, LLC"]
10
+ s.email = ["casecommons-dev@googlegroups.com"]
11
+ s.homepage = "https://github.com/Casecommons/with_model"
12
+ s.summary = %q{Dynamically build a model within an Rspec context}
13
+ s.description = s.summary
14
+
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
+ s.require_paths = ["lib"]
19
+
20
+ s.add_dependency 'activerecord', '>=2.3.5', '<4.0.0'
21
+ end
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: with_model
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ version: "0.1"
9
+ platform: ruby
10
+ authors:
11
+ - Case Commons, LLC
12
+ autorequire:
13
+ bindir: bin
14
+ cert_chain: []
15
+
16
+ date: 2010-12-15 00:00:00 -05:00
17
+ default_executable:
18
+ dependencies:
19
+ - !ruby/object:Gem::Dependency
20
+ name: activerecord
21
+ prerelease: false
22
+ requirement: &id001 !ruby/object:Gem::Requirement
23
+ none: false
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 2
29
+ - 3
30
+ - 5
31
+ version: 2.3.5
32
+ - - <
33
+ - !ruby/object:Gem::Version
34
+ segments:
35
+ - 4
36
+ - 0
37
+ - 0
38
+ version: 4.0.0
39
+ type: :runtime
40
+ version_requirements: *id001
41
+ description: Dynamically build a model within an Rspec context
42
+ email:
43
+ - casecommons-dev@googlegroups.com
44
+ executables: []
45
+
46
+ extensions: []
47
+
48
+ extra_rdoc_files: []
49
+
50
+ files:
51
+ - .autotest
52
+ - .gitignore
53
+ - Gemfile
54
+ - LICENSE
55
+ - README
56
+ - Rakefile
57
+ - gemfiles/Gemfile.common
58
+ - gemfiles/rspec1/Gemfile
59
+ - gemfiles/rspec1/Gemfile.lock
60
+ - gemfiles/rspec2/Gemfile
61
+ - gemfiles/rspec2/Gemfile.lock
62
+ - lib/with_model.rb
63
+ - lib/with_model/dsl.rb
64
+ - lib/with_model/version.rb
65
+ - spec/active_record_behaviors_spec.rb
66
+ - spec/spec_helper.rb
67
+ - spec/with_model_spec.rb
68
+ - with_model.gemspec
69
+ has_rdoc: true
70
+ homepage: https://github.com/Casecommons/with_model
71
+ licenses: []
72
+
73
+ post_install_message:
74
+ rdoc_options: []
75
+
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ none: false
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ segments:
84
+ - 0
85
+ version: "0"
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ none: false
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ segments:
92
+ - 0
93
+ version: "0"
94
+ requirements: []
95
+
96
+ rubyforge_project:
97
+ rubygems_version: 1.3.7
98
+ signing_key:
99
+ specification_version: 3
100
+ summary: Dynamically build a model within an Rspec context
101
+ test_files:
102
+ - spec/active_record_behaviors_spec.rb
103
+ - spec/spec_helper.rb
104
+ - spec/with_model_spec.rb