Class: Sanitize::Rails::Matchers::SanitizeFieldsMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/sanitize/rails/matchers.rb

Overview

Actual matcher class

Instance Method Summary collapse

Constructor Details

#initialize(*fields) ⇒ SanitizeFieldsMatcher

Take an array of fields to check, they must respect the same order given in model ‘sanitize` call



63
64
65
66
67
# File 'lib/sanitize/rails/matchers.rb', line 63

def initialize(*fields)
  self.options = fields.extract_options!
  self.sanitizer = ::Sanitize::Rails::Engine.method_for(fields)
  self.fields = fields
end

Instance Method Details

#descriptionObject



101
102
103
# File 'lib/sanitize/rails/matchers.rb', line 101

def description
  "sanitize #{should_helper}"
end

#matches?(instance) ⇒ Boolean

Actual match code

Returns:

  • (Boolean)


84
85
86
87
88
89
90
91
92
# File 'lib/sanitize/rails/matchers.rb', line 84

def matches?(instance)
  self.instance = instance
  # assign invalid value to each field
  fields.each { |field| instance.send("#{field}=", invalid_value) }
  # sanitize the object calling the method
  instance.send(sanitizer) rescue nil
  # check expectation on results
  fields.all? { |field| valid_value == instance.send(field) }
end

#replacing(invalid) ⇒ Object

Used to specify invalid text assigned to fields



70
71
72
73
74
# File 'lib/sanitize/rails/matchers.rb', line 70

def replacing(invalid)
  @invalid_changed = true
  @invalid = invalid
  self
end

#with(valid) ⇒ Object

Used to specify expected output for the invalid text



77
78
79
80
81
# File 'lib/sanitize/rails/matchers.rb', line 77

def with(valid)
  @valid_changed = true
  @valid = valid
  self
end