Sha256: aaa1c3144c11b7f9559c751916584824aea26ef5e5349d5b0be21a17875a1e89

Contents?: true

Size: 1.15 KB

Versions: 1

Compression:

Stored size: 1.15 KB

Contents

module Transproc
  # Transformation functions for Classes
  #
  # @example
  #   require 'transproc/class'
  #
  #   include Transproc::Helper
  #
  #   fn = t(:constructor_inject, 'User', :name, :age)
  #
  #   fn[Struct]
  #   # => Struct::User
  #
  # @api public
  module ClassTransformations
    extend Functions

    # Inject given arguments into the constructor of the class
    #
    # @example
    #   Transproct(:constructor_inject, 'User', :name, :age)[Struct]
    #   # => Struct::User
    #
    # @param [Class]
    #
    # @return [Mixed]
    #
    # @api public
    def constructor_inject(*args, klass)
      klass.new(*args)
    end

    # Set instance variables from the hash argument (key/value pairs) on the object
    #
    # @example
    #   Transproc(:set_ivars, Object)[name: 'Jane', age: 25]
    #   # => #<Object:0x007f411d06a210 @name="Jane", @age=25>
    #
    # @param [Object]
    #
    # @return [Object]
    #
    # @api public
    def 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
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
transproc-0.2.1 lib/transproc/class.rb