Skip to content
Browse files

re-introduce `ValueObject#with` which was removed with 6ea32ed.

This method is part of the public interface of a ValueObject.
It was removed due to lacking tests. This commit adds both the method
and tests.
  • Loading branch information...
1 parent 471454e commit 88677b6133bbfbcecee908999f5299250ac2dc5b @senny senny committed with elskwid Oct 18, 2013
Showing with 26 additions and 0 deletions.
  1. +4 −0 Changelog.md
  2. +16 −0 lib/virtus/value_object.rb
  3. +6 −0 spec/unit/virtus/value_object_spec.rb
View
4 Changelog.md
@@ -1,3 +1,7 @@
+# Unreleased
+
+* [feature] re-introduce `ValueObject#with`, which was removed in the past (senny)
+
# v1.0.0 2013-10-16
This release no longer works with Ruby 1.8.7.
View
16 lib/virtus/value_object.rb
@@ -62,6 +62,22 @@ def clone
end
alias dup clone
+ # Create a new ValueObject by combining the passed attribute hash with
+ # the instances attributes.
+ #
+ # @example
+ #
+ # number = PhoneNumber.new(kind: "mobile", number: "123-456-78-90")
+ # number.with(number: "987-654-32-10")
+ # # => #<PhoneNumber kind="mobile" number="987-654-32-10">
+ #
+ # @return [Object]
+ #
+ # @api public
+ def with(attribute_updates)
+ self.class.new(attribute_set.get(self).merge(attribute_updates))
+ end
+
end
module AllowedWriterMethods
View
6 spec/unit/virtus/value_object_spec.rb
@@ -35,6 +35,12 @@
%(#<Model #{attributes.map { |k, v| "#{k}=#{v.inspect}" }.join(' ')}>)
)
end
+
+ it 'allows to construct new values using #with' do
+ new_instance = subject.with(:name => "John Doe")
+ expect(new_instance.id).to eql(subject.id)
+ expect(new_instance.name).to eql("John Doe")
+ end
end
share_examples_for 'a valid value object with mass-assignment turned on' do

0 comments on commit 88677b6

Please sign in to comment.
Something went wrong with that request. Please try again.