Skip to content

Commit

Permalink
Cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabe Berke-Williams committed Mar 23, 2012
1 parent cab62a1 commit ba78ed0
Showing 1 changed file with 49 additions and 38 deletions.
87 changes: 49 additions & 38 deletions lib/shoulda/matchers/active_model/allow_value_matcher.rb
Expand Up @@ -17,8 +17,11 @@ module ActiveModel # :nodoc:
# it { should allow_value("isbn 1 2345 6789 0").for(:isbn) }
#
def allow_value(*values)
raise ArgumentError.new("need at least one argument") if values.empty?
AllowValueMatcher.new(*values)
if values.empty?
raise ArgumentError, "need at least one argument"
else
AllowValueMatcher.new(*values)
end
end

class AllowValueMatcher # :nodoc:
Expand All @@ -34,21 +37,17 @@ def for(attribute)
end

def with_message(message)
@expected_message = message if message
@expected_message = message
self
end

def matches?(instance)
@instance = instance
if Symbol === @expected_message
@expected_message = default_error_message(@expected_message, :model_name => @instance.class.to_s.underscore, :attribute => @attribute)
end
@values_to_match.each do |value|
@values_to_match.all? do |value|
@value = value
@instance.send("#{@attribute}=", @value)
return false if errors_match?
! errors_match?
end
true
end

def failure_message
Expand All @@ -60,56 +59,47 @@ def negative_failure_message
end

def description
"allow #{@attribute} to be set to " <<
if @values_to_match.length > 1
"any of [#{@values_to_match.map {|v| v.inspect }.join(', ')}]"
else
@values_to_match.first.inspect
end
"allow #{@attribute} to be set to #{allowed_values}"
end

private

def errors_match?
if ! @instance.valid?
@errors = errors_for_attribute(@instance, @attribute)
@errors = [@errors] unless @errors.is_a?(Array)
@expected_message ? (errors_match_regexp? || errors_match_string?) : (@errors.compact.any?)
else
@errors = []
if @instance.valid?
false
else
if expected_message
errors_match_regexp? || errors_match_string?
else
errors_for_attribute.compact.any?
end
end
end

def errors_for_attribute(instance, attribute)
if instance.errors.respond_to?(:[])
instance.errors[attribute]
def errors_for_attribute
if @instance.errors.respond_to?(:[])
errors = @instance.errors[@attribute]
else
instance.errors.on(attribute)
errors = @instance.errors.on(@attribute)
end
Array.wrap(errors)
end

def errors_match_regexp?
if Regexp === @expected_message
@matched_error = @errors.detect { |e| e =~ @expected_message }
else
false
if Regexp === expected_message
@matched_error = errors_for_attribute.detect { |e| e =~ expected_message }
end
end

def errors_match_string?
if @errors.include?(@expected_message)
@matched_error = @expected_message
true
else
false
if errors_for_attribute.include?(expected_message)
@matched_error = expected_message
end
end

def expectation
"errors " <<
(@expected_message ? "to include #{@expected_message.inspect} " : "") <<
"when #{@attribute} is set to #{@value.inspect}"
includes_expected_message = expected_message ? "to include #{expected_message.inspect}" : ''
["errors", includes_expected_message, "when #{@attribute} is set to #{@value.inspect}"].join(' ')
end

def error_description
Expand All @@ -119,8 +109,29 @@ def error_description
"errors: #{pretty_error_messages(@instance)}"
end
end
end

def allowed_values
if @values_to_match.length > 1
"any of [#{@values_to_match.map(&:inspect).join(', ')}]"
else
@values_to_match.first.inspect
end
end

def expected_message
if @expected_message
if Symbol === @expected_message
default_error_message(@expected_message, :model_name => model_name, :attribute => @attribute)
else
@expected_message
end
end
end

def model_name
@instance.class.to_s.underscore
end
end
end
end
end

0 comments on commit ba78ed0

Please sign in to comment.