Sha256: d90845b084a4953aef30f65e43628c5d8d6969e9442b14e8770c79334bd671b8

Contents?: true

Size: 1.96 KB

Versions: 12

Compression:

Stored size: 1.96 KB

Contents

# -*- encoding: UTF-8 -*-
require 'ostruct'
require 'csd/extensions/core/string'

module CSD
  
  # A convenience wrapper to get information about the available applications
  #
  class Applications
    
    # Returns the application module instance of +app_name+. Returns +nil+ if the application could not be found or loaded.
    #
    def self.find(app_name)
      return nil if app_name.to_s.empty?
      begin
        UI.debug "#{self}.find got a request to see whether `#{app_name}´ is a valid application or not"
        require File.join(Path.applications, app_name.to_s)
        UI.debug "#{self}.find tries to initialize the loaded application `#{app_name}´ now"
        "CSD::Application::#{app_name.to_s.camelize}".constantize
      rescue LoadError => e
        UI.debug "#{self}.find could not load `#{app_name}´ in #{e.to_s.gsub('no such file to load -- ', '').enquote}"
        nil
      end
    end
    
    # This method returns instantiated modules of all valid applications in an +Array+ or in a block.
    #
    def self.all(&block)
      result = []
      Dir.directories(Path.applications).sort.each do |dir|
        next if dir == 'default'
        if app = find(dir)
          block_given? ? yield(app) : result << app
        end
      end
      result
    end

    # This method holds the desired application and initializes its module into +@@current+.
    #
    def self.current
      # In testmode we don't want to perform caching
      return choose_current if Options.testmode
      # Otherwise we choose and cache the current application module here
      @@current ||= choose_current
    end
    
    protected
    
    # This method identifies the desired application. No caching takes place here.
    # It is meant to be very robust, we expect the application to be any one of the first three arguments.
    #
    def self.choose_current
      Applications.find(ARGV.first) || Applications.find(ARGV.second) || Applications.find(ARGV.third)
    end
    
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
csd-0.4.3 lib/csd/applications.rb
csd-0.4.2 lib/csd/applications.rb
csd-0.4.1 lib/csd/applications.rb
csd-0.4.0 lib/csd/applications.rb
csd-0.3.7 lib/csd/applications.rb
csd-0.3.6 lib/csd/applications.rb
csd-0.3.5 lib/csd/applications.rb
csd-0.3.4 lib/csd/applications.rb
csd-0.3.3 lib/csd/applications.rb
csd-0.3.2 lib/csd/applications.rb
csd-0.3.1 lib/csd/applications.rb
csd-0.3.0 lib/csd/applications.rb