Module: ActiveShopifyGraphQL::Model::LoaderSwitchable

Extended by:
ActiveSupport::Concern
Included in:
ActiveShopifyGraphQL::Model
Defined in:
lib/active_shopify_graphql/model/loader_switchable.rb

Instance Method Summary collapse

Instance Method Details

#with_admin_api(&block) ⇒ self

Executes with the admin API loader

Returns:

  • (self)


26
27
28
# File 'lib/active_shopify_graphql/model/loader_switchable.rb', line 26

def with_admin_api(&block)
  with_loader(ActiveShopifyGraphQL::Loaders::AdminApiLoader, &block)
end

#with_customer_account_api(&block) ⇒ self

Executes with the customer account API loader

Returns:

  • (self)


32
33
34
# File 'lib/active_shopify_graphql/model/loader_switchable.rb', line 32

def (&block)
  with_loader(ActiveShopifyGraphQL::Loaders::CustomerAccountApiLoader, &block)
end

#with_loader(loader_class) {|Object| ... } ⇒ Object

Generic method to execute with a specific loader

Parameters:

  • loader_class (Class)

    The loader class to use

Yields:

  • (Object)

    Block to execute with the loader

Returns:

  • (Object)

    Result of the block



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/active_shopify_graphql/model/loader_switchable.rb', line 11

def with_loader(loader_class, &_block)
  old_loader = Thread.current[:active_shopify_graphql_loader]
  Thread.current[:active_shopify_graphql_loader] = loader_class.new(self.class)

  if block_given?
    yield(self)
  else
    self
  end
ensure
  Thread.current[:active_shopify_graphql_loader] = old_loader
end