From e492fad15f1268c0e572df2315700bc0c937520b Mon Sep 17 00:00:00 2001 From: Pablo Manrubia Date: Mon, 29 Jul 2013 16:51:08 +0100 Subject: [PATCH 1/4] Prepare matcher to be included from other apps @andrewgarner --- lib/postcode_anywhere/email_validation.rb | 1 + .../matchers/email_validator.rb | 30 +++++++++++++++++++ .../matchers/email_validator_spec.rb | 24 +++++++++++++++ spec/support/matchers.rb | 28 ----------------- spec/support/not_validated_class.rb | 4 +++ 5 files changed, 59 insertions(+), 28 deletions(-) create mode 100644 lib/postcode_anywhere/matchers/email_validator.rb create mode 100644 spec/lib/postcode_anywhere/matchers/email_validator_spec.rb delete mode 100644 spec/support/matchers.rb create mode 100644 spec/support/not_validated_class.rb diff --git a/lib/postcode_anywhere/email_validation.rb b/lib/postcode_anywhere/email_validation.rb index 674786c..ebb451f 100644 --- a/lib/postcode_anywhere/email_validation.rb +++ b/lib/postcode_anywhere/email_validation.rb @@ -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' diff --git a/lib/postcode_anywhere/matchers/email_validator.rb b/lib/postcode_anywhere/matchers/email_validator.rb new file mode 100644 index 0000000..6cb0ef1 --- /dev/null +++ b/lib/postcode_anywhere/matchers/email_validator.rb @@ -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 #{should_or_should_not} have 'PostcodeAnywhere::EmailValidation::Validator' on attribute #{attribute}" + end + end + end +end \ No newline at end of file diff --git a/spec/lib/postcode_anywhere/matchers/email_validator_spec.rb b/spec/lib/postcode_anywhere/matchers/email_validator_spec.rb new file mode 100644 index 0000000..e24af1e --- /dev/null +++ b/spec/lib/postcode_anywhere/matchers/email_validator_spec.rb @@ -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 \ No newline at end of file diff --git a/spec/support/matchers.rb b/spec/support/matchers.rb deleted file mode 100644 index 554d55b..0000000 --- a/spec/support/matchers.rb +++ /dev/null @@ -1,28 +0,0 @@ -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) - end - - failure_message_for_should_not do |model| - failure_message(model, 'not') - end - - def failure_message(model, option = '') - "#{model.class} should #{option} have 'PostcodeAnywhere::EmailValidation::Validator' on attribute #{attribute}" - end - -end \ No newline at end of file diff --git a/spec/support/not_validated_class.rb b/spec/support/not_validated_class.rb new file mode 100644 index 0000000..8d92ff5 --- /dev/null +++ b/spec/support/not_validated_class.rb @@ -0,0 +1,4 @@ +class NotValidatedClass + include ActiveModel::Validations +end + From a904dc47adb95b5290a5eb0c5b79bf441b09e6e2 Mon Sep 17 00:00:00 2001 From: Pablo Manrubia Date: Tue, 30 Jul 2013 09:44:03 +0100 Subject: [PATCH 2/4] Fixed typo in Matcher 'should' message @andrewgarner --- lib/postcode_anywhere/matchers/email_validator.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/postcode_anywhere/matchers/email_validator.rb b/lib/postcode_anywhere/matchers/email_validator.rb index 6cb0ef1..15531d7 100644 --- a/lib/postcode_anywhere/matchers/email_validator.rb +++ b/lib/postcode_anywhere/matchers/email_validator.rb @@ -23,7 +23,7 @@ def attribute end def failure_message(model, should_or_should_not) - "#{model.class} should #{should_or_should_not} have 'PostcodeAnywhere::EmailValidation::Validator' on attribute #{attribute}" + "#{model.class} #{should_or_should_not} have 'PostcodeAnywhere::EmailValidation::Validator' on attribute #{attribute}" end end end From eb37a104192a113f8084d6eb90f79da6d4c0f433 Mon Sep 17 00:00:00 2001 From: Pablo Manrubia Date: Tue, 30 Jul 2013 09:43:01 +0100 Subject: [PATCH 3/4] Added coverage folder to .gitignore @andrewgarner --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 5b4ec31..39a0ec0 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ Gemfile.lock pkg .idea .DS_Store +coverage From b86142f5ee0ecdcf23b5d0dc77cc50d1d62324d1 Mon Sep 17 00:00:00 2001 From: Pablo Manrubia Date: Tue, 30 Jul 2013 09:48:23 +0100 Subject: [PATCH 4/4] Version bumped to 0.0.4 @andrewgarner --- lib/postcode_anywhere/email_validation/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/postcode_anywhere/email_validation/version.rb b/lib/postcode_anywhere/email_validation/version.rb index ba95fc6..8c26508 100644 --- a/lib/postcode_anywhere/email_validation/version.rb +++ b/lib/postcode_anywhere/email_validation/version.rb @@ -1,5 +1,5 @@ module PostcodeAnywhere module EmailValidation - VERSION = '0.0.3' + VERSION = '0.0.4' end end