Sha256: b0db17a65e8979ff2161a53d3fb0c3935ebcd3f8fe4f9c1efadefeeaf885a491

Contents?: true

Size: 1.15 KB

Versions: 10

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 = "iapi-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

10 entries across 10 versions & 1 rubygems

Version Path
iapi-idlc-sdk-pfm-1.0.13 lib/iapi-idlc-sdk-pfm/commands_map.rb
iapi-idlc-sdk-pfm-1.0.12 lib/iapi-idlc-sdk-pfm/commands_map.rb
iapi-idlc-sdk-pfm-1.0.11 lib/iapi-idlc-sdk-pfm/commands_map.rb
iapi-idlc-sdk-pfm-1.0.10 lib/iapi-idlc-sdk-pfm/commands_map.rb
iapi-idlc-sdk-pfm-1.0.9 lib/iapi-idlc-sdk-pfm/commands_map.rb
iapi-idlc-sdk-pfm-1.0.8 lib/iapi-idlc-sdk-pfm/commands_map.rb
iapi-idlc-sdk-pfm-1.0.7 lib/iapi-idlc-sdk-pfm/commands_map.rb
iapi-idlc-sdk-pfm-1.0.6 lib/iapi-idlc-sdk-pfm/commands_map.rb
iapi-idlc-sdk-pfm-1.0.5 lib/iapi-idlc-sdk-pfm/commands_map.rb
iapi-idlc-sdk-pfm-1.0.4 lib/iapi-idlc-sdk-pfm/commands_map.rb