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
10
11
12
13
14
15
|
# File 'lib/poleica/converters/convertible.rb', line 10
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
44
45
46
47
48
|
# File 'lib/poleica/converters/convertible.rb', line 44
def compatible_converters_by_type(type)
CONVERTERS.select do |converter|
converter::COMPATIBLE_TYPES.include?(type)
end
end
|
.convert_methods_for_converter(converter) ⇒ Object
50
51
52
|
# File 'lib/poleica/converters/convertible.rb', line 50
def convert_methods_for_converter(converter)
methods_for_converter(converter).reject { |m| !(m =~ /^to_(.*)/) }
end
|
.methods_for_converter(converter) ⇒ Object
54
55
56
|
# File 'lib/poleica/converters/convertible.rb', line 54
def methods_for_converter(converter)
converter.instance_methods(false).map(&:to_s)
end
|
Instance Method Details
#compatible_convert_methods ⇒ Object
35
36
37
38
39
40
|
# File 'lib/poleica/converters/convertible.rb', line 35
def compatible_convert_methods
@compatible_convert_methods ||=
compatible_converters.map do |c|
convert_methods_for_converter(c)
end.flatten
end
|
#compatible_converters ⇒ Object
29
30
31
32
33
|
# File 'lib/poleica/converters/convertible.rb', line 29
def compatible_converters
@compatible_converters ||=
compatible_converters_by_type(self.file_type.class) <<
Converters::General
end
|
#convert_to_extension(extension, options = {}) ⇒ Object
17
18
19
20
|
# File 'lib/poleica/converters/convertible.rb', line 17
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
22
23
24
25
26
27
|
# File 'lib/poleica/converters/convertible.rb', line 22
def converter_to_extension(extension)
compatible_converter = compatible_converters.find do |converter|
converter.method_defined?(:"to_#{extension}")
end
compatible_converter ||= Converters::Coercive
end
|