Module: Teckel::Operation::Config

Defined in:
lib/teckel/operation/config.rb

Shortcuts collapse

Instance Method Summary collapse

Instance Method Details

#cloneself

Produces a clone of this operation and all it’s configuration

Returns:

  • (self)


344
345
346
347
348
349
350
# File 'lib/teckel/operation/config.rb', line 344

def clone
  if frozen?
    super()
  else
    dup_config(super())
  end
end

#default_settingsnil|#call

Getter for configured default settings

Returns:

  • (nil|#call)

    The callable constructor



230
231
232
# File 'lib/teckel/operation/config.rb', line 230

def default_settings
  @config.for(:default_settings)
end

#default_settings!Object #default_settings!(sym_or_proc) ⇒ Object #default_settings!(arg1, arg2, ...) ⇒ Object

Declare default settings this operation should use when called without #with. When executing a Operation, settings will no longer be nil, but whatever you define here.

Explicit call-time settings will not get merged with declared default setting.

Overloads:

  • #default_settings!Object

    When this operation is called without #with, settings will be an instance of the settings class, initialized with no arguments.

  • #default_settings!(sym_or_proc) ⇒ Object

    When this operation is called without #with, settings will be an instance of this callable constructor.

    Parameters:

    • sym_or_proc (Symbol, #call)
      • Either a Symbol representing the public method to call on the settings class.

      • Or anything that responds to #call (like a Proc).

  • #default_settings!(arg1, arg2, ...) ⇒ Object

    When this operation is called without #with, settings will be an instance of the settings class, initialized with those arguments.

    (Like calling MyOperation.with(arg1, arg2, …))



218
219
220
221
222
223
224
225
226
# File 'lib/teckel/operation/config.rb', line 218

def default_settings!(*args)
  callable = if args.size.equal?(1)
    build_constructor(settings, args.first)
  end

  callable ||= -> { settings_constructor.call(*args) }

  @config.for(:default_settings, callable)
end

#dupself

Produces a shallow copy of this operation and all it’s configuration.

Returns:

  • (self)


336
337
338
# File 'lib/teckel/operation/config.rb', line 336

def dup
  dup_config(super())
end

#errorClass #error(klass) ⇒ Class?

Overloads:

  • #errorClass

    Get the configured class wrapping the error data structure.

    Returns:

    • (Class)

      The error class

  • #error(klass) ⇒ Class?

    Set the class wrapping the error data structure.

    Parameters:

    • klass (Class)

      The error class

    Returns:

    • (Class, nil)

      The error class or nil if it does not error



116
117
118
119
# File 'lib/teckel/operation/config.rb', line 116

def error(klass = nil)
  @config.for(:error, klass) { self::Error if const_defined?(:Error) } ||
    raise(MissingConfigError, "Missing error config for #{self}")
end

#error_constructorProc #error_constructor(sym_or_proc) ⇒ #call

Overloads:

  • #error_constructorProc

    The callable constructor to build an instance of the error class. Defaults to DEFAULT_CONSTRUCTOR

    Returns:

    • (Proc)

      A callable that will return an instance of error class.

  • #error_constructor(sym_or_proc) ⇒ #call

    Define how to build the error.

    Examples:

    class MyOperation
      include Teckel::Operation
    
      class Error
        def initialize(*args, **opts); end
      end
    
      # MyOperation.call("foo", "bar") # -> Error.new("foo", "bar")
      error_constructor :new
    
      # If you need more control over how to build a new +Error+ instance
      # MyOperation.call("foo", opt: "bar") # -> Error.new(name: "foo", opt: "bar")
      error_constructor ->(name, options) { Error.new(name: name, **options) }
    end

    Parameters:

    • sym_or_proc (Symbol, #call)
      • Either a Symbol representing the public method to call on the error class.

      • Or anything that response to #call (like a Proc).

    Returns:

    • (#call)

      The callable constructor



148
149
150
# File 'lib/teckel/operation/config.rb', line 148

def error_constructor(sym_or_proc = nil)
  get_set_constructor(:error_constructor, error, sym_or_proc)
end

#finalize!self

Disallow any further changes to this Operation. Make sure all configurations are set.

Returns:

  • (self)

    Frozen self

Raises:



326
327
328
329
330
# File 'lib/teckel/operation/config.rb', line 326

def finalize!
  define!
  @config.freeze
  self
end

#freezeself

Prevents further modifications to this operation and its config

Returns:

  • (self)


356
357
358
359
# File 'lib/teckel/operation/config.rb', line 356

def freeze
  @config.freeze
  super()
end

#inputClass #input(klass) ⇒ Class

Overloads:

  • #inputClass

    Get the configured class wrapping the input data structure.

    Returns:

    • (Class)

      The input class

  • #input(klass) ⇒ Class

    Set the class wrapping the input data structure.

    Parameters:

    • klass (Class)

      The input class

    Returns:

    • (Class)

      The input class



13
14
15
16
# File 'lib/teckel/operation/config.rb', line 13

def input(klass = nil)
  @config.for(:input, klass) { self::Input if const_defined?(:Input) } ||
    raise(MissingConfigError, "Missing input config for #{self}")
end

#input_constructorProc #input_constructor(sym_or_proc) ⇒ #call

Overloads:

  • #input_constructorProc

    The callable constructor to build an instance of the input class. Defaults to DEFAULT_CONSTRUCTOR

    Returns:

    • (Proc)

      A callable that will return an instance of the input class.

  • #input_constructor(sym_or_proc) ⇒ #call

    Define how to build the input.

    Examples:

    simple symbol to method constructor

    class MyOperation
      include Teckel::Operation
    
      class Input
        def initialize(name:, age:); end
      end
    
      # If you need more control over how to build a new +Input+ instance
      # MyOperation.call(name: "Bob", age: 23) # -> Input.new(name: "Bob", age: 23)
      input_constructor :new
    end
    
    MyOperation.input_constructor.is_a?(Method) #=> true

    Custom Proc constructor

    class MyOperation
      include Teckel::Operation
    
      class Input
        def initialize(*args, **opts); end
      end
    
      # If you need more control over how to build a new +Input+ instance
      # MyOperation.call("foo", opt: "bar") # -> Input.new(name: "foo", opt: "bar")
      input_constructor ->(name, options) { Input.new(name: name, **options) }
    end
    
    MyOperation.input_constructor.is_a?(Proc) #=> true

    Parameters:

    • sym_or_proc (Symbol, #call)
      • Either a Symbol representing the public method to call on the input class.

      • Or anything that response to #call (like a Proc).

    Returns:

    • (#call)

      The callable constructor



59
60
61
# File 'lib/teckel/operation/config.rb', line 59

def input_constructor(sym_or_proc = nil)
  get_set_constructor(:input_constructor, input, sym_or_proc)
end

#outputClass #output(klass) ⇒ Class

Overloads:

  • #outputClass

    Get the configured class wrapping the output data structure. Defaults to DEFAULT_CONSTRUCTOR

    Returns:

    • (Class)

      The output class

  • #output(klass) ⇒ Class

    Set the class wrapping the output data structure.

    Parameters:

    • klass (Class)

      The output class

    Returns:

    • (Class)

      The output class



72
73
74
75
# File 'lib/teckel/operation/config.rb', line 72

def output(klass = nil)
  @config.for(:output, klass) { self::Output if const_defined?(:Output) } ||
    raise(MissingConfigError, "Missing output config for #{self}")
end

#output_constructorProc #output_constructor(sym_or_proc) ⇒ #call

Overloads:

  • #output_constructorProc

    The callable constructor to build an instance of the output class. Defaults to DEFAULT_CONSTRUCTOR

    Returns:

    • (Proc)

      A callable that will return an instance of output class.

  • #output_constructor(sym_or_proc) ⇒ #call

    Define how to build the output.

    Examples:

    class MyOperation
      include Teckel::Operation
    
      class Output
        def initialize(*args, **opts); end
      end
    
      # MyOperation.call("foo", "bar") # -> Output.new("foo", "bar")
      output_constructor :new
    
      # If you need more control over how to build a new +Output+ instance
      # MyOperation.call("foo", opt: "bar") # -> Output.new(name: "foo", opt: "bar")
      output_constructor ->(name, options) { Output.new(name: name, **options) }
    end

    Parameters:

    • sym_or_proc (Symbol, #call)
      • Either a Symbol representing the public method to call on the output class.

      • Or anything that response to #call (like a Proc).

    Returns:

    • (#call)

      The callable constructor



104
105
106
# File 'lib/teckel/operation/config.rb', line 104

def output_constructor(sym_or_proc = nil)
  get_set_constructor(:output_constructor, output, sym_or_proc)
end

#resultClass #result(klass) ⇒ Class

Overloads:

  • #resultClass

    Get the configured result object class wrapping #error or #output. The ValueResult default will act as a pass-through and does. Any error or output will just returned as-is.

    Returns:

    • (Class)

      The result class, or ValueResult as default

  • #result(klass) ⇒ Class

    Set the result object class wrapping #error or #output.

    Parameters:

    • klass (Class)

      The result class

    Returns:

    • (Class)

      The result class configured



256
257
258
# File 'lib/teckel/operation/config.rb', line 256

def result(klass = nil)
  @config.for(:result, klass) { const_defined?(:Result, false) ? self::Result : ValueResult }
end

#result!nil (protected)

Note:

Don’t use in conjunction with #result or #result_constructor

Shortcut to use Result as a result object, wrapping any #error or #output.

Returns:

  • (nil)


297
298
299
300
301
# File 'lib/teckel/operation/config.rb', line 297

def result!
  @config.for(:result, Result)
  @config.for(:result_constructor, Result.method(:new))
  nil
end

#result_constructorProc #result_constructor(sym_or_proc) ⇒ #call

Overloads:

  • #result_constructorProc

    The callable constructor to build an instance of the result class. Defaults to DEFAULT_CONSTRUCTOR

    Returns:

    • (Proc)

      A callable that will return an instance of result class.

  • #result_constructor(sym_or_proc) ⇒ #call

    Define how to build the result.

    Examples:

    class MyOperation
      include Teckel::Operation
    
      class Result
        include Teckel::Result
        def initialize(value, success, opts = {}); end
      end
    
      # If you need more control over how to build a new +Result+ instance
      result_constructor ->(value, success) { result.new(value, success, {foo: :bar}) }
    end

    Parameters:

    • sym_or_proc (Symbol, #call)
      • Either a Symbol representing the public method to call on the result class.

      • Or anything that response to #call (like a Proc).

    Returns:

    • (#call)

      The callable constructor



284
285
286
287
# File 'lib/teckel/operation/config.rb', line 284

def result_constructor(sym_or_proc = nil)
  get_set_constructor(:result_constructor, result, sym_or_proc) ||
    raise(MissingConfigError, "Missing result_constructor config for #{self}")
end

#runnerClass #runner(klass) ⇒ Object

Overloads:

  • #runnerClass

    Returns The Runner class.

    Returns:

    • (Class)

      The Runner class

  • #runner(klass) ⇒ Object

    Overwrite the default runner

    Parameters:

    • klass (Class)

      A class like the Runner



242
243
244
# File 'lib/teckel/operation/config.rb', line 242

def runner(klass = nil)
  @config.for(:runner, klass) { Runner }
end

#settingsClass #settings(klass) ⇒ Class

Overloads:

  • #settingsClass

    Get the configured class wrapping the settings data structure.

    Returns:

  • #settings(klass) ⇒ Class

    Set the class wrapping the settings data structure.

    Parameters:

    • klass (Class)

      The settings class

    Returns:

    • (Class)

      The settings class configured



162
163
164
# File 'lib/teckel/operation/config.rb', line 162

def settings(klass = nil)
  @config.for(:settings, klass) { const_defined?(:Settings) ? self::Settings : none }
end

#settings_constructorProc #settings_constructor(sym_or_proc) ⇒ #call

Overloads:

  • #settings_constructorProc

    The callable constructor to build an instance of the settings class. Defaults to DEFAULT_CONSTRUCTOR

    Returns:

    • (Proc)

      A callable that will return an instance of settings class.

  • #settings_constructor(sym_or_proc) ⇒ #call

    Define how to build the settings.

    Examples:

    class MyOperation
      include Teckel::Operation
    
      class Settings
        def initialize(*args); end
      end
    
      # MyOperation.with("foo", "bar") # -> Settings.new("foo", "bar")
      settings_constructor :new
    end

    Parameters:

    • sym_or_proc (Symbol, #call)
      • Either a Symbol representing the public method to call on the settings class.

      • Or anything that response to #call (like a Proc).

    Returns:

    • (#call)

      The callable constructor



189
190
191
192
# File 'lib/teckel/operation/config.rb', line 189

def settings_constructor(sym_or_proc = nil)
  get_set_constructor(:settings_constructor, settings, sym_or_proc) ||
    raise(MissingConfigError, "Missing settings_constructor config for #{self}")
end