class Tsubaki::Shoulda::Matchers::ValidateCorporateNumberOfMatcher

Public Class Methods

new(attribute_name) click to toggle source
# File lib/tsubaki/matchers/validate_corporate_number_of_matcher.rb, line 17
def initialize(attribute_name)
  @attribute_name = attribute_name
  @options = {}
  @options[:strict] = nil
  @options[:divider] = nil
  @options[:allow_blank] = nil
  @failure_messages = []
  self
end

Public Instance Methods

allow_blank(allow_blank = true) click to toggle source
# File lib/tsubaki/matchers/validate_corporate_number_of_matcher.rb, line 56
def allow_blank(allow_blank = true)
  @options[:allow_blank] = allow_blank
  self
end
description() click to toggle source
# File lib/tsubaki/matchers/validate_corporate_number_of_matcher.rb, line 37
def description
  result = 'ensure corporate number format'
  result << " for #{@attribute_name}"
  result << ' with strict mode' if @options[:strict].present?
  result << " with divider '#{@options[:divider]}'" if @options[:divider].present?
  result << ' and allow blank' if @options[:allow_blank].present?
  result
end
failure_message() click to toggle source
# File lib/tsubaki/matchers/validate_corporate_number_of_matcher.rb, line 33
def failure_message
  @failure_messages.join("\n")
end
matches?(subject) click to toggle source
# File lib/tsubaki/matchers/validate_corporate_number_of_matcher.rb, line 27
def matches?(subject)
  @subject = subject
  @subject = @subject.new if @subject.class == Class
  (error_when_not_valid + no_error_when_valid).all?
end
strict(strict = true) click to toggle source
# File lib/tsubaki/matchers/validate_corporate_number_of_matcher.rb, line 46
def strict(strict = true)
  @options[:strict] = strict
  self
end
with_divider(divider) click to toggle source
# File lib/tsubaki/matchers/validate_corporate_number_of_matcher.rb, line 51
def with_divider(divider)
  @options[:divider] = divider
  self
end

Protected Instance Methods

error_when_not_valid() click to toggle source
# File lib/tsubaki/matchers/validate_corporate_number_of_matcher.rb, line 63
def error_when_not_valid
  [error_test_allow_blank, error_test_strict, error_test_divider, error_test]
end
no_error_when_valid() click to toggle source
# File lib/tsubaki/matchers/validate_corporate_number_of_matcher.rb, line 67
def no_error_when_valid
  [no_error_test_allow_blank, no_error_test_strict, no_error_test_divider, no_error_test]
end

Private Instance Methods

error_test() click to toggle source
# File lib/tsubaki/matchers/validate_corporate_number_of_matcher.rb, line 109
def error_test
  if valid_attribute_with?('xaaaabbbbcccc')
    @failure_messages << 'corporate number validation is not specified'
    false
  else
    true
  end
end
error_test_allow_blank() click to toggle source
# File lib/tsubaki/matchers/validate_corporate_number_of_matcher.rb, line 80
def error_test_allow_blank
  true
end
error_test_divider() click to toggle source
# File lib/tsubaki/matchers/validate_corporate_number_of_matcher.rb, line 95
def error_test_divider
  return true if @options[:divider].nil?

  dummy_divider = @options[:divider].succ
  invalid_corporate_number = "7#{dummy_divider}1234#{dummy_divider}5678#{dummy_divider}9012"

  if valid_attribute_with?(invalid_corporate_number)
    @failure_messages << "divider is not be specified or is not '#{@options[:divider]}'"
    false
  else
    true
  end
end
error_test_strict() click to toggle source
# File lib/tsubaki/matchers/validate_corporate_number_of_matcher.rb, line 84
def error_test_strict
  return true if @options[:strict].nil?

  if valid_attribute_with?('1111111111111')
    @failure_messages << 'strict mode is not be specified'
    false
  else
    true
  end
end
no_error_test() click to toggle source
# File lib/tsubaki/matchers/validate_corporate_number_of_matcher.rb, line 146
def no_error_test
  if !valid_attribute_with?('7123456789012')
    @failure_messages << 'corporate number validation is not specified'
    false
  else
    true
  end
end
no_error_test_allow_blank() click to toggle source
# File lib/tsubaki/matchers/validate_corporate_number_of_matcher.rb, line 118
def no_error_test_allow_blank
  return true if @options[:allow_blank].nil?

  if !valid_attribute_with?(nil)
    @failure_messages << 'allow_blank is not be specified'
    false
  else
    true
  end
end
no_error_test_divider() click to toggle source
# File lib/tsubaki/matchers/validate_corporate_number_of_matcher.rb, line 133
def no_error_test_divider
  return true if @options[:divider].nil?

  valid_corporate_number = "7#{@options[:divider]}1234#{@options[:divider]}5678#{@options[:divider]}9012"

  if !valid_attribute_with?(valid_corporate_number)
    @failure_messages << "divider is not be specified or is not '#{@options[:divider]}'"
    false
  else
    true
  end
end
no_error_test_strict() click to toggle source
# File lib/tsubaki/matchers/validate_corporate_number_of_matcher.rb, line 129
def no_error_test_strict
  true
end
valid_attribute_with?(value) click to toggle source
# File lib/tsubaki/matchers/validate_corporate_number_of_matcher.rb, line 73
def valid_attribute_with?(value)
  dup_subject = @subject.dup
  dup_subject.send("#{@attribute_name}=", value)
  dup_subject.valid?
  dup_subject.errors[@attribute_name].blank?
end