Sha256: 2f100792936aa81da95f4d2bbd7c98ffa9bdd2bb4f8232c290a95723118fb95c

Contents?: true

Size: 2 KB

Versions: 20

Compression:

Stored size: 2 KB

Contents

require "securerandom"

Foobara.require_project_file("value", "processor/runner")

module Foobara
  module Value
    class Transformer < Processor
      class Runner < Processor::Runner
        runner_methods :transform
      end

      class << self
        def foobara_manifest(to_include:)
          super.merge(processor_type: :transformer)
        end

        def error_classes
          []
        end

        def create(options)
          subclass(**options).instance
        end

        # TODO: make transform the first argument for convenience
        def subclass(name: nil, **options)
          arity_zero = %i[always_applicable? priority]
          arity_one = %i[applicable? transform]
          allowed = arity_zero + arity_one

          invalid_options = options.keys - allowed

          unless invalid_options.empty?
            # :nocov:
            raise ArgumentError, "Invalid options #{invalid_options} expected only #{allowed}"
            # :nocov:
          end

          name ||= "#{self.name}::Anon#{SecureRandom.hex}"

          unless name.include?(":")
            name = "#{self.name}::#{name}"
          end

          Util.make_class(name, self) do
            arity_one.each do |method_name|
              if options.key?(method_name)
                method = options[method_name]

                define_method method_name do |value|
                  method.call(value)
                end
              end
            end

            arity_zero.each do |method_name|
              if options.key?(method_name)
                value = options[method_name]

                define_method method_name do
                  value
                end
              end
            end
          end
        end
      end

      def transform(_value)
        # :nocov:
        raise "subclass responsibility"
        # :nocov:
      end

      def process_value(value)
        if applicable?(value)
          value = transform(value)
        end

        Outcome.success(value)
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
foobara-0.0.20 projects/value/src/transformer.rb
foobara-0.0.19 projects/value/src/transformer.rb
foobara-0.0.18 projects/value/src/transformer.rb
foobara-0.0.17 projects/value/src/transformer.rb
foobara-0.0.16 projects/value/src/transformer.rb
foobara-0.0.15 projects/value/src/transformer.rb
foobara-0.0.14 projects/value/src/transformer.rb
foobara-0.0.13 projects/value/src/transformer.rb
foobara-0.0.12 projects/value/src/transformer.rb
foobara-0.0.11 projects/value/src/transformer.rb
foobara-0.0.10 projects/value/src/transformer.rb
foobara-0.0.9 projects/value/src/transformer.rb
foobara-0.0.8 projects/value/src/transformer.rb
foobara-0.0.7 projects/value/src/transformer.rb
foobara-0.0.6 projects/value/src/transformer.rb
foobara-0.0.5 projects/value/src/transformer.rb
foobara-0.0.4 projects/value/src/transformer.rb
foobara-0.0.3 projects/value/src/transformer.rb
foobara-0.0.2 projects/value/src/transformer.rb
foobara-0.0.1 projects/value/src/transformer.rb