with_model 0.2.1 → 0.2.2

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.
data/CHANGELOG CHANGED
@@ -1,3 +1,7 @@
1
+ ### 0.2.2
2
+
3
+ The table block is now optional.
4
+
1
5
  ### 0.2.1
2
6
 
3
7
  Fix a bug when the with_model name contains capital letters. Now you can
data/lib/with_model.rb CHANGED
@@ -2,7 +2,7 @@ require "with_model/dsl"
2
2
 
3
3
  module WithModel
4
4
  def with_model(name, &block)
5
- Dsl.new(name, self).instance_eval(&block)
5
+ Dsl.new(name, self).tap { |dsl| dsl.instance_eval(&block) }.execute
6
6
  end
7
7
 
8
8
  def with_table(name, options = {}, &block)
@@ -3,43 +3,50 @@ require 'active_support/inflector'
3
3
 
4
4
  module WithModel
5
5
  class Dsl
6
- attr_reader :model_initialization
6
+ NOOP = lambda {|*|}
7
7
 
8
8
  def initialize(name, example_group)
9
- dsl = self
10
-
9
+ @name = name
11
10
  @example_group = example_group
12
- @table_name = table_name = "with_model_#{name.to_s.tableize}_#{$$}"
13
- @model_initialization = lambda {|*|}
11
+ @model_initialization = NOOP
12
+ @table_block = NOOP
13
+ @table_options = {}
14
+ end
15
+
16
+ def table(options = {}, &block)
17
+ @table_options = options
18
+ @table_block = block
19
+ end
14
20
 
15
- const_name = name.to_s.camelize.to_sym
21
+ def model(&block)
22
+ @model_initialization = block
23
+ end
24
+
25
+ def execute
26
+ model_initialization = @model_initialization
27
+ const_name = @name.to_s.camelize.to_sym
28
+ table_name = "with_model_#{@name.to_s.tableize}_#{$$}"
16
29
 
17
30
  original_const_defined = Object.const_defined?(const_name)
18
31
  original_const_value = Object.const_get(const_name) if original_const_defined
19
32
 
20
33
  model = Class.new(WithModel::Base)
21
34
 
22
- example_group.before do
35
+ @example_group.before do
23
36
  silence_warnings { Object.const_set(const_name, model) }
24
37
  Object.const_get(const_name).class_eval do
25
38
  set_table_name table_name
26
- self.class_eval(&dsl.model_initialization)
39
+ self.class_eval(&model_initialization)
27
40
  end
28
41
  end
29
42
 
30
- example_group.after do
31
- model._with_model_deconstructor if defined?(Mixico)
43
+ @example_group.after do
44
+ model._with_model_deconstructor if model.respond_to?(:_with_model_deconstructor)
32
45
  Object.send(:remove_const, const_name)
33
46
  Object.const_set(const_name, original_const_value) if original_const_defined
34
47
  end
35
- end
36
48
 
37
- def table(options = {}, &block)
38
- @example_group.with_table(@table_name, options, &block)
39
- end
40
-
41
- def model(&block)
42
- @model_initialization = block
49
+ @example_group.with_table(table_name, @table_options, &@table_block)
43
50
  end
44
51
  end
45
52
  end
@@ -1,3 +1,3 @@
1
1
  module WithModel
2
- VERSION = "0.2.1"
2
+ VERSION = "0.2.2"
3
3
  end
@@ -195,4 +195,24 @@ describe "a temporary ActiveRecord model created with with_model" do
195
195
 
196
196
  end
197
197
 
198
+ context "without a table block" do
199
+ with_model :blog_post do
200
+ end
201
+
202
+ it "should act like a normal ActiveRecord model" do
203
+ record = BlogPost.create!
204
+ record.reload
205
+ record.destroy
206
+ lambda {
207
+ record.reload
208
+ }.should raise_error(ActiveRecord::RecordNotFound)
209
+ end
210
+
211
+ if defined?(ActiveModel)
212
+ describe "the class" do
213
+ subject { BlogPost.new }
214
+ it_should_behave_like "ActiveModel"
215
+ end
216
+ end
217
+ end
198
218
  end
metadata CHANGED
@@ -1,38 +1,42 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: with_model
3
- version: !ruby/object:Gem::Version
4
- version: 0.2.1
3
+ version: !ruby/object:Gem::Version
5
4
  prerelease:
5
+ version: 0.2.2
6
6
  platform: ruby
7
- authors:
7
+ authors:
8
8
  - Case Commons, LLC
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-07-01 00:00:00.000000000 -04:00
12
+
13
+ date: 2011-07-11 00:00:00 -04:00
13
14
  default_executable:
14
- dependencies:
15
- - !ruby/object:Gem::Dependency
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
16
17
  name: activerecord
17
- requirement: &2164522740 !ruby/object:Gem::Requirement
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
18
20
  none: false
19
- requirements:
20
- - - ! '>='
21
- - !ruby/object:Gem::Version
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
22
24
  version: 2.3.5
23
25
  - - <
24
- - !ruby/object:Gem::Version
26
+ - !ruby/object:Gem::Version
25
27
  version: 4.0.0
26
28
  type: :runtime
27
- prerelease: false
28
- version_requirements: *2164522740
29
+ version_requirements: *id001
29
30
  description: Dynamically build a model within an Rspec context
30
- email:
31
+ email:
31
32
  - casecommons-dev@googlegroups.com
32
33
  executables: []
34
+
33
35
  extensions: []
36
+
34
37
  extra_rdoc_files: []
35
- files:
38
+
39
+ files:
36
40
  - .autotest
37
41
  - .gitignore
38
42
  - .rspec
@@ -56,29 +60,32 @@ files:
56
60
  has_rdoc: true
57
61
  homepage: https://github.com/Casecommons/with_model
58
62
  licenses: []
63
+
59
64
  post_install_message:
60
65
  rdoc_options: []
61
- require_paths:
66
+
67
+ require_paths:
62
68
  - lib
63
- required_ruby_version: !ruby/object:Gem::Requirement
69
+ required_ruby_version: !ruby/object:Gem::Requirement
64
70
  none: false
65
- requirements:
66
- - - ! '>='
67
- - !ruby/object:Gem::Version
68
- version: '0'
69
- required_rubygems_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: "0"
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
76
  none: false
71
- requirements:
72
- - - ! '>='
73
- - !ruby/object:Gem::Version
74
- version: '0'
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: "0"
75
81
  requirements: []
82
+
76
83
  rubyforge_project:
77
84
  rubygems_version: 1.6.2
78
85
  signing_key:
79
86
  specification_version: 3
80
87
  summary: Dynamically build a model within an Rspec context
81
- test_files:
88
+ test_files:
82
89
  - spec/active_record_behaviors_spec.rb
83
90
  - spec/readme_spec.rb
84
91
  - spec/spec_helper.rb