Sha256: 0b31bce377508ccede7915a3a26c494795d90b087e45df01a604875841584789

Contents?: true

Size: 1.17 KB

Versions: 7

Compression:

Stored size: 1.17 KB

Contents

module Vedeu
  class InvalidCommand < StandardError; end

  class Command
    class << self
      def define(name, klass, args = [], options = {})
        new(name, klass, args, options).define
      end
    end

    def initialize(name, klass, args = [], options = {})
      @name, @klass, @args, @options = name, klass, args, options
    end

    def define
      { name => executable } if valid?
    end

    private

    attr_reader :name, :klass, :args, :options

    def executable
      Proc.new { klass.dispatch(*args) }
    end

    def valid?
      empty_name? || empty_klass? || klass_defined? || dispatch_defined? || true
    end

    def empty_name?
      raise InvalidCommand, "Command name is missing." if name.to_s.empty?
    end

    def empty_klass?
      raise InvalidCommand, "Command class is missing." if klass.to_s.empty?
    end

    def klass_defined?
      unless Object.const_defined?(klass.to_s)
        raise InvalidCommand, "Command class is not defined."
      end
    end

    def dispatch_defined?
      unless klass.singleton_methods(false).include?(:dispatch)
        raise InvalidCommand, "Command dispatch method is not defined."
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
vedeu-0.0.10 lib/vedeu/process/command.rb
vedeu-0.0.9 lib/vedeu/process/command.rb
vedeu-0.0.8 lib/vedeu/process/command.rb
vedeu-0.0.7 lib/vedeu/process/command.rb
vedeu-0.0.6 lib/vedeu/process/command.rb
vedeu-0.0.5 lib/vedeu/process/command.rb
vedeu-0.0.4 lib/vedeu/process/command.rb