Sha256: 0fc51604a4c0aa2abab5e279c110435e096a23ca3204c5799c02a229ed5171ea

Contents?: true

Size: 789 Bytes

Versions: 1

Compression:

Stored size: 789 Bytes

Contents

module SpinningWheel
  class Fabric

    attr_reader :name, :class_name

    def initialize(name:, class_name:, &block)
      @name = name
      @class_name = class_name
      @attributes = {}
      @block = block
    end

    def build
      @klass = @class_name.constantize
      instance_eval(&@block)
      parameters = @klass.instance_method(:initialize).parameters
      required_parametes = []
      parameters.each do |parameter|
        if parameter[0] == :req
          required_parametes << parameter[1]
        end
      end
      if required_parametes == @attributes.keys
        @klass.new(*@attributes.values)
      else
        @klass.new(**@attributes)
      end
    end

    def method_missing(name, *args, &block)
      @attributes[name] = block.call
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
spinning_wheel-0.1.0 lib/spinning_wheel/fabric.rb