123456789_123456789_123456789_123456789_123456789_

Module: GraphQL::Client::CollocatedEnforcement

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Extended In:
Defined in: lib/graphql/client/collocated_enforcement.rb

Overview

Enforcements collocated object access best practices.

Instance Method Summary

Instance Method Details

#allow_noncollocated_callers

Public: Ignore collocated caller enforcement for the scope of the block.

[ GitHub ]

  
# File 'lib/graphql/client/collocated_enforcement.rb', line 18

def allow_noncollocated_callers
  Thread.current[:query_result_caller_location_ignore] = true
  yield
ensure
  Thread.current[:query_result_caller_location_ignore] = nil
end

#enforce_collocated_callers(mod, methods, path)

Internal: Decorate method with collocated caller enforcement.

mod - Target Module/Class methods - Array of Symbol method names path - String filename to assert calling from

Returns nothing.

[ GitHub ]

  
# File 'lib/graphql/client/collocated_enforcement.rb', line 49

def enforce_collocated_callers(mod, methods, path)
  mod.prepend(Module.new do
    methods.each do |method|
      define_method(method) do |*args, &block|
        location = caller_locations(1, 1)[0]
        CollocatedEnforcement.verify_collocated_path(location, path, method) do
          super(*args, &block)
        end
      end
    end
  end)
end

#verify_collocated_path(location, path, method = "method")

[ GitHub ]

  
# File 'lib/graphql/client/collocated_enforcement.rb', line 25

def verify_collocated_path(location, path, method = "method")
  return yield if Thread.current[:query_result_caller_location_ignore]

  if (location.path != path) && !(WHITELISTED_GEM_NAMES.any? { |g| location.path.include?("gems/#{g}") })
    error = NonCollocatedCallerError.new("#{method} was called outside of '#{path}' https://git.io/v1syX")
    error.set_backtrace(caller(2))
    raise error
  end

  begin
    Thread.current[:query_result_caller_location_ignore] = true
    yield
  ensure
    Thread.current[:query_result_caller_location_ignore] = nil
  end
end