Module: Confinement::BuilderGetterInitialization

Included in:
Confinement, Config
Defined in:
lib/confinement.rb

Instance Method Summary collapse

Instance Method Details

#builder_getter(method_name, klass, ivar, new: []) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/confinement.rb', line 80

def builder_getter(method_name, klass, ivar, new: [])
  init_parameters = [*new, "&block"].join(", ")

  class_eval(<<~RUBY, __FILE__, __LINE__)
    def #{method_name}(&block)
      if #{ivar}
        if block_given?
          raise "#{method_name} is already set up"
        end

        return #{ivar}
      end

      if !block_given?
        raise "Can't initialize #{method_name} without block"
      end

      #{ivar} = #{klass}.new(#{init_parameters})
      #{ivar}
    end
  RUBY
end