Sha256: 2f6143467b97b4257816f68c1f51d60bd426f05be4546ca9c7da0fbf788509f1

Contents?: true

Size: 737 Bytes

Versions: 1

Compression:

Stored size: 737 Bytes

Contents

module SmartInit
  @@_init_attributes = []

  def is_callable
    define_singleton_method :call do |*parameters|
      new(*parameters).call
    end
  end

  def initialize_with *attributes
    @_init_attributes = attributes
    define_method :initialize do |*parameters|
      if attributes.count != parameters.count
        raise ArgumentError, "wrong number of arguments (given #{parameters.count}, expected #{attributes.count})"
      end

      attributes.zip(parameters).each do |pair|
        name = pair[0]
        value = pair[1]
        instance_variable_set("@#{name}", value)
      end
    end

    instance_eval do
      private

      attr_reader *attributes
    end
  end
end

class SmartInit::Base
  extend SmartInit
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
smart_init-1.0.0 lib/smart_init/main.rb