Sha256: 95ae98585628a6952066ec7fa00b2c135d16270bbe5fb11864d5f0256856002c

Contents?: true

Size: 1.02 KB

Versions: 10

Compression:

Stored size: 1.02 KB

Contents

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

10 entries across 10 versions & 1 rubygems

Version Path
rake-funnel-0.22.2 lib/rake/funnel/support/internal/instantiate_symbol.rb
rake-funnel-0.22.1 lib/rake/funnel/support/internal/instantiate_symbol.rb
rake-funnel-0.22.0 lib/rake/funnel/support/internal/instantiate_symbol.rb
rake-funnel-0.21.2 lib/rake/funnel/support/internal/instantiate_symbol.rb
rake-funnel-0.21.1 lib/rake/funnel/support/internal/instantiate_symbol.rb
rake-funnel-0.21.0 lib/rake/funnel/support/internal/instantiate_symbol.rb
rake-funnel-0.20.2 lib/rake/funnel/support/internal/instantiate_symbol.rb
rake-funnel-0.20.1 lib/rake/funnel/support/internal/instantiate_symbol.rb
rake-funnel-0.20.0 lib/rake/funnel/support/internal/instantiate_symbol.rb
rake-funnel-0.19.0 lib/rake/funnel/support/internal/instantiate_symbol.rb