sham 1.0.3 → 1.1.0

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,3 @@
1
+ # 1.1.0
2
+
3
+ * Added `assign` sham configuration.
@@ -140,6 +140,27 @@ Alternative sham configurations can be invoked by passing their name into the
140
140
  Item.sham!(:small, :quantity => 100)
141
141
  Item.sham!(:large, :build, :quantity => 0)
142
142
 
143
+ ## Assign Shams
144
+
145
+ If you have configured mass-assignment protected attributes in Rails, or you
146
+ would prefer your object to go through regular instance setters rather than the
147
+ initializer, you can use the `assign` configuration.
148
+
149
+ # sham/user.rb
150
+ Sham.config(User) do |c|
151
+ c.assign do
152
+ { :name => 'John Doe' }
153
+ end
154
+ end
155
+
156
+ When executing `User.sham!` all attributes will be assigned using the instance
157
+ setters instead of the initializer. A `save` will also be called unless the
158
+ `:build` parameters is used.
159
+
160
+ User.any_instance.should_receive(:name=).with('Jane Doe')
161
+ User.any_instance.should_receive(:save)
162
+ User.sham!(:name => 'Jane Doe')
163
+
143
164
  ## Empty Shams
144
165
 
145
166
  Sometimes you simply want to be able to sham an object without passing any
@@ -1,4 +1,5 @@
1
1
  require 'sham/shammable'
2
+ require 'sham/config/assign'
2
3
  require 'sham/config/attributes'
3
4
  require 'sham/config/parameters'
4
5
  require 'sham/config/empty'
@@ -26,6 +27,10 @@ module Sham
26
27
  @name = name
27
28
  end
28
29
 
30
+ def assign(&config)
31
+ @klass.add_sham_config(@name, Sham::Config::Assign.new(config))
32
+ end
33
+
29
34
  def attributes(&config)
30
35
  @klass.add_sham_config(@name, Sham::Config::Attributes.new(config))
31
36
  end
@@ -0,0 +1,40 @@
1
+ require 'sham/config/base'
2
+ require 'sham/util'
3
+
4
+ module Sham
5
+ class Config
6
+ class Assign < Base
7
+ def initialize(config)
8
+ @config = config
9
+ end
10
+
11
+ def sham(build = false)
12
+ object = super(true)
13
+
14
+ @options.each do |key, value|
15
+ object.public_send("#{key}=", value)
16
+ end
17
+
18
+ if !build && object.respond_to?(:save)
19
+ object.save
20
+ end
21
+
22
+ object
23
+ end
24
+
25
+ def options(*args)
26
+ @options = ::Sham::Util.extract_options!(args)
27
+
28
+ @config.call.each do |key, value|
29
+ @options[key] = parse!(value) unless @options.has_key?(key)
30
+ end
31
+
32
+ self
33
+ end
34
+
35
+ def args
36
+ []
37
+ end
38
+ end
39
+ end
40
+ end
@@ -1,3 +1,3 @@
1
1
  module Sham
2
- VERSION = "1.0.3"
2
+ VERSION = "1.1.0"
3
3
  end
@@ -0,0 +1,58 @@
1
+ require 'sham/config/assign'
2
+
3
+ describe Sham::Config::Assign do
4
+ before(:all) do
5
+ Object.send(:remove_const, :AssignTester) if defined?(AssignTester)
6
+ class AssignTester; end
7
+ end
8
+
9
+ let(:id){ stub }
10
+ let(:options){ { :id => id } }
11
+ let(:subject){ described_class.new(lambda{ options }) }
12
+ let(:config){ subject.object(AssignTester) }
13
+ let(:instance){ stub.as_null_object }
14
+
15
+ before{ AssignTester.stub(:new){ instance } }
16
+
17
+ it 'sets default options' do
18
+ instance.should_receive(:public_send).with('id=', id)
19
+ config.options.sham
20
+ end
21
+
22
+ it 'prioritizes passed options' do
23
+ instance.should_receive(:public_send).with('id=', 2)
24
+ config.options(:id => 2).sham
25
+ end
26
+
27
+ it 'merges passed options' do
28
+ instance.should_receive(:public_send).with('name=', 'A')
29
+ config.options(:name => 'A').sham
30
+ end
31
+
32
+ it 'parses default options' do
33
+ subject.should_receive(:parse!).with(id)
34
+ config.options.sham
35
+ end
36
+
37
+ it 'does not parse passed options' do
38
+ subject.should_receive(:parse!).with(2).never
39
+ config.options(:id => 2).sham
40
+ end
41
+
42
+ it 'calls save if the instance responds to save' do
43
+ instance.stub(:respond_to?).with(:save){ true }
44
+ instance.should_receive(:save)
45
+ config.options.sham
46
+ end
47
+
48
+ it 'does not call save on build' do
49
+ instance.should_recieve(:save).never
50
+ config.options.sham(true)
51
+ end
52
+
53
+ it 'does not call save if it does not respond to save' do
54
+ instance.stub(:respond_to?).with(:save){ false }
55
+ instance.should_receive(:save).never
56
+ config.options.sham
57
+ end
58
+ end
@@ -151,6 +151,17 @@ describe Sham::Config do
151
151
  end
152
152
  end
153
153
 
154
+ it 'configures assign shams' do
155
+ Sham.config(parent) do |c|
156
+ c.assign{ { :first => 'first' } }
157
+ end
158
+
159
+ instance = stub
160
+ parent.stub(:new){ instance }
161
+ instance.should_receive(:public_send).with('first=', 'first')
162
+ parent.sham!.should == instance
163
+ end
164
+
154
165
  it 'configures attribute shams' do
155
166
  attributes = { :first => 'first', :last => 'last' }
156
167
  Sham.config(parent) do |c|
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sham
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 1.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-03-03 00:00:00.000000000 Z
12
+ date: 2012-06-13 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &70125021740440 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,7 +21,12 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70125021740440
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
25
30
  description: Lightweight flexible factories for Ruby and Rails testing.
26
31
  email:
27
32
  - pan.thomakos@gmail.com
@@ -34,6 +39,7 @@ files:
34
39
  - .document
35
40
  - .gitignore
36
41
  - .travis.yml
42
+ - CHANGES.markdown
37
43
  - Gemfile
38
44
  - License.txt
39
45
  - README.markdown
@@ -41,6 +47,7 @@ files:
41
47
  - lib/sham.rb
42
48
  - lib/sham/base.rb
43
49
  - lib/sham/config.rb
50
+ - lib/sham/config/assign.rb
44
51
  - lib/sham/config/attributes.rb
45
52
  - lib/sham/config/base.rb
46
53
  - lib/sham/config/empty.rb
@@ -53,6 +60,7 @@ files:
53
60
  - lib/sham/version.rb
54
61
  - sham.gemspec
55
62
  - spec/lib/sham/base_spec.rb
63
+ - spec/lib/sham/config/assign_spec.rb
56
64
  - spec/lib/sham/config/attributes_spec.rb
57
65
  - spec/lib/sham/config/empty_spec.rb
58
66
  - spec/lib/sham/config/no_args_spec.rb
@@ -83,12 +91,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
83
91
  version: '0'
84
92
  requirements: []
85
93
  rubyforge_project: sham
86
- rubygems_version: 1.8.16
94
+ rubygems_version: 1.8.23
87
95
  signing_key:
88
96
  specification_version: 3
89
- summary: sham-1.0.3
97
+ summary: sham-1.1.0
90
98
  test_files:
91
99
  - spec/lib/sham/base_spec.rb
100
+ - spec/lib/sham/config/assign_spec.rb
92
101
  - spec/lib/sham/config/attributes_spec.rb
93
102
  - spec/lib/sham/config/empty_spec.rb
94
103
  - spec/lib/sham/config/no_args_spec.rb
@@ -98,3 +107,4 @@ test_files:
98
107
  - spec/lib/sham/nested_spec.rb
99
108
  - spec/lib/sham/util_spec.rb
100
109
  - spec/root/sham/employee.rb
110
+ has_rdoc: