Class: ToPass::FileReader

Inherits:
Object
  • Object
show all
Defined in:
lib/to_pass/file_reader.rb

Overview

a generic Filereader, abstracting (among others) ToPass::AlgorithmReader and ToPass::ConverterReader.

Files are searched in a list of standard directories. Those are defined and managed in ToPass::Directories

Direct Known Subclasses

AlgorithmReader, ConfigReader, ConverterReader

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file = nil, dir_suffix = nil) ⇒ FileReader

:nodoc:



16
17
18
19
20
21
# File 'lib/to_pass/file_reader.rb', line 16

def initialize(file = nil, dir_suffix = nil) # :nodoc:
  @file = file
  @load_path = []

  @load_path.concat(standard_directories(dir_suffix))
end

Instance Attribute Details

#load_pathObject (readonly)

Returns the value of attribute load_path.



14
15
16
# File 'lib/to_pass/file_reader.rb', line 14

def load_path
  @load_path
end

Class Method Details

.discoverObject

searches for available algorithms



30
31
32
33
34
35
36
37
38
# File 'lib/to_pass/file_reader.rb', line 30

def discover
  extension = ".#{extension}" if extension

  new(nil).load_path.collect do |dir|
    Dir["#{dir}/#{search_pattern}#{extension}"]
  end.flatten.compact.map do |fn|
    File.basename(fn).gsub('#{extension}', '')
  end
end

.extensionObject



44
45
46
# File 'lib/to_pass/file_reader.rb', line 44

def extension
  nil
end

.load(fn) ⇒ Object

load a file with a given identifier



25
26
27
# File 'lib/to_pass/file_reader.rb', line 25

def load(fn)
  new(fn).load_from_file
end

.search_patternObject



40
41
42
# File 'lib/to_pass/file_reader.rb', line 40

def search_pattern
  '*'
end

Instance Method Details

#load_from_fileObject

:nodoc:

Raises:

  • (LoadError)


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/to_pass/file_reader.rb', line 49

def load_from_file # :nodoc:
  fn = load_path.map do |dir|
    extension = ".#{self.class.extension}" if self.class.extension
    file = Pathname.new("#{dir}/#{@file}#{extension}")

    if file.exist?
      file
    else
      next
    end
  end.compact.first

  raise LoadError, "file #{@file} could not be found in #{load_path}" if fn.nil?

  fn
end

#standard_directories(suffix = nil) ⇒ Object (private)



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/to_pass/file_reader.rb', line 68

def standard_directories(suffix = nil)
  suffix = suffix.to_s
  suffix = "/#{suffix}" unless suffix =~ /^\//

  ToPass::Directories[:all].map do |dir|
    dir.to_s + suffix
  end.map do |dir|
    dir = Pathname.new(dir).expand_path
    dir if dir.exist?
  end.compact
end