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-2.3.16 lib/albacore/cli_dsl.rb
albacore-2.3.15 lib/albacore/cli_dsl.rb
albacore-2.3.14 lib/albacore/cli_dsl.rb
albacore-2.3.13 lib/albacore/cli_dsl.rb
albacore-2.3.12 lib/albacore/cli_dsl.rb
albacore-2.3.11 lib/albacore/cli_dsl.rb
albacore-2.3.10 lib/albacore/cli_dsl.rb
albacore-2.3.9 lib/albacore/cli_dsl.rb
albacore-2.3.8 lib/albacore/cli_dsl.rb
albacore-2.3.7 lib/albacore/cli_dsl.rb
albacore-2.3.6 lib/albacore/cli_dsl.rb
albacore-2.3.5 lib/albacore/cli_dsl.rb
albacore-2.3.4 lib/albacore/cli_dsl.rb
albacore-2.3.3 lib/albacore/cli_dsl.rb
albacore-2.3.2 lib/albacore/cli_dsl.rb
albacore-2.3.1 lib/albacore/cli_dsl.rb
albacore-2.3.0 lib/albacore/cli_dsl.rb
albacore-2.2.1 lib/albacore/cli_dsl.rb
albacore-2.2.0 lib/albacore/cli_dsl.rb
albacore-2.2.0.pre.beta.7 lib/albacore/cli_dsl.rb