Module: Poleica::Convertible

Included in:
Polei
Defined in:
lib/poleica/converters/convertible.rb

Overview

Conversion Logic, given a type it can search for compatible converters

Constant Summary collapse

CONVERTERS =
Poleica::Converters.constants
.map { |c| Poleica::Converters.const_get(c) } -
[Poleica::Converters::Utils]

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



9
10
11
12
13
14
# File 'lib/poleica/converters/convertible.rb', line 9

def method_missing(method, *args, &block)
  extension, options =
    Converters::Utils.extract_extension_and_options(method, args)
  return convert_to_extension(extension, options) if extension
  super
end

Class Method Details

.compatible_converters_by_type(type) ⇒ Object



43
44
45
46
47
# File 'lib/poleica/converters/convertible.rb', line 43

def compatible_converters_by_type(type)
  CONVERTERS.select do |converter|
    converter::COMPATIBLE_TYPES.include?(type)
  end
end

.convert_methods_for_converter(converter) ⇒ Object



49
50
51
# File 'lib/poleica/converters/convertible.rb', line 49

def convert_methods_for_converter(converter)
  methods_for_converter(converter).reject { |m| !(m =~ /^to_(.*)/) }
end

.methods_for_converter(converter) ⇒ Object



53
54
55
# File 'lib/poleica/converters/convertible.rb', line 53

def methods_for_converter(converter)
  converter.instance_methods(false).map(&:to_s)
end

Instance Method Details

#compatible_convert_methodsObject



34
35
36
37
38
39
# File 'lib/poleica/converters/convertible.rb', line 34

def compatible_convert_methods
  @compatible_convert_methods ||=
    compatible_converters.map do |c|
      convert_methods_for_converter(c)
    end.flatten
end

#compatible_convertersObject



28
29
30
31
32
# File 'lib/poleica/converters/convertible.rb', line 28

def compatible_converters
  @compatible_converters ||=
    compatible_converters_by_type(file_type.class) <<
      Converters::General
end

#convert_to_extension(extension, options = {}) ⇒ Object



16
17
18
19
# File 'lib/poleica/converters/convertible.rb', line 16

def convert_to_extension(extension, options = {})
  converter = converter_to_extension(extension).new(self)
  converter.send("to_#{extension}".to_sym, options)
end

#converter_to_extension(extension) ⇒ Object



21
22
23
24
25
26
# File 'lib/poleica/converters/convertible.rb', line 21

def converter_to_extension(extension)
  compatible_converter = compatible_converters.find do |converter|
    converter.method_defined?(:"to_#{extension}")
  end
  compatible_converter || Converters::Coercive
end