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 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/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 diff --git a/lib/postcode_anywhere/matchers/email_validator.rb b/lib/postcode_anywhere/matchers/email_validator.rb new file mode 100644 index 0000000..15531d7 --- /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_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 +