Skip to content

Commit

Permalink
Merge b86142f into 6b79166
Browse files Browse the repository at this point in the history
  • Loading branch information
pmanrubia committed Jul 31, 2013
2 parents 6b79166 + b86142f commit 8714512
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 29 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ Gemfile.lock
pkg
.idea
.DS_Store
coverage
1 change: 1 addition & 0 deletions lib/postcode_anywhere/email_validation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require 'postcode_anywhere/email_validation/validation_error'
require 'postcode_anywhere/email_validation/validator'
require 'postcode_anywhere/email_validation/version'
require 'postcode_anywhere/matchers/email_validator' if defined?(RSpec)

require 'rest_client'

Expand Down
2 changes: 1 addition & 1 deletion lib/postcode_anywhere/email_validation/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module PostcodeAnywhere
module EmailValidation
VERSION = '0.0.3'
VERSION = '0.0.4'
end
end
30 changes: 30 additions & 0 deletions lib/postcode_anywhere/matchers/email_validator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module PostcodeAnywhere
module Matchers
RSpec::Matchers.define :validate_email_with_postcode_anywhere do
chain :on_attribute do |attribute|
@attribute = attribute
end

match do |model|
model_validators = model.class.validators_on(attribute)
model_validators.any? { |validator| validator.instance_of?(PostcodeAnywhere::EmailValidation::Validator) }
end

def attribute
@attribute || :email
end

failure_message_for_should do |model|
failure_message(model, 'should')
end

failure_message_for_should_not do |model|
failure_message(model, 'snould not')
end

def failure_message(model, should_or_should_not)
"#{model.class} #{should_or_should_not} have 'PostcodeAnywhere::EmailValidation::Validator' on attribute #{attribute}"
end
end
end
end
24 changes: 24 additions & 0 deletions spec/lib/postcode_anywhere/matchers/email_validator_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require 'spec_helper'

module PostcodeAnywhere::EmailValidation

describe 'Validator Matcher' do

context 'when Validator is defined' do
subject { ::ValidatedClass.new }

it { should validate_email_with_postcode_anywhere }

it { should validate_email_with_postcode_anywhere.on_attribute(:email) }

it { should_not validate_email_with_postcode_anywhere.on_attribute(:email_address) }
end

context 'when Validator is not defined' do
subject { ::NotValidatedClass.new }

it { should_not validate_email_with_postcode_anywhere }
end

end
end
28 changes: 0 additions & 28 deletions spec/support/matchers.rb

This file was deleted.

4 changes: 4 additions & 0 deletions spec/support/not_validated_class.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class NotValidatedClass
include ActiveModel::Validations
end

0 comments on commit 8714512

Please sign in to comment.