Module: Assistant::InputBuilder

Included in:
Service
Defined in:
lib/assistant/input_builder.rb

Overview

This module has the building blocks for the input validation. The building blocks of listing inputs with the #input and #inputs methods and the building blocks of validating inputs with the methods that are called within those methods.

Instance Method Summary collapse

Instance Method Details

#input(attr_name, type:, **options) ⇒ Object

Individual input with a specific type or options



20
21
22
23
24
25
26
27
28
29
# File 'lib/assistant/input_builder.rb', line 20

def input(attr_name, type:, **options)
  # Base Methods
  input_getter_meth(attr_name)
  input_checker_meth(attr_name)

  # Input type validation method, simple and conditional requirement validation methods
  input_type_validator_meth(attr_name, type)
  input_require_validator_meth(attr_name, **options) if options[:required] == true
  input_require_conditional_meth(attr_name, **options) if options[:required] == true && options[:if]
end

#input_checker_meth(attr_name) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/assistant/input_builder.rb', line 37

def input_checker_meth(attr_name)
  define_method("#{attr_name}?") do
    val = @inputs[attr_name]
    return false if val.nil? || val == false
    return !val.whitespace? if val.is_a?(String)

    val.respond_to?(:empty?) ? !val.empty? : true
  end
end

#input_getter_meth(attr_name) ⇒ Object



31
32
33
34
35
# File 'lib/assistant/input_builder.rb', line 31

def input_getter_meth(attr_name)
  define_method(attr_name) do
    @inputs[attr_name]
  end
end

#input_require_conditional_meth(attr_name, **options) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/assistant/input_builder.rb', line 58

def input_require_conditional_meth(attr_name, **options)
  define_method("valid_require_conditional_#{attr_name}?") do
    return false if send("valid_require_#{attr_name}?", false) == false
    return true if options[:if].call(send(attr_name))

    send(
      :log_item_error_initialize,
      attr_name:, message: "Service argument conditional requirement not met properly for #{attr_name}"
    )
    false
  end
end

#input_require_validator_meth(attr_name, **options) ⇒ Object



47
48
49
50
51
52
53
54
55
56
# File 'lib/assistant/input_builder.rb', line 47

def input_require_validator_meth(attr_name, **options)
  define_method("valid_require_#{attr_name}?") do |log = true|
    return true if options[:required] == true && send("#{attr_name}?") == true

    log && send(
      :log_item_error_initialize, attr_name:, message: "Service is missing argument with name #{attr_name}"
    )
    false
  end
end

#input_type_validator_meth(attr_name, type) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/assistant/input_builder.rb', line 71

def input_type_validator_meth(attr_name, type)
  define_method("valid_type_#{attr_name}?") do
    return true if @inputs[attr_name].is_a?(type)

    send("#{attr_name}?") &&
      send(
        :log_item_error_initialize,
        attr_name:, message: "Service argument with name #{attr_name} is not a #{type} but #{send(attr_name).class}"
      )
    false
  end
end

#inputs(attr_names, type:) ⇒ Object

Lists all inputs that have the same type and options.



13
14
15
16
17
# File 'lib/assistant/input_builder.rb', line 13

def inputs(attr_names, type:, **)
  attr_names.each do |attr_name|
    input(attr_name, type:, **)
  end
end