Class: DurableDecorator::Validator

Inherits:
Object
  • Object
show all
Defined in:
lib/durable_decorator/validator.rb

Constant Summary collapse

DECORATION_MODES =
['strict', 'soft']

Class Method Summary collapse

Class Method Details

.handle_soft_fault(clazz, method_name, expected_sha, provided_sha) ⇒ Object



23
24
25
26
# File 'lib/durable_decorator/validator.rb', line 23

def handle_soft_fault(clazz, method_name, expected_sha, provided_sha)
  Util.logger.fatal "#{clazz}##{method_name} decoration uses an invalid SHA. The original method definition could have been tampered with!"
  Util.logger.fatal "Expected SHA was #{expected_sha} but the provided SHA is #{provided_sha}"
end

.handle_strict_fault(clazz, method_name, expected_sha, provided_sha) ⇒ Object



19
20
21
# File 'lib/durable_decorator/validator.rb', line 19

def handle_strict_fault(clazz, method_name, expected_sha, provided_sha)
  raise TamperedDefinitionError, "Method SHA mismatch, the definition has been tampered with. #{expected_sha} is expected but #{provided_sha} was provided."
end

.validate_decoration_meta(clazz, method_name, old_method, meta) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/durable_decorator/validator.rb', line 6

def validate_decoration_meta clazz, method_name, old_method, meta
  return unless meta

  chill_meta = Util.symbolized_hash(meta)
  provided_mode = chill_meta[:mode]
  provided_sha = chill_meta[:sha]
  expected_sha = Util.method_sha(old_method)

  raise InvalidDecorationError, "The :mode provided is invalid. Possible modes are: #{DECORATION_MODES.join(", ")}" unless DECORATION_MODES.include? provided_mode
  raise InvalidDecorationError, "The SHA provided appears to be empty" unless provided_sha and !provided_sha.empty?
  send("handle_#{chill_meta[:mode]}_fault", clazz, method_name, expected_sha, provided_sha) unless expected_sha == provided_sha
end

.validate_existing_definition(clazz, method_name) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/durable_decorator/validator.rb', line 32

def validate_existing_definition clazz, method_name
  begin
    clazz.instance_method(method_name)
  rescue NameError => e
    raise UndefinedMethodError, "#{clazz}##{method_name} is not defined."
  end
end

.validate_method_arity(clazz, method_name, old_method, &block) ⇒ Object

Raises:



28
29
30
# File 'lib/durable_decorator/validator.rb', line 28

def validate_method_arity clazz, method_name, old_method, &block
  raise BadArityError, "Attempting to override #{clazz}'s #{method_name} with incorrect arity." if block.arity != old_method.arity and block.arity > 0 # See the #arity behavior disparity between 1.8- and 1.9+
end