Class: Normalizy::RSpec::Matcher

Inherits:
Object
  • Object
show all
Defined in:
lib/normalizy/rspec/matcher.rb

Instance Method Summary collapse

Constructor Details

#initialize(attribute) ⇒ Matcher

Returns a new instance of Matcher.



10
11
12
# File 'lib/normalizy/rspec/matcher.rb', line 10

def initialize(attribute)
  @attribute = attribute
end

Instance Method Details

#descriptionObject



14
15
16
17
18
# 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_messageObject



20
21
22
23
24
# 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_negatedObject



26
27
28
29
30
# 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) ⇒ Object



32
33
34
35
36
# File 'lib/normalizy/rspec/matcher.rb', line 32

def from(value)
  @from = value

  self
end

#matches?(subject) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# 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) ⇒ Object



65
66
67
68
69
# File 'lib/normalizy/rspec/matcher.rb', line 65

def to(value)
  @to = value

  self
end

#with(value) ⇒ Object



71
72
73
74
75
# File 'lib/normalizy/rspec/matcher.rb', line 71

def with(value)
  @with = value

  self
end