Class: ToPass::ConverterReader

Inherits:
FileReader show all
Defined in:
lib/to_pass/converter_reader.rb

Overview

The ConverterReader’s primary API is to load the converters from right directories into an Array

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

The bundled converter are, however, lazily loaded with autoload. User-provided converters are always required (for now).

see ToPass::Converter

Instance Attribute Summary collapse

Attributes inherited from FileReader

#load_path

Instance Method Summary collapse

Methods inherited from FileReader

discover, extension, load, search_pattern, #standard_directories

Constructor Details

#initializeConverterReader

:nodoc:



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

def initialize # :nodoc:
  @loaded     = []
  @discovered = []
  super(nil, 'converters')
end

Instance Attribute Details

#loadedObject (readonly)

Returns the value of attribute loaded.



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

def loaded
  @loaded
end

Instance Method Details

#camel_case(underscored_word) ⇒ Object (private)

:nodoc:



92
93
94
95
96
# File 'lib/to_pass/converter_reader.rb', line 92

def camel_case(underscored_word) # :nodoc:
  camel_cased_word = underscored_word.to_s.split('_').map do |part|
    part.capitalize
  end.join('')
end

#classname(converter) ⇒ Object (private)

:nodoc:



77
78
79
# File 'lib/to_pass/converter_reader.rb', line 77

def classname(converter) # :nodoc:
  constantize("ToPass::Converters::#{camel_case(converter)}")
end

#constantize(camel_cased_word) ⇒ Object (private)

:nodoc:



81
82
83
84
85
86
87
88
89
90
# File 'lib/to_pass/converter_reader.rb', line 81

def constantize(camel_cased_word) # :nodoc:
  names = camel_cased_word.split('::')
  names.shift if names.empty? || names.first.empty?

  constant = Object
  names.each do |name|
    constant = constant.const_defined?(name) ? constant.const_get(name) : constant.const_missing(name)
  end
  constant
end

#discoverObject

discover a list of available converters



34
35
36
# File 'lib/to_pass/converter_reader.rb', line 34

def discover
  search_for_converters
end

#load(converter) ⇒ Object

loads the converters



24
25
26
27
28
29
30
31
# File 'lib/to_pass/converter_reader.rb', line 24

def load(converter)
  unless @loaded.include?(converter.to_sym)
    load_from_file(converter)
    @loaded << converter
  end

  classname(converter)
end

#load_from_file(converter) ⇒ Object (private)

:nodoc:

Raises:

  • (LoadError)


59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/to_pass/converter_reader.rb', line 59

def load_from_file(converter) # :nodoc:
  fn = load_path.collect do |dir|
    path = Pathname.new("#{dir}/#{converter}.rb").expand_path

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

  raise LoadError, "converter '#{converter}' could not be found in #{load_path.join(' ')}" if fn.nil?

  if require fn
    classname converter
  end
end

#search_for_convertersObject (private)

:nodoc:

Raises:

  • (LoadError)


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/to_pass/converter_reader.rb', line 40

def search_for_converters # :nodoc:
  files = load_path.collect do |directory|
    dir = Pathname.new(directory)
    if dir.exist?
      Dir["#{dir}/*.rb"].collect do |converter|
        fn_re = %r!/([^/]*)\.rb$!i
        name  = converter.match(fn_re)
        if name
          name[1].to_sym
        end
      end
    end
  end.flatten.compact

  raise LoadError, "converters could not be found in #{load_path}" if files.nil?

  @discovered = files
end