Module: Domainic::Command::ClassMethods

Defined in:
lib/domainic/command/class_methods.rb

Overview

Class methods that are extended onto any class that includes Domainic::Command. These methods provide the DSL for defining command inputs and outputs, as well as class-level execution methods.

Author:

Since:

  • 0.1.0

Instance Method Summary collapse

Instance Method Details

#accepts_arguments_matching(input_context_class) ⇒ void

This method returns an undefined value.

Specifies an external input context class for the command

Parameters:

Raises:

Since:

  • 0.1.0



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/domainic/command/class_methods.rb', line 26

def accepts_arguments_matching(input_context_class)
  unless input_context_class < Context::InputContext
    raise ArgumentError, 'Input context class must be a subclass of Context::InputContext'
  end

  # @type self: Class & ClassMethods
  @input_context_class = begin
    const_set(:InputContext, Class.new(input_context_class))
    const_get(:InputContext)
  end
end

#argument(name, *type_validator_and_description, **options) ⇒ void

This method returns an undefined value.

Defines an input argument for the command

Parameters:

  • name (String, Symbol)

    The name of the argument

  • type_validator_and_description (Array<Class, Module, Object, Proc, String, nil>)

    Type validator or description arguments

  • options (Hash)

    Configuration options for the argument

Options Hash (**options):

  • :default (Object)

    A static default value

  • :default_generator (Proc)

    A proc that generates the default value

  • :default_value (Object)

    Alias for :default

  • :desc (String, nil)

    Short description of the argument

  • :description (String, nil)

    Full description of the argument

  • :required (Boolean)

    Whether the argument is required

  • :type (Class, Module, Object, Proc)

    A type validator

Since:

  • 0.1.0



65
66
67
# File 'lib/domainic/command/class_methods.rb', line 65

def argument(...)
  input_context_class.argument(...)
end

#call(**context) ⇒ Result

Executes the command with the given context, handling any errors

Parameters:

  • context (Hash)

    The input context for the command

Returns:

  • (Result)

    The result of the command execution

Since:

  • 0.1.0



75
76
77
78
# File 'lib/domainic/command/class_methods.rb', line 75

def call(**context)
  # @type self: Class & ClassMethods & InstanceMethods
  new.call(**context)
end

#call!(**context) ⇒ Result

Executes the command with the given context, raising any errors

Parameters:

  • context (Hash)

    The input context for the command

Returns:

  • (Result)

    The result of the command execution

Raises:

Since:

  • 0.1.0



87
88
89
90
# File 'lib/domainic/command/class_methods.rb', line 87

def call!(**context)
  # @type self: Class & ClassMethods & InstanceMethods
  new.call!(**context)
end

#output(name, *type_validator_and_description, **options) ⇒ void

This method returns an undefined value.

Defines an output field for the command

Parameters:

  • name (String, Symbol)

    The name of the output field

  • type_validator_and_description (Array<Class, Module, Object, Proc, String, nil>)

    Type validator or description arguments

  • options (Hash)

    Configuration options for the output

Options Hash (**options):

  • :default (Object)

    A static default value

  • :default_generator (Proc)

    A proc that generates the default value

  • :default_value (Object)

    Alias for :default

  • :desc (String, nil)

    Short description of the output

  • :description (String, nil)

    Full description of the output

  • :required (Boolean)

    Whether the output is required

  • :type (Class, Module, Object, Proc)

    A type validator

Since:

  • 0.1.0



119
120
121
# File 'lib/domainic/command/class_methods.rb', line 119

def output(...)
  output_context_class.field(...)
end

#returns_data_matching(output_context_class) ⇒ void

This method returns an undefined value.

Specifies an external output context class for the command

Parameters:

Raises:

Since:

  • 0.1.0



130
131
132
133
134
135
136
137
138
139
140
# File 'lib/domainic/command/class_methods.rb', line 130

def returns_data_matching(output_context_class)
  unless output_context_class < Context::OutputContext
    raise ArgumentError, 'Output context class must be a subclass of Context::OutputContext'
  end

  # @type self: Class & ClassMethods
  @output_context_class = begin
    const_set(:OutputContext, Class.new(output_context_class))
    const_get(:OutputContext)
  end
end