Class: ValidateMyRoutes::Validate::ValidationRule

Inherits:
Object
  • Object
show all
Extended by:
Macros
Includes:
RulesCombinators
Defined in:
lib/validate_my_routes/validate/validation_rule.rb

Overview

ValidationRule is a base class for all rules

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from RulesCombinators

#and, #negate, #or

Constructor Details

#initialize(rule_name, rule_type, *expected, declarations) ⇒ ValidationRule

Returns a new instance of ValidationRule.



14
15
16
17
18
19
# File 'lib/validate_my_routes/validate/validation_rule.rb', line 14

def initialize(rule_name, rule_type, *expected, declarations)
  self.rule_name = rule_name
  self.rule_type = rule_type
  self.app = nil # this is a Sinatra application instance
  singleton_class.class_exec(*expected, &declarations)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object

Expand method lookup to the application scope



75
76
77
# File 'lib/validate_my_routes/validate/validation_rule.rb', line 75

def method_missing(method_name, *args, &block)
  app && app.respond_to?(method_name) ? app.send(method_name, *args, &block) : super
end

Instance Attribute Details

#rule_typeObject

Returns the value of attribute rule_type.



12
13
14
# File 'lib/validate_my_routes/validate/validation_rule.rb', line 12

def rule_type
  @rule_type
end

Instance Method Details

#descriptionObject



44
45
46
# File 'lib/validate_my_routes/validate/validation_rule.rb', line 44

def description
  rule_name.to_s.capitalize.tr('_', ' ')
end

#failure_code(in_path) ⇒ Object



48
49
50
# File 'lib/validate_my_routes/validate/validation_rule.rb', line 48

def failure_code(in_path)
  in_path ? 404 : 400
end

#failure_message(*args) ⇒ Object



52
53
54
55
56
57
58
59
60
61
# File 'lib/validate_my_routes/validate/validation_rule.rb', line 52

def failure_message(*args)
  if args.size == 1
    "parameters were expected to satisfy: #{description} but were <#{args[0]}>"
  elsif args.size == 2
    "parameter #{args[1]} was expected to satisfy: #{description} but was <#{args[0]}>"
  else
    raise Errors::MissusedRuleError, "failure_message method called with #{args.size} " \
                                     'arguments'
  end
end

#failure_message_when_negated(*args) ⇒ Object



63
64
65
66
67
68
69
70
71
72
# File 'lib/validate_my_routes/validate/validation_rule.rb', line 63

def failure_message_when_negated(*args)
  if args.size == 1
    "parameters were expected not to satisfy: #{description} but were <#{args[0]}>"
  elsif args.size == 2
    "parameter #{args[1]} was expected not to satisfy: #{description} but was <#{args[0]}>"
  else
    raise Errors::MissusedRuleError, 'failure_message_when_negated method called with ' \
                                     "#{args.size} arguments"
  end
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/validate_my_routes/validate/validation_rule.rb', line 79

def respond_to_missing?(method_name, include_private = false)
  super || app.respond_to?(method_name) || super
end

#validate(*_args) ⇒ Object



40
41
42
# File 'lib/validate_my_routes/validate/validation_rule.rb', line 40

def validate(*_args)
  raise Errors::MissusedRuleError, 'validate method not implemented'
end

#validate!(app, value, path_param, *args) ⇒ Object

Current method can be used for validation



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/validate_my_routes/validate/validation_rule.rb', line 22

def validate!(app, value, path_param, *args)
  # save current Sinatra app instance for method lookup on it
  self.app = app

  self.value = value
  self.path_param = path_param == true

  validate(value, *args) || fail_validation(failure_message(value, *args))
rescue Errors::ValidationError
  # validation failed, so just re-raise an error to buble it up to the root
  # re-raising is needed in order to catch all other exceptions to wrap them in
  # special error
  raise
rescue => ex # rubocop:disable Style/RescueStandardError
  # unexpected exception happened in validation block, so we should wrap it in special error
  raise Errors::ValidationRaisedAnExceptionError.new(ex, failure_code(path_param?))
end