activerecord-tableless 1.0.2 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color --backtrace --format doc
data/README.md CHANGED
@@ -59,6 +59,11 @@ For Rails 2.3.x series you need to add this line in the top of your model file.
59
59
 
60
60
  require 'activerecord-tableless'
61
61
 
62
+ If you wish (this is not recommended), you can pretend you have a succeeding database by using
63
+
64
+ has_no_table :database => :pretend_succes
65
+
66
+
62
67
 
63
68
  Development
64
69
  -----------
data/Rakefile CHANGED
@@ -1,6 +1,7 @@
1
1
  require 'bundler/gem_tasks'
2
2
  require 'appraisal'
3
3
  require 'rake/testtask'
4
+ require 'rspec/core/rake_task'
4
5
  require 'cucumber/rake/task'
5
6
 
6
7
  desc 'Default: clean, all.'
@@ -9,10 +10,10 @@ task :default => [:clean, :all]
9
10
  desc 'Test the activerecord-tableless on all supported Rails versions.'
10
11
  task :all do |t|
11
12
  if ENV['BUNDLE_GEMFILE']
12
- exec('rake test cucumber')
13
+ exec('rake test spec cucumber')
13
14
  else
14
15
  Rake::Task["appraisal:install"].execute
15
- exec('rake appraisal test cucumber')
16
+ exec('rake appraisal test spec cucumber')
16
17
  end
17
18
  end
18
19
 
@@ -23,6 +24,12 @@ Rake::TestTask.new(:test) do |t|
23
24
  t.verbose = true
24
25
  end
25
26
 
27
+ desc "Run specs"
28
+ RSpec::Core::RakeTask.new do |t|
29
+ t.pattern = "./spec/**/*_spec.rb" # don't need this, it's default.
30
+ # Put spec opts in a file named .rspec in root
31
+ end
32
+
26
33
  desc 'Run integration test'
27
34
  Cucumber::Rake::Task.new do |t|
28
35
  t.cucumber_opts = %w{--format progress}
@@ -10,7 +10,7 @@ Gem::Specification.new do |gem|
10
10
  gem.files = `git ls-files`.split($\)
11
11
  gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
12
12
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
13
- gem.version = "1.0.2"
13
+ gem.version = "1.1.0"
14
14
  gem.has_rdoc = true
15
15
 
16
16
  gem.require_paths = ["lib"]
@@ -26,6 +26,7 @@ Gem::Specification.new do |gem|
26
26
 
27
27
  gem.add_development_dependency('appraisal', '~> 0.4')
28
28
  gem.add_development_dependency('cucumber', '~> 1.1')
29
+ gem.add_development_dependency("rspec")
29
30
  gem.add_development_dependency('launchy', '~> 2.1')
30
31
  gem.add_development_dependency('aruba', '>= 0.5')
31
32
  gem.add_development_dependency('capybara')
@@ -29,6 +29,11 @@ module ActiveRecord
29
29
  #
30
30
  module Tableless
31
31
 
32
+ class Exception < StandardError
33
+ end
34
+ class NoDatabase < Exception
35
+ end
36
+
32
37
  def self.included( base ) #:nodoc:
33
38
  base.send :extend, ActsMethods
34
39
  end
@@ -37,17 +42,22 @@ module ActiveRecord
37
42
 
38
43
  # A model that needs to be tableless will call this method to indicate
39
44
  # it.
40
- def has_no_table
45
+ def has_no_table(options = {:database => :fail_fast})
46
+ raise ArgumentError.new("Invalid database option '#{options[:database]}'") unless [:fail_fast, :pretend_succes].member? options[:database]
41
47
  # keep our options handy
42
- if Rails::VERSION::STRING < "3.1.0"
43
- write_inheritable_attribute(
44
- :tableless_options,
45
- :columns => []
48
+ if ActiveRecord::VERSION::STRING < "3.1.0"
49
+ write_inheritable_attribute(:tableless_options,
50
+ { :database => options[:database],
51
+ :columns => []
52
+ }
46
53
  )
47
54
  class_inheritable_reader :tableless_options
48
55
  else
49
56
  class_attribute :tableless_options
50
- self.tableless_options = {:columns => []}
57
+ self.tableless_options = {
58
+ :database => options[:database],
59
+ :columns => []
60
+ }
51
61
  end
52
62
 
53
63
  # extend
@@ -85,15 +95,59 @@ module ActiveRecord
85
95
  end
86
96
  end
87
97
 
88
- %w(find create destroy).each do |m|
98
+ %w(destroy destroy_all).each do |m|
89
99
  eval %{
90
100
  def #{m}(*args)
91
- logger.warn "Can't #{m} a Tableless object"
92
- false
101
+ case tableless_options[:database]
102
+ when :pretend_succes
103
+ self
104
+ when :fail_fast
105
+ raise NoDatabase.new("Can't ##{m} on Tableless class")
106
+ end
93
107
  end
94
108
  }
95
109
  end
96
110
 
111
+ if ActiveRecord::VERSION::STRING < "3.0"
112
+ def find_with_ids(*args)
113
+ case tableless_options[:database]
114
+ when :pretend_succes
115
+ raise ActiveRecord::RecordNotFound.new("Couldn't find #{self} with ID=#{args[0].to_s}")
116
+
117
+ when :fail_fast
118
+ raise NoDatabase.new("Can't #find_from_ids on Tableless class")
119
+ end
120
+ end
121
+
122
+ def find_every(*args)
123
+ case tableless_options[:database]
124
+ when :pretend_succes
125
+ []
126
+ when :fail_fast
127
+ raise NoDatabase.new("Can't #find_every on Tableless class")
128
+ end
129
+ end
130
+ else ## ActiveRecord::VERSION::STRING >= "3.0"
131
+ def all(*args)
132
+ case tableless_options[:database]
133
+ when :pretend_succes
134
+ []
135
+ when :fail_fast
136
+ raise NoDatabase.new("Can't #find_every on Tableless class")
137
+ end
138
+
139
+ end
140
+ end
141
+
142
+ def transaction(&block)
143
+ case tableless_options[:database]
144
+ when :pretend_succes
145
+ yield
146
+ when :fail_fast
147
+ raise NoDatabase.new("Can't #transaction on Tableless class")
148
+ end
149
+ end
150
+
97
151
  def tableless?
98
152
  true
99
153
  end
@@ -125,13 +179,41 @@ module ActiveRecord
125
179
  attributes.to_a.collect{|(name,value)| escaped_var_name(name, prefix) + "=" + escape_for_url(value) if value }.compact.join("&")
126
180
  end
127
181
 
128
- %w(save destroy).each do |m|
129
- eval %{
130
- def #{m}(*args)
131
- logger.warn "Can't #{m} a Tableless object"
132
- false
133
- end
134
- }
182
+ def create(*args)
183
+ case self.class.tableless_options[:database]
184
+ when :pretend_succes
185
+ true
186
+ when :fail_fast
187
+ raise NoDatabase.new("Can't #create a Tableless object")
188
+ end
189
+ end
190
+
191
+ def update(*args)
192
+ case self.class.tableless_options[:database]
193
+ when :pretend_succes
194
+ true
195
+ when :fail_fast
196
+ raise NoDatabase.new("Can't #update a Tableless object")
197
+ end
198
+ end
199
+
200
+ def destroy
201
+ case self.class.tableless_options[:database]
202
+ when :pretend_succes
203
+ @destroyed = true
204
+ freeze
205
+ when :fail_fast
206
+ raise NoDatabase.new("Can't #destroy a Tableless object")
207
+ end
208
+ end
209
+
210
+ def reload(*args)
211
+ case self.class.tableless_options[:database]
212
+ when :pretend_succes
213
+ self
214
+ when :fail_fast
215
+ raise NoDatabase.new("Can't #reload a Tableless object")
216
+ end
135
217
  end
136
218
 
137
219
  private
@@ -0,0 +1,155 @@
1
+ require 'sqlite3'
2
+ require 'active_record'
3
+ require 'activerecord-tableless'
4
+ require 'logger'
5
+
6
+ ActiveRecord::Base.logger = Logger.new(STDERR)
7
+ ActiveRecord::Base.logger.level = Logger::Severity::UNKNOWN
8
+
9
+ class ChairFailure < ActiveRecord::Base
10
+ has_no_table
11
+ column :id, :integer
12
+ column :name, :string
13
+ end
14
+
15
+ class ChairPretend < ActiveRecord::Base
16
+ has_no_table :database => :pretend_succes
17
+ column :id, :integer
18
+ column :name, :string
19
+ end
20
+
21
+ FileUtils.mkdir_p "tmp"
22
+ ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => 'tmp/test.db')
23
+ ActiveRecord::Base.connection.execute("drop table if exists chairs")
24
+ ActiveRecord::Base.connection.execute("create table chairs (id INTEGER PRIMARY KEY, name TEXT )")
25
+
26
+ class Chair < ActiveRecord::Base
27
+ end
28
+
29
+ describe "tableless attributes" do
30
+
31
+ subject { ChairFailure.new }
32
+ it { should respond_to :id }
33
+ it { should respond_to :id= }
34
+ it { should respond_to :name }
35
+ it { should respond_to :name= }
36
+
37
+ end
38
+
39
+ describe "tableless with fail_fast" do
40
+ let!(:klass) { ChairFailure }
41
+ subject { ChairFailure.new }
42
+
43
+ describe "class" do
44
+ if ActiveRecord::VERSION::STRING < "3.0"
45
+ describe "#find" do
46
+ it "raises ActiveRecord::Tableless::NoDatabase" do
47
+ expect { klass.find(1) }.to raise_exception(ActiveRecord::Tableless::NoDatabase)
48
+ end
49
+ end
50
+ describe "#find(:all)" do
51
+ it "raises ActiveRecord::Tableless::NoDatabase" do
52
+ expect { klass.find(:all) }.to raise_exception(ActiveRecord::Tableless::NoDatabase)
53
+ end
54
+ end
55
+ else ## ActiveRecord::VERSION::STRING >= "3.0"
56
+ describe "#all" do
57
+ it "raises ActiveRecord::Tableless::NoDatabase" do
58
+ expect { klass.all }.to raise_exception(ActiveRecord::Tableless::NoDatabase)
59
+ end
60
+ end
61
+ end
62
+ describe "#create" do
63
+ it "raises ActiveRecord::Tableless::NoDatabase" do
64
+ expect { klass.create(:name => 'Jarl') }.to raise_exception(ActiveRecord::Tableless::NoDatabase)
65
+ end
66
+ end
67
+ describe "#destroy" do
68
+ it "raises ActiveRecord::Tableless::NoDatabase" do
69
+ expect { klass.destroy(1) }.to raise_exception(ActiveRecord::Tableless::NoDatabase)
70
+ end
71
+ end
72
+ describe "#destroy_all" do
73
+ it "raises ActiveRecord::Tableless::NoDatabase" do
74
+ expect { klass.destroy_all }.to raise_exception(ActiveRecord::Tableless::NoDatabase)
75
+ end
76
+ end
77
+ end
78
+
79
+ describe "#save" do
80
+ it "raises ActiveRecord::Tableless::NoDatabase" do
81
+ expect { subject.save }.to raise_exception(ActiveRecord::Tableless::NoDatabase)
82
+ end
83
+ end
84
+ describe "#save!" do
85
+ it "raises ActiveRecord::Tableless::NoDatabase" do
86
+ expect { subject.save! }.to raise_exception(ActiveRecord::Tableless::NoDatabase)
87
+ end
88
+ end
89
+ describe "#reload" do
90
+ it "raises ActiveRecord::Tableless::NoDatabase" do
91
+ expect { subject.reload }.to raise_exception(ActiveRecord::Tableless::NoDatabase)
92
+ end
93
+ end
94
+ describe "#update_attributes" do
95
+ it "raises ActiveRecord::Tableless::NoDatabase" do
96
+ expect { subject.update_attributes(:name => 'Jarl') }.to raise_exception(StandardError)
97
+ end
98
+ end
99
+ end
100
+
101
+ shared_examples_for "a succeeding database" do
102
+
103
+ describe "class" do
104
+ if ActiveRecord::VERSION::STRING < "3.0"
105
+ describe "#find" do
106
+ it "raises ActiveRecord::RecordNotFound" do
107
+ expect { klass.find(314) }.to raise_exception(ActiveRecord::RecordNotFound)
108
+ end
109
+ end
110
+ describe "#find(:all)" do
111
+ specify { klass.find(:all) == []}
112
+ end
113
+ else ## ActiveRecord::VERSION::STRING >= "3.0"
114
+ describe "#all" do
115
+ specify { klass.all == []}
116
+ end
117
+ end
118
+ describe "#create" do
119
+ specify { klass.create(:name => 'Jarl') == true }
120
+ end
121
+ describe "#destroy" do
122
+ specify { klass.destroy(1) == true }
123
+ end
124
+ describe "#destroy_all" do
125
+ specify { klass.destroy_all == true }
126
+ end
127
+ end
128
+
129
+ describe "#save" do
130
+ specify { subject.save.should == true }
131
+ end
132
+ describe "#save!" do
133
+ specify { subject.save!.should == true }
134
+ end
135
+ describe "#reload" do
136
+ before { subject.save! }
137
+ specify { subject.reload.should == subject }
138
+ end
139
+ describe "#update_attributes" do
140
+ specify { subject.update_attributes(:name => 'Jarl Friis').should == true }
141
+ end
142
+ end
143
+
144
+ describe "tableless with real database" do
145
+ ##This is only here to ensure that the shared examples are actually behaving like a real database.
146
+ let!(:klass) { Chair }
147
+ subject { Chair.new(:name => 'Jarl') }
148
+ it_behaves_like "a succeeding database"
149
+ end
150
+
151
+ describe "tableless with succeeding database" do
152
+ let!(:klass) { ChairPretend }
153
+ subject { ChairPretend.new(:name => 'Jarl') }
154
+ it_behaves_like "a succeeding database"
155
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-tableless
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2013-01-29 00:00:00.000000000 Z
14
+ date: 2013-02-20 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: activerecord
@@ -109,6 +109,22 @@ dependencies:
109
109
  - - ~>
110
110
  - !ruby/object:Gem::Version
111
111
  version: '1.1'
112
+ - !ruby/object:Gem::Dependency
113
+ name: rspec
114
+ requirement: !ruby/object:Gem::Requirement
115
+ none: false
116
+ requirements:
117
+ - - ! '>='
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ type: :development
121
+ prerelease: false
122
+ version_requirements: !ruby/object:Gem::Requirement
123
+ none: false
124
+ requirements:
125
+ - - ! '>='
126
+ - !ruby/object:Gem::Version
127
+ version: '0'
112
128
  - !ruby/object:Gem::Dependency
113
129
  name: launchy
114
130
  requirement: !ruby/object:Gem::Requirement
@@ -167,6 +183,7 @@ extensions: []
167
183
  extra_rdoc_files: []
168
184
  files:
169
185
  - .gitignore
186
+ - .rspec
170
187
  - Appraisals
171
188
  - Gemfile
172
189
  - README.md
@@ -186,6 +203,7 @@ files:
186
203
  - gemfiles/rails3x.gemfile
187
204
  - init.rb
188
205
  - lib/activerecord-tableless.rb
206
+ - spec/lib/activerecord-tableless_spec.rb
189
207
  homepage: https://github.com/softace/activerecord-tableless
190
208
  licenses: []
191
209
  post_install_message:
@@ -200,7 +218,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
200
218
  version: '0'
201
219
  segments:
202
220
  - 0
203
- hash: -1781231480361877678
221
+ hash: 3225799463076378481
204
222
  required_rubygems_version: !ruby/object:Gem::Requirement
205
223
  none: false
206
224
  requirements:
@@ -209,7 +227,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
209
227
  version: '0'
210
228
  segments:
211
229
  - 0
212
- hash: -1781231480361877678
230
+ hash: 3225799463076378481
213
231
  requirements: []
214
232
  rubyforge_project:
215
233
  rubygems_version: 1.8.24
@@ -225,3 +243,4 @@ test_files:
225
243
  - features/support/paths.rb
226
244
  - features/support/rails.rb
227
245
  - features/support/selectors.rb
246
+ - spec/lib/activerecord-tableless_spec.rb