Sha256: 0c4c4797632acef5f07e42a84daa3e86319cda22ea195276799b1611d6fffedb

Contents?: true

Size: 1018 Bytes

Versions: 1

Compression:

Stored size: 1018 Bytes

Contents

# frozen_string_literal: true

module JsonApiClient
  module Utils
    def self.compute_type(klass, type_name)
      # If the type is prefixed with a scope operator then we assume that
      # the type_name is an absolute reference.
      return type_name.constantize if type_name.match(/^::/)

      # Build a list of candidates to search for
      candidates = []
      klass.name.scan(/::|$/) { candidates.unshift "#{$`}::#{type_name}" }
      candidates << type_name

      candidates.each do |candidate|
        constant = candidate.constantize
        return constant if candidate == constant.to_s
      rescue NameError => e
        # We don't want to swallow NoMethodError < NameError errors
        raise e unless e.instance_of?(NameError)
      end

      # raise NameError, "uninitialized constant #{candidates.first}"
      # return default resource if not found
      Rails.logger.warn("WARNING - JsonApiClient: uninitialized constant #{candidates.first}")
      JsonApiClient::Resource
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
translation_cms-0.1.5 lib/json_api_client/utils.rb