Class: SpinningWheel::Fabric

Inherits:
Object
  • Object
show all
Defined in:
lib/spinning_wheel/fabric.rb

Defined Under Namespace

Classes: DSL

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, class_name:, &block) ⇒ Fabric

Returns a new instance of Fabric.



6
7
8
9
10
# File 'lib/spinning_wheel/fabric.rb', line 6

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

Instance Attribute Details

#class_nameObject (readonly)

Returns the value of attribute class_name.



4
5
6
# File 'lib/spinning_wheel/fabric.rb', line 4

def class_name
  @class_name
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/spinning_wheel/fabric.rb', line 4

def name
  @name
end

Instance Method Details

#buildObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/spinning_wheel/fabric.rb', line 12

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