Module: MimeActor::Validator::ClassMethods

Defined in:
lib/mime_actor/validator.rb

Instance Method Summary collapse

Instance Method Details

#validate!(rule, *args) ⇒ Object

Raise the error returned by validator if any.

Parameters:

  • rule

    the name of validator

Raises:

  • (NameError)


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

def validate!(rule, *args)
  validator = "validate_#{rule}"
  raise NameError, "Validator not found, got: #{validator.inspect}" unless respond_to?(validator)

  error = send(validator, *args)
  raise error if error
end

#validate_action(unchecked) ⇒ Object

Validate ‘action` must be a Symbol

Parameters:

  • unchecked

    the ‘action` to be validated



44
45
46
# File 'lib/mime_actor/validator.rb', line 44

def validate_action(unchecked)
  TypeError.new("action must be a Symbol") unless unchecked.is_a?(Symbol)
end

#validate_action_or_actions(unchecked) ⇒ Object

Validate against ‘actions` rule if argument is a Enumerable. otherwise, validate against `action` rule.

Parameters:

  • unchecked

    the ‘actions` or `action` to be validated



61
62
63
# File 'lib/mime_actor/validator.rb', line 61

def validate_action_or_actions(unchecked)
  unchecked.is_a?(Enumerable) ? validate_actions(unchecked) : validate_action(unchecked)
end

#validate_actions(unchecked) ⇒ Object

Validate ‘actions` must be a collection of Symbol

Parameters:

  • unchecked

    the ‘actions` to be validated



51
52
53
54
55
56
# File 'lib/mime_actor/validator.rb', line 51

def validate_actions(unchecked)
  return TypeError.new("actions must not be empty") if unchecked.empty?

  rejected = unchecked.reject { |action| action.is_a?(Symbol) }
  NameError.new("invalid actions, got: #{rejected.map(&:inspect).join(", ")}") if rejected.size.positive?
end

#validate_callable(unchecked) ⇒ Object

Validate ‘callable` must be a Symbol or Proc

Parameters:

  • unchecked

    the ‘callable` to be validated



106
107
108
109
110
# File 'lib/mime_actor/validator.rb', line 106

def validate_callable(unchecked)
  return if unchecked.is_a?(Proc) || unchecked.is_a?(Symbol)

  TypeError.new("#{unchecked.inspect} must be a Symbol or Proc")
end

#validate_format(unchecked) ⇒ Object

Validate ‘format` must be a Symbol and a valid MIME type

Parameters:

  • unchecked

    the ‘format` to be validated



68
69
70
71
72
# File 'lib/mime_actor/validator.rb', line 68

def validate_format(unchecked)
  return TypeError.new("format must be a Symbol") unless unchecked.is_a?(Symbol)

  NameError.new("invalid format, got: #{unchecked.inspect}") unless scene_formats.include?(unchecked)
end

#validate_format_or_formats(unchecked) ⇒ Object

Validate against ‘formats` rule if argument is a Enumerable. otherwise, validate against `format` rule.

Parameters:

  • unchecked

    the ‘formats` or `format` to be validated



90
91
92
# File 'lib/mime_actor/validator.rb', line 90

def validate_format_or_formats(unchecked)
  unchecked.is_a?(Enumerable) ? validate_formats(unchecked) : validate_format(unchecked)
end

#validate_formats(unchecked) ⇒ Object

Validate ‘formats` must be an collection of Symbol which each of them is a valid MIME type

Parameters:

  • unchecked

    the ‘formats` to be validated



77
78
79
80
81
82
83
84
85
# File 'lib/mime_actor/validator.rb', line 77

def validate_formats(unchecked)
  return TypeError.new("formats must not be empty") if unchecked.empty?

  unfiltered = unchecked.to_set
  filtered = unfiltered & scene_formats
  rejected = unfiltered - filtered

  NameError.new("invalid formats, got: #{rejected.map(&:inspect).join(", ")}") if rejected.size.positive?
end

#validate_klazz(unchecked) ⇒ Object

Validate ‘klazz` must be a Class/Module or a String referencing a Class/Module

Parameters:

  • unchecked

    the ‘klazz` to be validated



97
98
99
100
101
# File 'lib/mime_actor/validator.rb', line 97

def validate_klazz(unchecked)
  return if unchecked.is_a?(Module) || unchecked.is_a?(String)

  TypeError.new("#{unchecked.inspect} must be a Class/Module or a String referencing a Class/Module")
end