class Poleica::Converters::Coercive
The Coercive
converter, it tries to coerce the polei
Constants
- COMPATIBLE_TYPES
- TYPE_RETURNED_BY_METHOD
TODO: Think about another way of declare return types
Attributes
Public Class Methods
# File lib/poleica/converters/coercive.rb, line 20 def initialize(polei) @polei = polei end
Private Instance Methods
# File lib/poleica/converters/coercive.rb, line 40 def coerce(method, options) conversion_hash = find_next_converter_by_method(method) return nil unless conversion_hash next_method = conversion_hash.keys.first polei.send(next_method.to_sym, options) end
Find the next method and converter needed to execute a given conversion @param <Symbol/String> the given method conversion name @return [Hash<Symbol, Converter>] the next method needed to execute
# File lib/poleica/converters/coercive.rb, line 51 def find_next_converter_by_method(origin_method) next_converters_by_method.find do |converter_by_method| conv = converter_by_method.values.first conv.method_defined?(origin_method.to_sym) end end
# File lib/poleica/converters/coercive.rb, line 26 def method_missing(method, *args, &block) extension, options = Utils.extract_extension_and_options(method, args) return try_convert(method, options) if extension super end
Returns a hash with the method to execute and the Converters
reachable by it @return [
# File lib/poleica/converters/coercive.rb, line 61
def next_converters_by_method
polei.compatible_convert_methods.map do |m|
ret_type = TYPE_RETURNED_BY_METHOD[m.to_sym]
next unless ret_type
convs = Poleica::Convertible.compatible_converters_by_type(ret_type)
convs.map { |conv| { m.to_sym => conv } }
end.compact.flatten(1)
end
# File lib/poleica/converters/coercive.rb, line 32 def try_convert(method, options = {}) coerced_path = coerce(method, options) return nil unless coerced_path converted_path = Poleica.new(coerced_path).send(method.to_sym, options) File.delete(coerced_path) converted_path end