Class: Polytrix::Validator

Inherits:
Object
  • Object
show all
Includes:
RSpec::Matchers
Defined in:
lib/polytrix/validator.rb

Constant Summary collapse

UNIVERSAL_MATCHER =
//

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(description, scope = {}, &validator) ⇒ Validator

Returns a new instance of Validator.



10
11
12
13
14
15
# File 'lib/polytrix/validator.rb', line 10

def initialize(description, scope = {}, &validator)
  @description = description
  @suite = scope[:suite] ||= UNIVERSAL_MATCHER
  @scenario = scope[:scenario] ||= UNIVERSAL_MATCHER
  @callback = validator
end

Instance Attribute Details

#callbackObject (readonly)

Returns the value of attribute callback.



8
9
10
# File 'lib/polytrix/validator.rb', line 8

def callback
  @callback
end

#descriptionObject (readonly)

Returns the value of attribute description.



8
9
10
# File 'lib/polytrix/validator.rb', line 8

def description
  @description
end

#levelObject (readonly)

Returns the value of attribute level.



8
9
10
# File 'lib/polytrix/validator.rb', line 8

def level
  @level
end

#scenarioObject (readonly)

Returns the value of attribute scenario.



8
9
10
# File 'lib/polytrix/validator.rb', line 8

def scenario
  @scenario
end

#suiteObject (readonly)

Returns the value of attribute suite.



8
9
10
# File 'lib/polytrix/validator.rb', line 8

def suite
  @suite
end

Instance Method Details

#should_validate?(challenge) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
20
# File 'lib/polytrix/validator.rb', line 17

def should_validate?(challenge)
  # TODO: Case-insensitive matching?
  !!(@suite.match(challenge.suite.to_s) && @scenario.match(challenge.name.to_s)) # rubocop:disable Style/DoubleNegation
end

#to_sObject



30
31
32
# File 'lib/polytrix/validator.rb', line 30

def to_s
  @description
end

#validate(challenge) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/polytrix/validator.rb', line 22

def validate(challenge)
  instance_exec(challenge, &@callback) if should_validate?(challenge)
  challenge.result.validations[description] = Validation.new(result: :passed)
rescue StandardError, RSpec::Expectations::ExpectationNotMetError => e
  validation = Validation.new(result: :failed, error: ValidationFailure.new(e.message, e))
  challenge.result.validations[description] = validation
end