Sha256: 3860ccd7650dd7be2c830f728db1769a1a2fd6172163605e485de289928cdb23

Contents?: true

Size: 1.97 KB

Versions: 4

Compression:

Stored size: 1.97 KB

Contents

require 'thread'
require 'jruby_coercion'
require 'jruby_coercion/converter'

class ::JrubyCoercion::Registry

  DEFAULT_KEY = "JRUBY_COERCION_DEFAULT".freeze

  ##
  # Class Methods
  #
  def self.alternative_class(java_type)
    if ::JrubyCoercion.native_type?(java_type)
      return java_type.ruby_class
    else
      return java_type.java_class
    end
  end

  def self.converter_registry
    @converter_registry ||= Java::JavaUtilConcurrent::ConcurrentHashMap.new
  end

  def self.new_registry_entry_for_type(from_type)
    raise "new_registry_entry_for_type must be overridden in child classes"
  end

  def self.register_converter(from_type, to_type, callable = nil, &blk)
    Thread.exclusive do
      callable ||= blk
      to_type ||= DEFAULT_KEY

      from_type_converter = (converter_registry[from_type] ||= new_registry_entry_for_type(from_type))

      from_type_converter[to_type] = callable
      # Register alternative type java_class/ruby_class
      from_type_converter[self.alternative_class(to_type)] = callable unless to_type == DEFAULT_KEY

      converter_registry[from_type] = from_type_converter
    end
  end

  def self.registry_converts_class?(convert_type)
    if !convert_type.nil? && (class_level = converter_registry[convert_type])
      return class_level
    else
      false
    end
  end

  def self.registry_converts_class_and_to?(from_type, to_type)
    if (class_level = registry_converts_class?(from_type))
      to_type ||= DEFAULT_KEY
      return ::JrubyCoercion::Converter.new(class_level[to_type])
    else
      return ::JrubyCoercion::Converter.new(false)
    end
  end

  ##
  # Class Aliases
  #
  class << self
    alias_method :register_conversion, :register_converter
    alias_method :register_coercion, :register_converter
    alias_method :registry_converts_type?, :registry_converts_class?
    alias_method :registry_converts_type_and_to?, :registry_converts_class_and_to?
    alias_method :registry_for_type_and_to, :registry_converts_type_and_to?
  end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
jruby_coercion-0.0.6-java lib/jruby_coercion/registry.rb
jruby_coercion-0.0.5-java lib/jruby_coercion/registry.rb
jruby_coercion-0.0.4-java lib/jruby_coercion/registry.rb
jruby_coercion-0.0.3-java lib/jruby_coercion/registry.rb