class Normalizy::RSpec::Matcher

Public Class Methods

new(attribute) click to toggle source
# File lib/normalizy/rspec/matcher.rb, line 10
def initialize(attribute)
  @attribute = attribute
end

Public Instance Methods

description() click to toggle source
# File lib/normalizy/rspec/matcher.rb, line 14
def description
  return "normalizy #{@attribute} with #{with_expected}" if @with.present?

  "normalizy #{@attribute} from #{from_value} to #{to_value}"
end
failure_message() click to toggle source
# File lib/normalizy/rspec/matcher.rb, line 20
def failure_message
  return "expected: #{with_expected}\n     got: #{actual_value}" if @with.present?

  "expected: #{to_value}\n     got: #{actual_value}"
end
failure_message_when_negated() click to toggle source
# File lib/normalizy/rspec/matcher.rb, line 26
def failure_message_when_negated
  return "expected: value != #{with_expected}\n     got: #{actual_value}" if @with.present?

  "expected: value != #{to_value}\n     got: #{actual_value}"
end
from(value) click to toggle source
# File lib/normalizy/rspec/matcher.rb, line 32
def from(value)
  @from = value

  self
end
matches?(subject) click to toggle source
# File lib/normalizy/rspec/matcher.rb, line 38
def matches?(subject)
  @subject = subject

  if @with.present?
    options = @subject.class.normalizy_rules[@attribute]

    return false if options.blank?

    options = default_rules if options.map { |option| option[:rules] }.compact.blank?

    return false if options.blank?

    options.each do |option|
      rules = option[:rules]

      return true if rules.is_a?(Array) && rules.include?(@with)
      return true if rules == @with
    end

    false
  else
    @subject.send :"#{@attribute}=", @from

    @subject[@attribute] == @to
  end
end
to(value) click to toggle source
# File lib/normalizy/rspec/matcher.rb, line 65
def to(value)
  @to = value

  self
end
with(value) click to toggle source
# File lib/normalizy/rspec/matcher.rb, line 71
def with(value)
  @with = value

  self
end

Private Instance Methods

actual_value() click to toggle source
# File lib/normalizy/rspec/matcher.rb, line 79
def actual_value
  return with_value if @with

  value = @subject.send(@attribute)

  value.is_a?(String) ? %("#{value}") : value
end
default_rules() click to toggle source
# File lib/normalizy/rspec/matcher.rb, line 87
def default_rules
  [Normalizy.config.default_filters].flatten.compact.map do |rule|
    { rules: rule }
  end
end
from_value() click to toggle source
# File lib/normalizy/rspec/matcher.rb, line 93
def from_value
  @from.nil? ? :nil : %("#{@from}")
end
to_value() click to toggle source
# File lib/normalizy/rspec/matcher.rb, line 97
def to_value
  @to.nil? ? :nil : %("#{@to}")
end
with_expected() click to toggle source
# File lib/normalizy/rspec/matcher.rb, line 101
def with_expected
  @with
end
with_value() click to toggle source
# File lib/normalizy/rspec/matcher.rb, line 105
def with_value
  options = @subject.class.normalizy_rules[@attribute]

  return :nil            if options.nil?
  return %("#{options}") if options.blank?

  result = options.map do |option|
    rules = option[:rules]

    return :nil if rules.nil?

    rules.presence || %("#{rules}")
  end

  result.join ', '
end