Module: HatiCommand::Cmd

Defined in:
lib/hati_command/cmd.rb

Overview

Dev-friendly extension of the Befehl core module with Railway track API. This module provides a Railway-oriented programming interface for handling success and failure states in a functional way, making it easier to chain operations and handle errors gracefully.

Examples:

class MyCommand
  include HatiCommand::Cmd

  def call(input)
    if valid?(input)
      Success(input)
    else
      Failure("Invalid input")
    end
  end
end

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ void

This method returns an undefined value.

Includes the module in the base class and sets up necessary configurations

Parameters:

  • base (Class)

    The class including this module



28
29
30
31
# File 'lib/hati_command/cmd.rb', line 28

def self.included(base)
  base.extend(HatiCommand::Befehl)
  base.private_class_method :new
end

.whoamiString

Note:

This is a work in progress method

Returns the identity of the module

Returns:

  • (String)

    The module’s identity string



37
38
39
# File 'lib/hati_command/cmd.rb', line 37

def self.whoami
  'My Name is Cmd'
end

Instance Method Details

#Failure(value = nil, err: nil, meta: {}) ⇒ HatiCommand::Failure

Creates a Failure monad representing a failed operation

Examples:

Failure("Operation failed", err: StandardError.new, meta: { reason: "invalid_input" })

Parameters:

  • value (Object, nil) (defaults to: nil)

    The value to wrap in the Failure monad

  • err (Object, nil) (defaults to: nil)

    Optional error object (falls back to configured default)

  • meta (Hash) (defaults to: {})

    Additional metadata for the failure state

Returns:



59
60
61
62
# File 'lib/hati_command/cmd.rb', line 59

def Failure(value = nil, err: nil, meta: {}) # rubocop:disable Naming/MethodName
  default_err = self.class.command_config[:failure]
  HatiCommand::Failure.new(value, err: err || default_err, meta: meta)
end

#Failure!(value = nil, err: nil, meta: {}, **_opts) ⇒ Object

Creates a Failure monad and immediately raises a FailFastError

Examples:

Failure!("Critical error", err: FatalError.new)

Parameters:

  • value (Object, nil) (defaults to: nil)

    The value to wrap in the Failure monad

  • err (Object, nil) (defaults to: nil)

    Optional error object (falls back to configured defaults)

  • meta (Hash) (defaults to: {})

    Additional metadata for the failure state

  • _opts (Hash)

    Additional options (currently unused)

Raises:



72
73
74
75
76
77
78
# File 'lib/hati_command/cmd.rb', line 72

def Failure!(value = nil, err: nil, meta: {}, **_opts) # rubocop:disable Naming/MethodName
  default_error = self.class.command_config[:fail_fast] || self.class.command_config[:failure]
  error = err || default_error

  failure_obj = HatiCommand::Failure.new(value, err: error, meta: meta)
  raise HatiCommand::Errors::FailFastError.new('Fail Fast Triggered', failure_obj: failure_obj)
end

#Success(value = nil, err: nil, meta: {}) ⇒ HatiCommand::Success

Creates a Success monad representing a successful operation

Examples:

Success("Operation completed", meta: { time: Time.now })

Parameters:

  • value (Object, nil) (defaults to: nil)

    The value to wrap in the Success monad

  • err (Object, nil) (defaults to: nil)

    Optional error object

  • meta (Hash) (defaults to: {})

    Additional metadata for the success state

Returns:



48
49
50
# File 'lib/hati_command/cmd.rb', line 48

def Success(value = nil, err: nil, meta: {}) # rubocop:disable Naming/MethodName
  HatiCommand::Success.new(value, err: err, meta: meta)
end