Sha256: 1958b115f1c3a239b38ca325b32e64e940782a10f174e82b5cf451971043a3bc

Contents?: true

Size: 614 Bytes

Versions: 5

Compression:

Stored size: 614 Bytes

Contents

module RailsConnector

class TypeComputer
  def initialize(base_class, fallback_class)
    @base_class, @fallback_class = base_class, fallback_class
    @type_cache = {}
  end

  def compute_type(obj_class)
    @type_cache.fetch(obj_class) { calculate_type(obj_class) }
  end

  private

  def calculate_type(obj_class)
    load_class(obj_class) || @fallback_class
  end

  def load_class(obj_class)
    klass = obj_class.constantize
    klass if inherits_from_base?(klass)
  rescue NameError
  end

  def inherits_from_base?(klass)
    klass.ancestors.include?(@base_class) && klass != @base_class
  end
end

end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
infopark_cloud_connector-7.1.0 lib/rails_connector/type_computer.rb
infopark_cloud_connector-7.0.2 lib/rails_connector/type_computer.rb
infopark_cloud_connector-7.0.1 lib/rails_connector/type_computer.rb
infopark_cloud_connector-7.0.0 lib/rails_connector/type_computer.rb
infopark_cloud_connector-6.9.5 lib/rails_connector/type_computer.rb