module Graphiti

robots.thoughtbot.com/mygem-configure-block

This could definitely use some refactoring love, but I have no time ATM The code is pretty self-contained; we’re just listening to notifications and taking action.

For “Rails STI” behavior CreditCard.all # => [<Visa>, <Mastercard>, etc]

If you’re using Rails + responders gem to get respond_with

Private, tested in resource specs

Todo: class purpose

A minimal implementation of an errors object similar to ‘ActiveModel::Errors`. Designed to support internal Graphiti classes like the `RequestValidator` so that there does not need to be a dependency on activemodel.

Constants

DEPRECATOR
VERSION

Public Class Methods

broadcast(name, payload) { |payload| ... } click to toggle source
# File lib/graphiti.rb, line 61
def self.broadcast(name, payload)
  # AS::N prefers domain naming format with more specific towards end
  name = "#{name}.graphiti"

  ActiveSupport::Notifications.instrument(name, payload) do
    yield payload if block_given?
  end
end
cache() click to toggle source
# File lib/graphiti.rb, line 114
def self.cache
  @cache
end
cache=(val) click to toggle source
# File lib/graphiti.rb, line 110
def self.cache=(val)
  @cache = val
end
config() click to toggle source
# File lib/graphiti.rb, line 43
def self.config
  @config ||= Configuration.new
end
configure() { |config| ... } click to toggle source

@example

Graphiti.configure do |c|
  c.raise_on_missing_sideload = false
end

@see Configuration

# File lib/graphiti.rb, line 53
def self.configure
  yield config
end
context() click to toggle source

@api private

# File lib/graphiti.rb, line 21
def self.context
  Thread.current[:context] ||= {}
end
context=(val) click to toggle source

@api private

# File lib/graphiti.rb, line 26
def self.context=(val)
  Thread.current[:context] = val
end
log(msg, color = :white, bold = false) click to toggle source
# File lib/graphiti.rb, line 86
def self.log(msg, color = :white, bold = false)
  colored = if ::ActiveSupport.version >= Gem::Version.new("7.1")
    ActiveSupport::LogSubscriber.new.send(:color, msg, color, bold: bold)
  else
    ActiveSupport::LogSubscriber.new.send(:color, msg, color, bold)
  end

  logger.debug(colored)
end
logger() click to toggle source
# File lib/graphiti.rb, line 70
def self.logger
  @logger ||= stdout_logger
end
logger=(val) click to toggle source
# File lib/graphiti.rb, line 82
def self.logger=(val)
  @logger = val
end
resources() click to toggle source
# File lib/graphiti.rb, line 57
def self.resources
  @resources ||= []
end
setup!() click to toggle source

When we add a sideload, we need to do configuration, such as adding the relationship to the Resource’s serializer. However, the sideload’s Resource class may not be loaded yet.

This is not a problem when Rails autoloading, but is a problem when eager loading, or not using Rails.

So, load every Resource class then call Graphiti.setup!

# File lib/graphiti.rb, line 104
def self.setup!
  resources.each do |r|
    r.apply_sideloads_to_serializer
  end
end
stdout_logger() click to toggle source
# File lib/graphiti.rb, line 74
def self.stdout_logger
  logger = Logger.new($stdout)
  logger.formatter = proc do |severity, datetime, progname, msg|
    "#{msg}\n"
  end
  logger
end
with_context(obj, namespace = nil) { || ... } click to toggle source

@api private

# File lib/graphiti.rb, line 31
def self.with_context(obj, namespace = nil)
  prior = context
  self.context = {object: obj, namespace: namespace}
  yield
ensure
  self.context = prior

  resources.each do |resource_class|
    resource_class.sideloads.values.each(&:clear_resources)
  end
end