Class: Easy::Matchers::Validations::HaveValidationMatcher
- Inherits:
-
Object
- Object
- Easy::Matchers::Validations::HaveValidationMatcher
- Defined in:
- lib/matchers/validations.rb
Instance Method Summary collapse
- #check_on ⇒ Object
- #description ⇒ Object
- #failure_message ⇒ Object
- #failure_message_when_negated ⇒ Object
-
#initialize(field, type) ⇒ HaveValidationMatcher
constructor
A new instance of HaveValidationMatcher.
- #matches?(actual) ⇒ Boolean
- #on(*on_method) ⇒ Object
Constructor Details
#initialize(field, type) ⇒ HaveValidationMatcher
Returns a new instance of HaveValidationMatcher.
5 6 7 8 9 |
# File 'lib/matchers/validations.rb', line 5 def initialize(field, type) @field = field @type = type @options = {} end |
Instance Method Details
#check_on ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/matchers/validations.rb', line 49 def check_on validator_on_methods = [@validator.[:on]].flatten if validator_on_methods.any? = " on methods: #{ validator_on_methods }" if (@validator) @positive_result_message << else @negative_result_message << @result = false end end end |
#description ⇒ Object
38 39 40 41 42 |
# File 'lib/matchers/validations.rb', line 38 def description desc = "have #{ @type } validator on #{ @field }" desc << " on #{ @options[:on] }" if @options[:on] desc end |
#failure_message ⇒ Object
30 31 32 |
# File 'lib/matchers/validations.rb', line 30 def "Expected #{ @klass.inspect } to #{ description }; instead got #{ @negative_result_message }" end |
#failure_message_when_negated ⇒ Object
34 35 36 |
# File 'lib/matchers/validations.rb', line 34 def "Expected #{ @klass.inspect } to not #{ description }; instead got #{ @positive_result_message }" end |
#matches?(actual) ⇒ Boolean
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/matchers/validations.rb', line 11 def matches?(actual) @klass = actual.is_a?(Class) ? actual : actual.class @validator = @klass.validators_on(@field).detect{ |v| v.kind == @type && (!v.[:on] || (v)) } if @validator @negative_result_message = "#{ @type.inspect } validator on #{ @field.inspect }" @positive_result_message = "#{ @type.inspect } validator on #{ @field.inspect }" else @negative_result_message = "no #{ @type.inspect } validator on #{ @field.inspect }" return false end @result = true check_on if @options[:on] @result end |
#on(*on_method) ⇒ Object
44 45 46 47 |
# File 'lib/matchers/validations.rb', line 44 def on(*on_method) @options[:on] = on_method.flatten self end |