Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
smtlaissezfaire committed Jan 13, 2010
1 parent 84d8b46 commit b7c9606
Showing 1 changed file with 16 additions and 15 deletions.
31 changes: 16 additions & 15 deletions lib/spec/rails/matchers/validations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,7 @@ def validate_inclusion_of(attribute, hash)
values = hash[:in]

simple_matcher("model to validate the inclusion of #{attribute} in #{values.inspect}") do |model|
booleans = values.map do |value|
model.send("#{attribute}=", value)
model.valid?
!model.errors.invalid?(attribute)
end

booleans = assign_and_validate_values(model, attribute, values)
booleans.all?
end
end
Expand All @@ -74,17 +69,23 @@ def validate_boolean_of(attribute)
values = [true, false]

simple_matcher("model to validate boolean of #{attribute}") do |model|
booleans = values.map do |value|
model.send("#{attribute}=", value)
model.valid?
!model.errors.invalid?(attribute)
end
booleans = assign_and_validate_values(model, attribute, [true, false])
booleans << !assign_and_validate_value(model, attribute, nil)
booleans.all?
end
end

model.send("#{attribute}=", nil)
model.valid?
booleans << model.errors.invalid?(attribute)
private

booleans.all?
def assign_and_validate_value(model, attribute, value)
model.send("#{attribute}=", value)
model.valid?
!model.errors.invalid?(attribute)
end

def assign_and_validate_values(model, attribute, values)
values.map do |value|
assign_and_validate_value(model, attribute, value)
end
end
end
Expand Down

0 comments on commit b7c9606

Please sign in to comment.