Class: JrubyCoercion::Registry

Inherits:
Object
  • Object
show all
Defined in:
lib/jruby_coercion/registry.rb

Direct Known Subclasses

JrubyCoercion::RubyToJava::Registry

Constant Summary collapse

DEFAULT_KEY =
"JRUBY_COERCION_DEFAULT".freeze

Class Method Summary collapse

Class Method Details

.alternative_class(java_type) ⇒ Object

Class Methods



12
13
14
15
16
17
18
# File 'lib/jruby_coercion/registry.rb', line 12

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

.converter_registryObject



20
21
22
# File 'lib/jruby_coercion/registry.rb', line 20

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

.new_registry_entry_for_type(from_type) ⇒ Object



24
25
26
# File 'lib/jruby_coercion/registry.rb', line 24

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

.register_converter(from_type, to_type, callable = nil, &blk) ⇒ Object Also known as: register_conversion, register_coercion



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/jruby_coercion/registry.rb', line 28

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

.registry_converts_class?(convert_type) ⇒ Boolean Also known as: registry_converts_type?

Returns:

  • (Boolean)


43
44
45
46
47
48
49
# File 'lib/jruby_coercion/registry.rb', line 43

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

.registry_converts_class_and_to?(from_type, to_type) ⇒ Boolean Also known as: registry_converts_type_and_to?

Returns:

  • (Boolean)


51
52
53
54
55
56
57
58
# File 'lib/jruby_coercion/registry.rb', line 51

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