Module: Dry::Transformer::ClassTransformations

Extended by:
Registry
Defined in:
lib/dry/transformer/class_transformations.rb

Overview

Transformation functions for Classes

Examples:

require 'dry/transformer/class'

include Dry::Transformer::Helper

fn = t(:constructor_inject, Struct)

fn['User', :name, :age]
# => Struct::User

Class Method Summary collapse

Methods included from Registry

[], contain?, fetch, import, register, store

Class Method Details

.constructor_inject(*args, klass) ⇒ Object

Inject given arguments into the constructor of the class

Examples:

Transproct(:constructor_inject, Struct)['User', :name, :age]
# => Struct::User

Parameters:

  • A (*Mixed)

    list of arguments to inject

Returns:

  • (Object)

    An instance of the given klass



32
33
34
# File 'lib/dry/transformer/class_transformations.rb', line 32

def self.constructor_inject(*args, klass)
  klass.new(*args)
end

.set_ivars(ivar_hash, klass) ⇒ Object

Set instance variables from the hash argument (key/value pairs) on the object

Examples:

Dry::Transformer(:set_ivars, Object)[name: 'Jane', age: 25]
# => #<Object:0x007f411d06a210 @name="Jane", @age=25>

Parameters:

  • (Object)

Returns:

  • (Object)


47
48
49
50
51
52
53
# File 'lib/dry/transformer/class_transformations.rb', line 47

def self.set_ivars(ivar_hash, klass)
  object = klass.allocate
  ivar_hash.each do |ivar_name, ivar_value|
    object.instance_variable_set("@#{ivar_name}", ivar_value)
  end
  object
end