Module: Teckel::Operation::ClassMethods

Defined in:
lib/teckel/operation.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#error()Class (readonly)

Get the configured class wrapping the error data structure.

Returns:

  • (Class)

    The error class



# File 'lib/teckel/operation.rb', line 241


#error_constructor()Class (readonly)

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

Returns:

  • (Class)

    The Error class



# File 'lib/teckel/operation.rb', line 254


#input()Class (readonly)

Get the configured class wrapping the input data structure.

Returns:

  • (Class)

    The input class



# File 'lib/teckel/operation.rb', line 139


#input_constructor()Class (readonly)

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

Returns:

  • (Class)

    The Input class



# File 'lib/teckel/operation.rb', line 152


#output()Class (readonly)

Get the configured class wrapping the output data structure.

Returns:

  • (Class)

    The output class



# File 'lib/teckel/operation.rb', line 197


#output_constructor()Class (readonly)

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

Returns:

  • (Class)

    The Output class



# File 'lib/teckel/operation.rb', line 210


#runner()Class (readonly, protected)

Returns The Runner class.

Returns:

  • (Class)

    The Runner class



# File 'lib/teckel/operation.rb', line 315


Instance Method Details

#call(input = nil) ⇒ Object

Invoke the Operation

Parameters:

  • input (defaults to: nil)

    Any form of input your input class can handle via the given input_constructor

Returns:

  • Either An instance of your defined error class or output class



331
332
333
# File 'lib/teckel/operation.rb', line 331

def call(input = nil)
  runner.new(self).call(input)
end

#cloneObject



365
366
367
368
369
370
371
372
373
# File 'lib/teckel/operation.rb', line 365

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

#dupObject



358
359
360
361
362
# File 'lib/teckel/operation.rb', line 358

def dup
  super.tap do |copy|
    copy.instance_variable_set(:@config, @config.dup)
  end
end

#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



249
250
251
252
# File 'lib/teckel/operation.rb', line 249

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

#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



280
281
282
283
# File 'lib/teckel/operation.rb', line 280

def error_constructor(sym_or_proc = Config.default_constructor)
  @config.for(:error_constructor) { build_counstructor(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:



350
351
352
353
354
355
# File 'lib/teckel/operation.rb', line 350

def finalize!
  define!
  freeze
  @config.freeze
  self
end

#input(klass) ⇒ Class

Set the class wrapping the input data structure.

Parameters:

  • klass (Class)

    The input class

Returns:

  • (Class)

    The input class



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

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

#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



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

def input_constructor(sym_or_proc = Config.default_constructor)
  @config.for(:input_constructor) { build_counstructor(input, sym_or_proc) } ||
    raise(MissingConfigError, "Missing input_constructor config for #{self}")
end

#noneNone

Convenience method for setting #input, #output or #error to the None value.

Examples:

Enforcing nil input, output or error

class MyOperation
  include Teckel::Operation

  input none

  # same as
  output Teckel::None

  error none

  def call(_) # you still need to take than +nil+ input when using `input none`
    # when using `error none`:
    # `fail!` works, but `fail!("data")` raises an error

    # when using `output none`:
    # `success!` works, but `success!("data")` raises an error
    # same thing when using simple return values as success:
    # take care to not return anything
    nil
  end
end

MyOperation.call #=> nil

Returns:



311
312
313
# File 'lib/teckel/operation.rb', line 311

def none
  None
end

#output(klass) ⇒ Class

Set the class wrapping the output data structure.

Parameters:

  • klass (Class)

    The output class

Returns:

  • (Class)

    The output class



205
206
207
208
# File 'lib/teckel/operation.rb', line 205

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

#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



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

def output_constructor(sym_or_proc = Config.default_constructor)
  @config.for(:output_constructor) { build_counstructor(output, sym_or_proc) } ||
    raise(MissingConfigError, "Missing output_constructor config for #{self}")
end

#runner(klass = nil) ⇒ Object (protected)

Overwrite the default runner

Parameters:

  • klass (Class) (defaults to: nil)

    A class like the Runner



322
323
324
# File 'lib/teckel/operation.rb', line 322

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