-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c9cfcb1
commit e8cebb7
Showing
3 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
require 'sham/config/base' | ||
require 'sham/util' | ||
|
||
module Sham | ||
class Config | ||
class Assign < Base | ||
def initialize(config) | ||
@config = config | ||
end | ||
|
||
def sham(build = false) | ||
object = super(true) | ||
|
||
@options.each do |key, value| | ||
object.public_send("#{key}=", value) | ||
end | ||
|
||
if !build && object.respond_to?(:save) | ||
object.save | ||
end | ||
|
||
object | ||
end | ||
|
||
def options(*args) | ||
@options = ::Sham::Util.extract_options!(args) | ||
|
||
@config.call.each do |key, value| | ||
@options[key] = parse!(value) unless @options.has_key?(key) | ||
end | ||
|
||
self | ||
end | ||
|
||
def args | ||
[] | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
require 'sham/config/assign' | ||
|
||
describe Sham::Config::Assign do | ||
before(:all) do | ||
Object.send(:remove_const, :AssignTester) if defined?(AssignTester) | ||
class AssignTester; end | ||
end | ||
|
||
let(:id){ stub } | ||
let(:options){ { :id => id } } | ||
let(:subject){ described_class.new(lambda{ options }) } | ||
let(:config){ subject.object(AssignTester) } | ||
let(:instance){ stub.as_null_object } | ||
|
||
before{ AssignTester.stub(:new){ instance } } | ||
|
||
it 'sets default options' do | ||
instance.should_receive(:public_send).with('id=', id) | ||
config.options.sham | ||
end | ||
|
||
it 'prioritizes passed options' do | ||
instance.should_receive(:id=).with(2) | ||
config.options(:id => 2).sham | ||
end | ||
|
||
it 'merges passed options' do | ||
instance.should_receive(:name=).with('A') | ||
config.options(:name => 'A').sham | ||
end | ||
|
||
it 'parses default options' do | ||
subject.should_receive(:parse!).with(id) | ||
config.options.sham | ||
end | ||
|
||
it 'does not parse passed options' do | ||
subject.should_receive(:parse!).with(2).never | ||
config.options(:id => 2).sham | ||
end | ||
|
||
it 'calls save if the instance responds to save' do | ||
instance.stub(:respond_to?).with(:save){ true } | ||
instance.should_receive(:save) | ||
config.options.sham | ||
end | ||
|
||
it 'does not call save on build' do | ||
instance.should_recieve(:save).never | ||
config.options.sham(true) | ||
end | ||
|
||
it 'does not call save if it does not respond to save' do | ||
instance.stub(:respond_to?).with(:save){ false } | ||
instance.should_receive(:save).never | ||
config.options.sham | ||
end | ||
end |