class Poro::ContextFactory

This class serves as both the base class for all context factories, and the root class for retriving the application’s context factory.

Public Class Methods

has_instance?() click to toggle source

Returns true if a context factory instance is configured.

# File lib/poro/context_factory.rb, line 27
def self.has_instance?
  return (@instance != nil)
end
instance() click to toggle source

Returns the context factory instance for the application. Returns nil if none is set.

One normally gets this via Context.factory, but it doesn’t make a difference.

# File lib/poro/context_factory.rb, line 13
def self.instance
  raise RuntimeError, "No context factory configured for this application." if @instance.nil?
  return @instance
end
instance=(instance) click to toggle source

Sets the context factory instance for the application.

One normally sets this via Context.factory, but it doesn’t make a difference.

# File lib/poro/context_factory.rb, line 21
def self.instance=(instance)
  raise TypeError, "Cannot set an object of class #{instance.class} as the application's context factory." unless instance.kind_of?(self) || instance.nil?
  @instance = instance
end
new(&context_factory_block) click to toggle source

Takes a factory block that delivers a configured context for the class passed to it.

# File lib/poro/context_factory.rb, line 33
def initialize(&context_factory_block)
  @context_factory_block = context_factory_block
  @context_cache = {}
end

Public Instance Methods

context_managed_class?(klass) click to toggle source
# File lib/poro/context_factory.rb, line 38
def context_managed_class?(klass)
  return klass && klass.include?(Poro::Persistify)
end
fetch(klass) click to toggle source

Fetches the context for a given class, or returns nil if the given object should not have a context.

This is the most basic implementation possible, though, like any context factory must do, it guarantees that the same Context instance will be returned for the same class throughout the lifetime of the application so that configuration subsequent to generation is honored.

Subclasses are expected to call this method instead of running the factory block directly.

# File lib/poro/context_factory.rb, line 52
def fetch(klass)
  raise FactoryError, "Cannot create a context for class #{klass.inspect}, as it has not been flagged for persistence.  Include Context::Persistify to fix." unless self.context_managed_class?(klass)
  if( !@context_cache.has_key?(klass) )
    @context_cache[klass] = build(klass)
  end
  return @context_cache[klass]
end

Private Instance Methods

build(klass) click to toggle source

Calls the context factory block to generate a new context. This should not be called directly, but instead left to the fetch method to call when needed so that it is only called once per class during the application’s lifetime.

# File lib/poro/context_factory.rb, line 66
def build(klass)
  begin
    return @context_factory_block.call(klass)
  rescue Exception => e
    raise RuntimeError, "Error encountered during context fetch build: #{e.class}: #{e.message.inspect}", e.backtrace
  end
end