class Necromancer::Conversions
Represents the context used to configure various converters and facilitate type conversion
@api public
Constants
- DELIMITER
Attributes
converter_map[R]
Map of type and conversion
@return [Hash]
@api private
Public Class Methods
new(configuration = Configuration.new, map = {})
click to toggle source
Creates a new conversions map
@example
conversion = Necromancer::Conversions.new
@api public
# File lib/necromancer/conversions.rb, line 26 def initialize(configuration = Configuration.new, map = {}) @configuration = configuration @converter_map = map.dup end
Public Instance Methods
[](source, target)
click to toggle source
Retrieve converter for source and target
@param [Object] source
the source of conversion
@param [Object] target
the target of conversion
@return [Converter]
the converter for the source and target
@api public
# File lib/necromancer/conversions.rb, line 55 def [](source, target) key = "#{source}#{DELIMITER}#{target}" converter_map[key] || converter_map["object#{DELIMITER}#{target}"] || raise_no_type_conversion_available(key) end
Also aliased as: fetch
load()
click to toggle source
Load converters
@api private
# File lib/necromancer/conversions.rb, line 34 def load ArrayConverters.load(self) BooleanConverters.load(self) DateTimeConverters.load(self) HashConverters.load(self) NumericConverters.load(self) RangeConverters.load(self) end
register(converter = nil, &block)
click to toggle source
Register a converter
@example with simple object
conversions.register NullConverter.new(:array, :array)
@example with block
conversions.register do |c| c.source = :array c.target = :array c.convert = -> { |val, options| val } end
@api public
# File lib/necromancer/conversions.rb, line 76 def register(converter = nil, &block) converter ||= Converter.create(&block) key = generate_key(converter) converter = add_config(converter, @configuration) return false if converter_map.key?(key) converter_map[key] = converter true end
to_hash()
click to toggle source
Export all the conversions as hash
@return [Hash[String, String]]
@api public
# File lib/necromancer/conversions.rb, line 91 def to_hash converter_map.dup end
Protected Instance Methods
add_config(converter, config)
click to toggle source
Inject config into converter
@api private
# File lib/necromancer/conversions.rb, line 115 def add_config(converter, config) converter.instance_exec(:"@config") do |var| instance_variable_set(var, config) end converter end
generate_key(converter)
click to toggle source
@api private
# File lib/necromancer/conversions.rb, line 105 def generate_key(converter) parts = [] parts << (converter.source ? converter.source.to_s : "none") parts << (converter.target ? converter.target.to_s : "none") parts.join(DELIMITER) end
raise_no_type_conversion_available(key)
click to toggle source
Fail with conversion error
@api private
# File lib/necromancer/conversions.rb, line 100 def raise_no_type_conversion_available(key) raise NoTypeConversionAvailableError, "Conversion '#{key}' unavailable." end