Class: HasFace::Test::Matchers::ValidateHasFaceFor

Inherits:
Object
  • Object
show all
Defined in:
lib/has_face/test/matchers.rb

Instance Method Summary collapse

Constructor Details

#initialize(expected, options) ⇒ ValidateHasFaceFor

Returns a new instance of ValidateHasFaceFor.



7
8
9
10
# File 'lib/has_face/test/matchers.rb', line 7

def initialize(expected, options)
  @expected = expected
  @options  = options
end

Instance Method Details

#failure_messageObject



24
25
26
# File 'lib/has_face/test/matchers.rb', line 24

def failure_message
  "expected #{@actual.class.to_s} to validate has face for #{@expected.inspect} #{with_options_message}"
end

#has_correct_allow_blank(validator) ⇒ Object



32
33
34
35
# File 'lib/has_face/test/matchers.rb', line 32

def has_correct_allow_blank(validator)
  return true if @options[:allow_blank].nil?
  validator.instance_variable_get('@allow_blank') == @options[:allow_blank]
end

#has_correct_allow_nil(validator) ⇒ Object



37
38
39
40
# File 'lib/has_face/test/matchers.rb', line 37

def has_correct_allow_nil(validator)
  return true if @options[:allow_nil].nil?
  validator.instance_variable_get('@allow_nil') == @options[:allow_nil]
end

#matches?(actual) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
15
16
17
18
19
20
21
22
# File 'lib/has_face/test/matchers.rb', line 12

def matches?(actual)
  @actual = actual

  @actual.class.validators.any? do |validator|
    validator.is_a?(HasFace::Validator) && \
      validator.attributes.include?(@expected) && \
      has_correct_allow_blank(validator) && \
      has_correct_allow_nil(validator)
  end

end

#negative_failure_messageObject



28
29
30
# File 'lib/has_face/test/matchers.rb', line 28

def negative_failure_message
  "expected #{@actual.class.to_s} not to validate has face for #{@expected.inspect} #{with_options_message}"
end

#with_options_messageObject



42
43
44
45
46
# File 'lib/has_face/test/matchers.rb', line 42

def with_options_message
  if @options.present?
    "with options #{@options.inspect}"
  end
end