Skip to content

Commit

Permalink
Added DataObjects::Schema::Transformable#transform_unless
Browse files Browse the repository at this point in the history
  • Loading branch information
snusnu committed Aug 29, 2010
1 parent 36ff489 commit 22368f0
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 18 deletions.
6 changes: 5 additions & 1 deletion do-schema.gemspec
Expand Up @@ -120,7 +120,9 @@ Gem::Specification.new do |s|
"spec/unit/data_objects/schema/tables/initialize_spec.rb",
"spec/unit/data_objects/schema/tables/length_spec.rb",
"spec/unit/data_objects/schema/tables/merge_spec.rb",
"spec/unit/data_objects/schema/transformable/fixtures/dummy.rb",
"spec/unit/data_objects/schema/transformable/transform_spec.rb",
"spec/unit/data_objects/schema/transformable/transform_unless_spec.rb",
"tasks/ci.rake",
"tasks/clean.rake",
"tasks/quality/flay.rake",
Expand Down Expand Up @@ -213,7 +215,9 @@ Gem::Specification.new do |s|
"spec/unit/data_objects/schema/tables/initialize_spec.rb",
"spec/unit/data_objects/schema/tables/length_spec.rb",
"spec/unit/data_objects/schema/tables/merge_spec.rb",
"spec/unit/data_objects/schema/transformable/transform_spec.rb"
"spec/unit/data_objects/schema/transformable/fixtures/dummy.rb",
"spec/unit/data_objects/schema/transformable/transform_spec.rb",
"spec/unit/data_objects/schema/transformable/transform_unless_spec.rb"
]

if s.respond_to? :specification_version then
Expand Down
4 changes: 4 additions & 0 deletions lib/do-schema/support/transformable.rb
Expand Up @@ -11,6 +11,10 @@ def transform(&block)
copy.freeze
end

def transform_unless(condition, &block)
condition ? self : transform(&block)
end

end # module Transformable

end # module Schema
Expand Down
23 changes: 23 additions & 0 deletions spec/unit/data_objects/schema/transformable/fixtures/dummy.rb
@@ -0,0 +1,23 @@
require 'do-schema/support/transformable'

class Immutable

include DataObjects::Schema::Transformable

attr_reader :state, :delta

def initialize(state)
@state = state
@delta = nil
end

def modify(delta)
transform { @delta = delta }
end

def modify_unless(condition, delta)
transform_unless(condition) { @delta = delta }
end

end

18 changes: 1 addition & 17 deletions spec/unit/data_objects/schema/transformable/transform_spec.rb
@@ -1,22 +1,6 @@
require 'spec_helper'
require 'do-schema/support/transformable'

class Immutable

include DataObjects::Schema::Transformable

attr_reader :state, :delta

def initialize(state)
@state = state
@delta = nil
end

def modify(delta)
transform { @delta = delta }
end

end
require 'spec/unit/data_objects/schema/transformable/fixtures/dummy'

describe 'DataObjects::Schema::Transformable#transform' do

Expand Down
@@ -0,0 +1,38 @@
require 'spec_helper'
require 'do-schema/support/transformable'
require 'spec/unit/data_objects/schema/transformable/fixtures/dummy'

describe 'DataObjects::Schema::Transformable#transform_unless' do

let(:instance) { Immutable.new(state) }
let(:state) { 'foo' }
let(:delta) { 'baz' }

context 'when the condition evaluates to true' do

subject { instance.modify_unless(true, delta) }

it { should equal(instance) }
it { should_not be_frozen }

its(:state) { should == state }
its(:delta) { should == nil }

end

context 'when the condition evaluates to false' do

subject { instance.modify_unless(false, delta) }

it { should_not equal(instance) }
it { should be_instance_of(Immutable) }
it { should be_frozen }

its(:state) { should == state }
its(:delta) { should == delta }

end

end


0 comments on commit 22368f0

Please sign in to comment.