Sha256: 5750b67dcf4bbb874b05bde98107a2063731a08ae21c8e4fd3eda3c814cd3750

Contents?: true

Size: 1.15 KB

Versions: 2

Compression:

Stored size: 1.15 KB

Contents

require 'pathname'

module Airake #:nodoc:
  
  module Commands #:nodoc:
    
    # Base command 
    class Base 
      
      # Process command options array
      def process(command)
        command.compact.join(" ")
      end
      
      # Escape any spacing
      def escape(command)
        command.to_s.gsub(" ", "\\ ")
      end
      
      # Get relative path
      def relative_path(path, from)
        Pathname.new(path).relative_path_from(Pathname.new(from))
      end      
      
      def with_options(options, defaults = {}) 
        options.each do |key, value|
          raise "Invalid option: '#{key}' for command: #{self.class}" unless respond_to?(key.to_sym)
          instance_variable_set("@#{key}", value)          
        end
        
        defaults.each do |key, value|
          existing = instance_variable_get("@#{key}")
          instance_variable_set("@#{key}", value) unless existing
        end
      end
      
      def assert_required(vars)        
        vars.each do |var|
          raise ArgumentError, "Must specify option: #{var}" if instance_variable_get("@#{var}").nil?
        end
      end
      
    end
    
  end
  
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
airake-0.2.10 lib/airake/commands/base.rb
airake-0.2.9 lib/airake/commands/base.rb