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 212

#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 228

#input()Class (readonly)

Get the configured class wrapping the input data structure.

Returns:

  • (Class)

    The input class



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

#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 106

#output()Class (readonly)

Get the configured class wrapping the output data structure.

Returns:

  • (Class)

    The output class



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

#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 174

Instance Method Details

#call(input) ⇒ Object

Invoke the Operation

Parameters:

  • input

    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



270
271
272
# File 'lib/teckel/operation.rb', line 270

def call(input)
  new.call!(input)
end

#error(klass) ⇒ Class

Set the class wrapping the error data structure.

Parameters:

  • klass (Class)

    The error class

Returns:

  • (Class)

    The error class



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

def error(klass = nil)
  return @error_class if @error_class

  @error_class = @config.error(klass)
  @error_class ||= self::Error if const_defined?(:Error)
  @error_class
end

#error_constructor(sym_or_proc) ⇒ #call

Define how to build the error.

Examples:

class MyOperation
  include Teckel::Operation

  class Error
    # ....
  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 a callable (like a Proc).

Returns:

  • (#call)

    The callable constructor



254
255
256
257
258
259
260
261
262
263
264
# File 'lib/teckel/operation.rb', line 254

def error_constructor(sym_or_proc = nil)
  return @error_constructor if @error_constructor

  constructor = @config.error_constructor(sym_or_proc)
  @error_constructor =
    if constructor.is_a?(Symbol) && error.respond_to?(constructor)
      error.public_method(constructor)
    elsif sym_or_proc.respond_to?(:call)
      sym_or_proc
    end
end

#input(klass) ⇒ Class

Set the class wrapping the input data structure.

Parameters:

  • klass (Class)

    The input class

Returns:

  • (Class)

    The input class



98
99
100
101
102
103
104
# File 'lib/teckel/operation.rb', line 98

def input(klass = nil)
  return @input_class if @input_class

  @input_class = @config.input(klass)
  @input_class ||= self::Input if const_defined?(:Input)
  @input_class
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
    # ...
  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
    # ...
  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 a callable (like a Proc).

Returns:

  • (#call)

    The callable constructor



146
147
148
149
150
151
152
153
154
155
156
# File 'lib/teckel/operation.rb', line 146

def input_constructor(sym_or_proc = nil)
  return @input_constructor if @input_constructor

  constructor = @config.input_constructor(sym_or_proc)
  @input_constructor =
    if constructor.is_a?(Symbol) && input.respond_to?(constructor)
      input.public_method(constructor)
    elsif sym_or_proc.respond_to?(:call)
      sym_or_proc
    end
end

#output(klass) ⇒ Class

Set the class wrapping the output data structure.

Parameters:

  • klass (Class)

    The output class

Returns:

  • (Class)

    The output class



166
167
168
169
170
171
172
# File 'lib/teckel/operation.rb', line 166

def output(klass = nil)
  return @output_class if @output_class

  @output_class = @config.output(klass)
  @output_class ||= self::Output if const_defined?(:Output)
  @output_class
end

#output_constructor(sym_or_proc) ⇒ #call

Define how to build the output.

Examples:

class MyOperation
  include Teckel::Operation

  class Output
    # ....
  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 a callable (like a Proc).

Returns:

  • (#call)

    The callable constructor



200
201
202
203
204
205
206
207
208
209
210
# File 'lib/teckel/operation.rb', line 200

def output_constructor(sym_or_proc = nil)
  return @output_constructor if @output_constructor

  constructor = @config.output_constructor(sym_or_proc)
  @output_constructor =
    if constructor.is_a?(Symbol) && output.respond_to?(constructor)
      output.public_method(constructor)
    elsif sym_or_proc.respond_to?(:call)
      sym_or_proc
    end
end