Sha256: f397a40c7749729931932cdb5b53a36bb19d237cdf2d5015eec3c72a55f21e93

Contents?: true

Size: 1.38 KB

Versions: 20

Compression:

Stored size: 1.38 KB

Contents

module JsonApiClient
  module Utils

    def self.compute_type(klass, type_name)
      return klass.custom_type_to_class.fetch(type_name).constantize if klass.custom_type_to_class.key?(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|
        begin
          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
      end

      raise NameError, "uninitialized constant #{candidates.first}"
    end

    def self.parse_includes(klass, *tables)
      tables.map do |table|
        case table
        when Hash
          table.map do |k, v|
            parse_includes(klass, *v).map do |sub|
              "#{k}.#{sub}"
            end
          end
        when Array
          table.map do |v|
            parse_includes(klass, *v)
          end
        else
          klass.key_formatter.format(table)
        end
      end.flatten
    end

  end
end

Version data entries

20 entries across 20 versions & 2 rubygems

Version Path
json_api_client-1.21.1 lib/json_api_client/utils.rb
json_api_client-1.21.0 lib/json_api_client/utils.rb
json_api_client-1.20.0 lib/json_api_client/utils.rb
json_api_client-1.19.0 lib/json_api_client/utils.rb
carwow-json_api_client-1.19.0 lib/json_api_client/utils.rb
json_api_client-1.18.0 lib/json_api_client/utils.rb
json_api_client-1.17.1 lib/json_api_client/utils.rb
json_api_client-1.17.0 lib/json_api_client/utils.rb
json_api_client-1.16.1 lib/json_api_client/utils.rb
json_api_client-1.16.0 lib/json_api_client/utils.rb
json_api_client-1.15.0 lib/json_api_client/utils.rb
json_api_client-1.14.1 lib/json_api_client/utils.rb
json_api_client-1.14.0 lib/json_api_client/utils.rb
json_api_client-1.13.0 lib/json_api_client/utils.rb
json_api_client-1.12.2 lib/json_api_client/utils.rb
json_api_client-1.12.1 lib/json_api_client/utils.rb
json_api_client-1.12.0 lib/json_api_client/utils.rb
json_api_client-1.11.0 lib/json_api_client/utils.rb
json_api_client-1.10.0 lib/json_api_client/utils.rb
json_api_client-1.9.0 lib/json_api_client/utils.rb