Sha256: 1b95d006287f61f13d71c9e67392875c00949fdba5a2e8e30747931d740ba740

Contents?: true

Size: 1.21 KB

Versions: 68

Compression:

Stored size: 1.21 KB

Contents

module Albacore
  module CliDSL

    def self.included klass
      klass.extend ClassMethods
      klass.send :include, InstanceMethods
    end

    class CommandError < StandardError
    end

    module InstanceMethods
      # Calls an instance method defined via the ::command class method.
      # Raises CommandError if the command does not exist.
      def run_command(command)
        method_name = "#{self.class.command_prefix}#{command}"
        if self.class.method_defined?(method_name)
          send method_name
        else
          raise CommandError, "invalid command #{command}"
        end
      end
    end

    module ClassMethods
      # Defines an instance method based on the first command name.
      # The method executes the code of the given block.
      # Aliases methods for any subsequent command names.
      def command(*command_names, &block)
        method_name = "#{command_prefix}#{command_names.shift}"
        define_method method_name, &block
        command_names.each do |c|
          alias_method "#{command_prefix}#{c}", method_name
        end
      end

      # The prefix for any instance method defined by the ::command method.
      def command_prefix
        :_run_
      end
    end
  end
end

Version data entries

68 entries across 68 versions & 1 rubygems

Version Path
albacore-3.0.1 ./lib/albacore/cli_dsl.rb
albacore-3.0.0.pre.alpha.6 ./lib/albacore/cli_dsl.rb
albacore-3.0.0.pre.alpha.5 ./lib/albacore/cli_dsl.rb
albacore-3.0.0.pre.alpha.4 ./lib/albacore/cli_dsl.rb
albacore-3.0.0.pre.alpha.3 ./lib/albacore/cli_dsl.rb
albacore-3.0.0.pre.alpha.2 ./lib/albacore/cli_dsl.rb
albacore-3.0.0.pre.alpha ./lib/albacore/cli_dsl.rb
albacore-2.8.0 ./lib/albacore/cli_dsl.rb
albacore-2.7.0 ./lib/albacore/cli_dsl.rb
albacore-2.6.8 ./lib/albacore/cli_dsl.rb
albacore-2.6.7 ./lib/albacore/cli_dsl.rb
albacore-2.6.6 ./lib/albacore/cli_dsl.rb
albacore-2.6.4 ./lib/albacore/cli_dsl.rb
albacore-2.6.3 ./lib/albacore/cli_dsl.rb
albacore-2.6.2 ./lib/albacore/cli_dsl.rb
albacore-2.6.1 ./lib/albacore/cli_dsl.rb
albacore-2.6.0 ./lib/albacore/cli_dsl.rb
albacore-2.5.14 ./lib/albacore/cli_dsl.rb
albacore-2.5.13 ./lib/albacore/cli_dsl.rb
albacore-2.5.11 ./lib/albacore/cli_dsl.rb