Sha256: e742240a72ee819c53149306a1ea75cfbe749b0c99ae33f0178c3b3510f24fce

Contents?: true

Size: 1.7 KB

Versions: 2

Compression:

Stored size: 1.7 KB

Contents

# frozen_string_literal: true

require 'transproc/transformer/class_interface'
require 'transproc/transformer/deprecated/class_interface'

module Transproc
  # Transfomer class for defining transprocs with a class DSL.
  #
  # @example
  #   require 'anima'
  #   require 'transproc/all'
  #
  #   class User
  #     include Anima.new(:name, :address)
  #   end
  #
  #   class Address
  #     include Anima.new(:city, :street, :zipcode)
  #   end
  #
  #   class UsersMapper < Transproc::Transformer
  #     map_array do
  #       symbolize_keys
  #       rename_keys user_name: :name
  #       nest :address, %i(city street zipcode)
  #       map_value :address do
  #         constructor_inject Address
  #       end
  #       constructor_inject User
  #     end
  #   end
  #
  #   UsersMapper.new.call(
  #     [
  #       { 'user_name' => 'Jane',
  #         'city' => 'NYC',
  #         'street' => 'Street 1',
  #         'zipcode' => '123'
  #       }
  #     ]
  #   )
  #   # => [
  #     #<User
  #       name="Jane"
  #       address=#<Address city="NYC" street="Street 1" zipcode="123">>
  #   ]
  #
  # @api public
  class Transformer
    extend ClassInterface
    extend Deprecated::ClassInterface

    attr_reader :transproc

    # Execute the transformation pipeline with the given input.
    #
    # @example
    #
    #   class SymbolizeKeys < Transproc::Transformer
    #     symbolize_keys
    #   end
    #
    #   SymbolizeKeys.new.call('name' => 'Jane')
    #   # => {:name=>"Jane"}
    #
    # @param [mixed] input The input to pass to the pipeline
    #
    # @return [mixed] output The output returned from the pipeline
    #
    # @api public
    def call(input)
      transproc.call(input)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
transproc-1.1.1 lib/transproc/transformer.rb
transproc-1.1.0 lib/transproc/transformer.rb