File: option_definition.rb

package info (click to toggle)
ruby-process-executer 4.0.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 408 kB
  • sloc: ruby: 873; makefile: 4
file content (60 lines) | stat: -rw-r--r-- 1,706 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# frozen_string_literal: true

module ProcessExecuter
  module Options
    # Defines an option that can be used by an Options object
    #
    # @api public
    #
    class OptionDefinition
      # The name of the option
      #
      # @example
      #   option = ProcessExecuter::Options::OptionDefinition.new(:timeout_after)
      #   option.name # => :timeout_after
      #
      # @return [Symbol]
      #
      attr_reader :name

      # The default value of the option
      #
      # @example
      #   option = ProcessExecuter::Options::OptionDefinition.new(:timeout_after, default: 10)
      #   option.default # => 10
      #
      # @return [Object]
      #
      attr_reader :default

      # A method or proc that validates the option
      #
      # A callable that receives `option_key`, `option_value` and is executed in the
      # context of the options instance. It should add messages to an `errors` array
      # if validation fails.
      #
      # @example
      #   option = ProcessExecuter::Options::OptionDefinition.new(
      #     :timeout_after, validator: method(:validate_timeout_after)
      #   )
      #   option.validator # => #<Method: ProcessExecuter#validate_timeout_after>
      #
      # @return [Method, Proc, nil]
      #
      attr_reader :validator

      # Create a new option definition
      #
      # @example
      #   option = ProcessExecuter::Options::OptionDefinition.new(
      #     :timeout_after, default: 10, validator: ->(_k, _v) { timeout_after.is_a?(Numeric) }
      #   )
      #
      def initialize(name, default: nil, validator: nil)
        @name = name
        @default = default
        @validator = validator
      end
    end
  end
end