Sha256: 930560bc0d415d91769b543942ed930f67a5d1d826cba3e39aedb7b731e83d1d

Contents?: true

Size: 1.15 KB

Versions: 47

Compression:

Stored size: 1.15 KB

Contents

module Pfm
  class CommandsMap
    NULL_ARG = Object.new

    CommandSpec = Struct.new(:name, :constant_name, :require_path, :description)

    class CommandSpec

      def instantiate
        require require_path
        command_class = Pfm::Command.const_get(constant_name)
        command_class.new
      end

    end

    attr_reader :command_specs

    def initialize
      @command_specs = {}
    end

    def builtin(name, constant_name, require_path: NULL_ARG, desc: "")
      if null?(require_path)
        snake_case_path = name.tr("-", "_")
        require_path = "idlc-sdk-pfm/command/#{snake_case_path}"
      end
      command_specs[name] = CommandSpec.new(name, constant_name, require_path, desc)
    end

    def instantiate(name)
      spec_for(name).instantiate
    end

    def have_command?(name)
      command_specs.key?(name)
    end

    def command_names
      command_specs.keys
    end

    def spec_for(name)
      command_specs[name]
    end

    private

    def null?(argument)
      argument.equal?(NULL_ARG)
    end
  end

  def self.commands_map
    @commands_map ||= CommandsMap.new
  end

  def self.commands
    yield commands_map
  end
end

Version data entries

47 entries across 47 versions & 1 rubygems

Version Path
idlc-sdk-pfm-1.0.8 lib/idlc-sdk-pfm/commands_map.rb
idlc-sdk-pfm-1.0.7 lib/idlc-sdk-pfm/commands_map.rb
idlc-sdk-pfm-1.0.6 lib/idlc-sdk-pfm/commands_map.rb
idlc-sdk-pfm-1.0.5 lib/idlc-sdk-pfm/commands_map.rb
idlc-sdk-pfm-1.0.4 lib/idlc-sdk-pfm/commands_map.rb
idlc-sdk-pfm-1.0.3 lib/idlc-sdk-pfm/commands_map.rb
idlc-sdk-pfm-1.0.2 lib/idlc-sdk-pfm/commands_map.rb
idlc-sdk-pfm-1.0.1 lib/idlc-sdk-pfm/commands_map.rb
idlc-sdk-pfm-1.0.0.rc18 lib/idlc-sdk-pfm/commands_map.rb
idlc-sdk-pfm-1.0.0.rc17 lib/idlc-sdk-pfm/commands_map.rb
idlc-sdk-pfm-1.0.0.rc16 lib/idlc-sdk-pfm/commands_map.rb
idlc-sdk-pfm-1.0.0.rc15 lib/idlc-sdk-pfm/commands_map.rb
idlc-sdk-pfm-1.0.0.rc14 lib/idlc-sdk-pfm/commands_map.rb
idlc-sdk-pfm-1.0.0.rc13 lib/idlc-sdk-pfm/commands_map.rb
idlc-sdk-pfm-1.0.0.rc12 lib/idlc-sdk-pfm/commands_map.rb
idlc-sdk-pfm-1.0.0.rc11 lib/idlc-sdk-pfm/commands_map.rb
idlc-sdk-pfm-1.0.0.rc10 lib/idlc-sdk-pfm/commands_map.rb
idlc-sdk-pfm-1.0.0.rc9 lib/idlc-sdk-pfm/commands_map.rb
idlc-sdk-pfm-1.0.0.rc8 lib/idlc-sdk-pfm/commands_map.rb
idlc-sdk-pfm-1.0.0.rc7 lib/idlc-sdk-pfm/commands_map.rb