Sha256: 93efce10515e54fa8291d1274daa9cd9c22526f099088547b43ef53ac04f10ea

Contents?: true

Size: 1.05 KB

Versions: 3

Compression:

Stored size: 1.05 KB

Contents

# frozen_string_literal: true

module Rake
  module Funnel
    module Support
      module InstantiateSymbol
        def self.included(klass)
          klass.extend(ClassMethods)
          klass.send(:instantiate, klass)
        end

        module ClassMethods
          attr_reader :module

          private

          def instantiate(mod)
            @module = mod
          end
        end

        private

        def create(sym, *args) # rubocop:disable Metrics/AbcSize
          return sym unless sym.is_a?(Symbol)

          found = [sym, sym.pascalize.to_sym]
                  .select { |candidate| mod.constants.include?(candidate) }
                  .first

          if found.nil?
            raise NameError,
                  "Unknown type to instantiate: #{sym.inspect}. Available types are: #{available.inspect}"
          end

          type = mod.const_get(found)
          type.new(*args)
        end

        def available
          mod.constants.sort
        end

        def mod
          self.class.module
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rake-funnel-0.24.0 lib/rake/funnel/support/internal/instantiate_symbol.rb
rake-funnel-0.23.0 lib/rake/funnel/support/internal/instantiate_symbol.rb
rake-funnel-0.22.3 lib/rake/funnel/support/internal/instantiate_symbol.rb