Sha256: b153615550dffe30fa6308caa2982d4831464070f177880860e852de5fcdcfe7

Contents?: true

Size: 1.47 KB

Versions: 4

Compression:

Stored size: 1.47 KB

Contents

module SpinningWheel
  class Fabric

    attr_reader :name, :class_name

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

    def build
      @klass = @class_name.constantize
      attributes = DSL.run(@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


    class DSL

      attr_accessor :attributes

      def self.run(block)
        dsl = self.new
        dsl.instance_eval(&block)
        return dsl.attributes
      end

      def initialize()
        @attributes = Hash.new
        clean_up_object_methods()
      end

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

      private

      def clean_up_object_methods
        self.instance_eval('undef :clone')
        self.instance_eval('undef :display')
        self.instance_eval('undef :dup')
        self.instance_eval('undef :extend')
        self.instance_eval('undef :freeze')
        self.instance_eval('undef :frozen?')
        self.instance_eval('undef :inspect')
        self.instance_eval('undef :taint')
        self.instance_eval('undef :tap')
      end
    end

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
spinning_wheel-1.0.0 lib/spinning_wheel/fabric.rb
spinning_wheel-0.2.2 lib/spinning_wheel/fabric.rb
spinning_wheel-0.2.1 lib/spinning_wheel/fabric.rb
spinning_wheel-0.2.0 lib/spinning_wheel/fabric.rb