Skip to content

Commit

Permalink
ValueObject #clone and #dup return self
Browse files Browse the repository at this point in the history
  • Loading branch information
senny committed May 17, 2012
1 parent 559960f commit 0402b3e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
10 changes: 10 additions & 0 deletions lib/virtus/value_object.rb
Expand Up @@ -53,6 +53,16 @@ def initialize(attributes = {})
def with(attribute_updates)
self.class.new(get_attributes(&FILTER_NONE).merge(attribute_updates))
end

# ValueObjects are immutable and can't be cloned. They always represent the same value.
#
# @return [self]
#
# @api public
def clone
self
end
alias dup clone
end

module ClassMethods
Expand Down
2 changes: 1 addition & 1 deletion spec/integration/default_values_spec.rb
Expand Up @@ -53,7 +53,7 @@ def default_editor_title
it 'should not duplicate the ValueObject' do
page1 = Examples::Page.new
page2 = Examples::Page.new
page1.reference.object_id.should == page2.reference.object_id
page1.reference.should equal(page2.reference)
end
end

Expand Down
22 changes: 22 additions & 0 deletions spec/unit/virtus/value_object/instance_methods/duplicates_spec.rb
@@ -0,0 +1,22 @@
require 'spec_helper'

describe Virtus::ValueObject::InstanceMethods, 'duplication' do
let(:described_class) do
Class.new do
include Virtus::ValueObject

attribute :name, String
end
end

subject { described_class.new }

it '#clone returns the same instance' do
subject.should equal(subject.clone)
end

it '#dup returns the same instance' do
subject.should equal(subject.dup)
end

end

0 comments on commit 0402b3e

Please sign in to comment.