Sha256: e849c5f113c1bbc7e1cf4fc2401d34f04ad9e787f1074fe898297d258ead8375

Contents?: true

Size: 1.27 KB

Versions: 3

Compression:

Stored size: 1.27 KB

Contents

module Spinebox
  module Command
    
    @@commands = []
    
    # Registers a command to dispatch
    def self.on command, desc, options = {}
      @@commands << Command.new(command, desc, options, &Proc.new)
    end
    
    # Executes the matching commands
    def self.execute
      @@commands.each { |command| command.run! }
    end
    
    # Dispatches the command registering block and executes the matching commands
    def self.dispatch
      raise "Nothing to dispatch" unless block_given?
      self.module_exec(&Proc.new)
      execute
    end
    
    # The registered commands
    def self.commands
      @@commands
    end
    
    # Combines the information of all registered commands and concatenates them to a string
    def self.help
      output = "Usage: spinebox [options]\n\n"
      @@commands.each { |command| output += "\t#{command.command}\t\t#{command.desc}\n" }
      output
    end
    
    # Encapsules a registered command
    class Command
      
      attr_reader :command
      attr_reader :desc
      
      def initialize command, desc, options, &block
        @command, @desc, @options, @block = command, desc, options, block
      end
      
      def run!
        return false unless @options[:if].call
        @block.call
      end
      
    end
    
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
spinebox-0.0.3 lib/spinebox/command.rb
spinebox-0.0.2 lib/spinebox/command.rb
spinebox-0.0.1 lib/spinebox/command.rb