Class: Polytrix::Validator
- Inherits:
-
Object
- Object
- Polytrix::Validator
- Includes:
- RSpec::Matchers
- Defined in:
- lib/polytrix/validator.rb
Constant Summary collapse
- UNIVERSAL_MATCHER =
//
Instance Attribute Summary collapse
-
#callback ⇒ Object
readonly
Returns the value of attribute callback.
-
#description ⇒ Object
readonly
Returns the value of attribute description.
-
#level ⇒ Object
readonly
Returns the value of attribute level.
-
#scenario ⇒ Object
readonly
Returns the value of attribute scenario.
-
#suite ⇒ Object
readonly
Returns the value of attribute suite.
Instance Method Summary collapse
-
#initialize(description, scope = {}, &validator) ⇒ Validator
constructor
A new instance of Validator.
- #should_validate?(challenge) ⇒ Boolean
- #to_s ⇒ Object
- #validate(challenge) ⇒ Object
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
#callback ⇒ Object (readonly)
Returns the value of attribute callback.
8 9 10 |
# File 'lib/polytrix/validator.rb', line 8 def callback @callback end |
#description ⇒ Object (readonly)
Returns the value of attribute description.
8 9 10 |
# File 'lib/polytrix/validator.rb', line 8 def description @description end |
#level ⇒ Object (readonly)
Returns the value of attribute level.
8 9 10 |
# File 'lib/polytrix/validator.rb', line 8 def level @level end |
#scenario ⇒ Object (readonly)
Returns the value of attribute scenario.
8 9 10 |
# File 'lib/polytrix/validator.rb', line 8 def scenario @scenario end |
#suite ⇒ Object (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
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_s ⇒ Object
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., e)) challenge.result.validations[description] = validation end |