class Tsubaki::Shoulda::Matchers::ValidateMyNumberOfMatcher

Public Class Methods

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

Public Instance Methods

allow_nil(allow_nil = true) click to toggle source
# File lib/tsubaki/matchers/validate_my_number_of_matcher.rb, line 56
def allow_nil(allow_nil = true)
  @options[:allow_nil] = allow_nil
  self
end
description() click to toggle source
# File lib/tsubaki/matchers/validate_my_number_of_matcher.rb, line 37
def description
  result = 'ensure my 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 nil' if @options[:allow_nil].present?
  result
end
failure_message() click to toggle source
# File lib/tsubaki/matchers/validate_my_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_my_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_my_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_my_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_my_number_of_matcher.rb, line 63
def error_when_not_valid
  [error_test_allow_nil, error_test_strict, error_test_divider, error_test]
end
no_error_when_valid() click to toggle source
# File lib/tsubaki/matchers/validate_my_number_of_matcher.rb, line 67
def no_error_when_valid
  [no_error_test_allow_nil, 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_my_number_of_matcher.rb, line 109
def error_test
  if valid_attribute_with?('aaaabbbbcccc')
    @failure_messages << 'my number validation is not specified'
    false
  else
    true
  end
end
error_test_allow_nil() click to toggle source
# File lib/tsubaki/matchers/validate_my_number_of_matcher.rb, line 80
def error_test_allow_nil
  true
end
error_test_divider() click to toggle source
# File lib/tsubaki/matchers/validate_my_number_of_matcher.rb, line 95
def error_test_divider
  return true if @options[:divider].nil?

  dummy_divider = @options[:divider].succ
  invalid_my_number = "9656#{dummy_divider}7219#{dummy_divider}7231"

  if valid_attribute_with?(invalid_my_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_my_number_of_matcher.rb, line 84
def error_test_strict
  return true if @options[:strict].nil?

  if valid_attribute_with?('111111111111')
    @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_my_number_of_matcher.rb, line 146
def no_error_test
  if !valid_attribute_with?('965672197231')
    @failure_messages << 'my number validation is not specified'
    false
  else
    true
  end
end
no_error_test_allow_nil() click to toggle source
# File lib/tsubaki/matchers/validate_my_number_of_matcher.rb, line 118
def no_error_test_allow_nil
  return true if @options[:allow_nil].nil?

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

  valid_my_number = "9656#{@options[:divider]}7219#{@options[:divider]}7231"

  if !valid_attribute_with?(valid_my_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_my_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_my_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