Skip to content

Commit

Permalink
Merge ralph's changes to allow assigning nil to a mapping.
Browse files Browse the repository at this point in the history
  • Loading branch information
therealadam committed Feb 27, 2010
1 parent 6f63e10 commit d990c8e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
14 changes: 6 additions & 8 deletions lib/attribute_mapper.rb
Expand Up @@ -114,14 +114,12 @@ def verify_existence_of(attribute)
module InstanceMethods

private

def resolve_value_of(attribute, raw_value)
check_value = raw_value.is_a?(String) ? raw_value.to_sym : raw_value
mapping = self.class.send(attribute.to_s.pluralize)
raise ArgumentError, "`#{check_value}' not present in attribute mapping `#{mapping.inspect}'" unless mapping.to_a.flatten.include? check_value
mapping[check_value] || check_value
end

def resolve_value_of(attribute, raw_value)
check_value = raw_value.is_a?(String) ? raw_value.to_sym : raw_value
mapping = self.class.send(attribute.to_s.pluralize)
raise ArgumentError, "`#{check_value}' not present in attribute mapping `#{mapping.inspect}'" unless mapping.to_a.flatten.include? check_value
mapping.include?(check_value) ? mapping[check_value] : check_value
end
end

end
22 changes: 22 additions & 0 deletions test/attribute_mapper_test.rb
Expand Up @@ -96,6 +96,28 @@ class AttributeMapperTest < Test::Unit::TestCase
assert_equal [[["Open", :open], ["Closed", :closed]], {:selected => :open}], ticket.status_options(false)
end

context "setting nil as a valid value in the mapping" do
setup do
Ticket.map_attribute :status, :to => mapping(:unanswered => nil)
end

should "allow setting the status by primitive value" do
assert_nothing_raised do
ticket.status = mapping[:unanswered]
end
assert_equal :unanswered, ticket.status
assert ticket.unanswered?
end

should "allow setting the status by symbol" do
assert_nothing_raised do
ticket.status = :unanswered
end
assert_equal :unanswered, ticket.status
assert ticket.unanswered?
end
end

end

#######
Expand Down

0 comments on commit d990c8e

Please sign in to comment.