Sha256: c60bc8d94b4c186831338a788bf2fc6feb6eb4d064098469afd172faee883987

Contents?: true

Size: 1.94 KB

Versions: 6

Compression:

Stored size: 1.94 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
      puts "Usage:"
      printf "%-3s %s\n\n", "", "spinebox new APP_PATH"
      
      puts "Options:"
      @@commands.each { |command| printf "%-3s %-20s %s\n", "", command.command, command.desc if command.options[:type] == :option }
      
      puts ""
      
      puts "Actions:"
      @@commands.each { |command| printf "%-3s %-20s %s\n", "", command.command, command.desc if command.options[:type] == :action }
      
      puts ""
      
      puts "Generators:"
      @@commands.each { |command| printf "%-3s %-40s %s\n", "", command.command, command.desc if command.options[:type] == :generator }
      
      puts ""
      
      puts "Scaffolds:"
      @@commands.each { |command| printf "%-3s %-40s %s\n", "", command.command, command.desc if command.options[:type] == :scaffold }
    end
    
    # Encapsules a registered command
    class Command
      
      attr_reader :command
      attr_reader :desc
      attr_reader :options
      
      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
        true
      end
      
    end
    
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
spinebox-0.0.15 lib/spinebox/command.rb
spinebox-0.0.14 lib/spinebox/command.rb
spinebox-0.0.13 lib/spinebox/command.rb
spinebox-0.0.12 lib/spinebox/command.rb
spinebox-0.0.11 lib/spinebox/command.rb
spinebox-0.0.10 lib/spinebox/command.rb