Class: ToPass::AlgorithmReader

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

Overview

The AlgorithmReader’s primary API is to load the rules from a YAML-file into a Hash.

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

see ToPass::Converter for usage of the loaded algorithm

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(algorithm) ⇒ AlgorithmReader

:nodoc:



17
18
19
20
21
22
23
24
25
26
# File 'lib/to_pass/algorithm_reader.rb', line 17

def initialize(algorithm) # :nodoc:
  @algorithm = algorithm
  @load_path = []
  ToPass::Directories[:standard].map do |dir|
    dir + '/algorithms'
  end.each do |dir|
    dir = Pathname.new(dir).expand_path
    @load_path << dir if dir.exist?
  end
end

Instance Attribute Details

#load_pathObject (readonly)

Returns the value of attribute load_path.



15
16
17
# File 'lib/to_pass/algorithm_reader.rb', line 15

def load_path
  @load_path
end

Class Method Details

.discoverObject

searches for available algorithms



35
36
37
38
39
40
41
# File 'lib/to_pass/algorithm_reader.rb', line 35

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

.load(algorithm) ⇒ Object

load an algorithm with a given identifier



30
31
32
# File 'lib/to_pass/algorithm_reader.rb', line 30

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

Instance Method Details

#load_from_fileObject

:nodoc:

Raises:

  • (LoadError)


44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/to_pass/algorithm_reader.rb', line 44

def load_from_file # :nodoc:
  fn = load_path.map do |dir|
    file = Pathname.new("#{dir}/#{@algorithm}.yml")

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

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

  YAML.load_file(fn.expand_path)
end