Sha256: 931ac17818e723b2e409b5eb78574d9a35029fa98f6ca99b69fa027a64f63636

Contents?: true

Size: 1.73 KB

Versions: 1

Compression:

Stored size: 1.73 KB

Contents

module Rad
  module FilesHelper    
    def directories
      $LOAD_PATH
    end
    
    def file_exist? path, directories = nil
      find_files(path).size > 0
    end

    def find_files fname, directories = nil
      directories ||= self.directories
      FilesHelper.find_files fname, directories
    end
    
    def find_files_without_cache fname, directories = nil
      # directories ||= self.directories
      FilesHelper.find_files_without_cache fname, directories
    end
    
    def find_file fname, directories = nil
      files = find_files(fname, directories)
      raise "File '#{fname}' not found!" if files.size == 0
      raise "Found multiple files for '#{fname}'" if files.size > 1
      files.first
    end
    
    def find_files_by_pattern_without_cache pattern, directories = nil
      directories ||= self.directories
      patterns = directories.to_a.collect{|d| "#{d}#{pattern}"}
      Dir.glob patterns
    end
    alias_method :find_files_by_pattern, :find_files_by_pattern_without_cache
    
    def find_file_by_pattern pattern
      files = find_files_by_pattern(pattern)
      raise "File '#{pattern}' not found!" if files.size == 0
      raise "Found multiple files for '#{pattern}'" if files.size > 1
      files.first
    end
    
    class << self
      # don't move this class method to module, because there will be then 
      # multiple cache for every object that includes it.      
      def find_files fname, directories = nil
        fname.must =~ /\//
        directories.must_be.present # || raise('directories ')= self.directories
        files = directories.collect{|dir| "#{dir}#{fname}"}
        files.select{|f| File.exist? f}
      end
      cache_method_with_params_in_production :find_files
    end
  end
end
  

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rad_core-0.0.13 lib/rad/environment/files_helper.rb