Class: YFantasy::Transformations::BaseTransform Abstract

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/y_fantasy/transformations/base_transform.rb

Overview

This class is abstract.

Subclasses must set @function instance variable

Base class for all transformations in the YFantasy module.

Examples:

Creating a custom transformation

class MyTransform < BaseTransform
  def initialize(param)
    @function = ->(data) { transform_data(data, param) }
  end

  private

  def transform_data(data, param)
    # transformation logic here
  end
end

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ BaseTransform

Initializes a new transformation instance

Parameters:

  • args (Array)

    Arguments for initialization (used by subclasses)

Raises:

  • (RuntimeError)

    If @function is not properly set by the subclass



41
42
43
# File 'lib/y_fantasy/transformations/base_transform.rb', line 41

def initialize(*args)
  ensure_function_set
end

Instance Method Details

#>>(other) ⇒ Proc

Function composition - chains this transformation with another

Parameters:

  • other (#call)

    Another callable object to compose with

Returns:

  • (Proc)

    A new composed function



36
# File 'lib/y_fantasy/transformations/base_transform.rb', line 36

def_delegators :@function, :>>, :call, :[]

#[](*args) ⇒ Object

Alias for call

Parameters:

  • args

    Arguments to pass to the transformation

Returns:

  • (Object)

    The transformed result



36
# File 'lib/y_fantasy/transformations/base_transform.rb', line 36

def_delegators :@function, :>>, :call, :[]

#call(*args) ⇒ Object

Invokes the transformation function

Parameters:

  • args

    Arguments to pass to the transformation

Returns:

  • (Object)

    The transformed result



36
# File 'lib/y_fantasy/transformations/base_transform.rb', line 36

def_delegators :@function, :>>, :call, :[]

#ensure_function_setnil

Ensures that the transformation function is properly set

Returns:

  • (nil)

    if the function is properly set

Raises:

  • (RuntimeError)

    If @function is not callable or does not respond to the required methods



55
56
57
58
59
# File 'lib/y_fantasy/transformations/base_transform.rb', line 55

def ensure_function_set
  return if @function.respond_to?(:>>) && @function.respond_to?(:call)

  raise "Subclass must set @function instance variable"
end

#t(*args) ⇒ Object

Helper method to call transformations provided by Dry::Transformer (see T module)

Parameters:

  • args (Array)

    Arguments to pass to Dry::Transformer

Returns:

  • (Object)

    A new transformation object



48
49
50
# File 'lib/y_fantasy/transformations/base_transform.rb', line 48

def t(*args)
  T[*args]
end