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)


352
353
354
355
356
357
358
359
360
# File 'lib/teckel/operation/config.rb', line 352

def clone
  if frozen?
    super
  else
    super.tap do |copy|
      copy.instance_variable_set(:@config, @config.dup)
    end
  end
end

#default_settingsnil|#call

Getter for configured default settings

Returns:

  • (nil|#call)

    The callable constructor



236
237
238
# File 'lib/teckel/operation/config.rb', line 236

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, …))



221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/teckel/operation/config.rb', line 221

def default_settings!(*args)
  callable =
    if args.empty?
      -> { settings_constructor.call }
    elsif args.length == 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)


342
343
344
345
346
# File 'lib/teckel/operation/config.rb', line 342

def dup
  super.tap do |copy|
    copy.instance_variable_set(:@config, @config.dup)
  end
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



118
119
120
121
# File 'lib/teckel/operation/config.rb', line 118

def error(klass = nil)
  @config.for(:error, klass) { self::Error if const_defined?(:Error) } ||
    raise(Teckel::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



150
151
152
153
# File 'lib/teckel/operation/config.rb', line 150

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

#finalize!self

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

Returns:

  • (self)

    Frozen self

Raises:



332
333
334
335
336
# File 'lib/teckel/operation/config.rb', line 332

def finalize!
  define!
  @config.freeze
  self
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(Teckel::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
62
# File 'lib/teckel/operation/config.rb', line 59

def input_constructor(sym_or_proc = nil)
  get_set_constructor(:input_constructor, input, sym_or_proc) ||
    raise(MissingConfigError, "Missing input_constructor config for #{self}")
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



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

def output(klass = nil)
  @config.for(:output, klass) { self::Output if const_defined?(:Output) } ||
    raise(Teckel::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



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

def output_constructor(sym_or_proc = nil)
  get_set_constructor(:output_constructor, output, sym_or_proc) ||
    raise(MissingConfigError, "Missing output_constructor config for #{self}")
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



262
263
264
# File 'lib/teckel/operation/config.rb', line 262

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)


303
304
305
306
307
# File 'lib/teckel/operation/config.rb', line 303

def result!
  @config.for(:result, Teckel::Operation::Result)
  @config.for(:result_constructor, Teckel::Operation::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



290
291
292
293
# File 'lib/teckel/operation/config.rb', line 290

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



248
249
250
# File 'lib/teckel/operation/config.rb', line 248

def runner(klass = nil)
  @config.for(:runner, klass) { Teckel::Operation::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



165
166
167
# File 'lib/teckel/operation/config.rb', line 165

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



192
193
194
195
# File 'lib/teckel/operation/config.rb', line 192

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