Skip to content

Commit

Permalink
Does not try to set attributes on initialization if provided argument…
Browse files Browse the repository at this point in the history
… is nil
  • Loading branch information
fgrehm committed Mar 11, 2012
1 parent 680a59e commit 7ab279a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/virtus/instance_methods.rb
Expand Up @@ -12,7 +12,7 @@ module InstanceMethods
#
# @api private
def initialize(attribute_values = {})
self.attributes = attribute_values
self.attributes = attribute_values if attribute_values
end

# Returns a value of the attribute with the given name
Expand Down
32 changes: 32 additions & 0 deletions spec/unit/virtus/instance_methods/initialize_spec.rb
@@ -0,0 +1,32 @@
require 'spec_helper'

describe Virtus::InstanceMethods, '#initialize' do
let(:described_class) do
Class.new do
include Virtus
attribute :name, String
end
end

let(:subject) do
described_class.new(attributes)
end

context "with valid argument" do
let(:attributes) do
{:name => 'john'}
end

it 'sets attributes' do
subject.name.should == attributes[:name]
end
end

context "with nil argument" do
let(:attributes) { nil }

it 'does not try to set attributes' do
expect { subject }.to_not raise_error(NoMethodError)
end
end
end

0 comments on commit 7ab279a

Please sign in to comment.