Sha256: 64b43a89920a188f9aae26d8c905c1c479a3ddf50ec09b527dba35279fb0a812

Contents?: true

Size: 1.44 KB

Versions: 20

Compression:

Stored size: 1.44 KB

Contents

module Hyperloop
  # Allows interactive systems to reset context to the state at boot. Any
  # modules or classes that set context instance variables to hold things like
  # call backs should use Hyperloop::Context.set_var(self, :@var_name) { .... }
  # the provided block will be rerun and the instance variable re-initialized
  # when the reset! method is called
  module Context
    # Replace @foo ||= ... with
    # Context.set_var(self, :@foo) { ... }
    # If reset! has been called then the instance variable will be record, and
    # will be reset on the next call to reset!
    # If you want to record the current value of the instance variable then set
    # force to true.
    def self.set_var(ctx, var, force: nil)
      inst_value_b4 = ctx.instance_variable_get(var)
      if @context && !@context[ctx].key?(var) && (force || !inst_value_b4)
        @context[ctx][var] = (inst_value_b4 && inst_value_b4.dup)
      end
      inst_value_b4 || ctx.instance_variable_set(var, yield)
    end

    def self.reset!(reboot = true)
      # if @context is already initialized then reset all the instance
      # vars using their corresponding blocks.  Otherwise initialize
      # @context.
      if @context
        @context.each do |ctx, vars|
          vars.each { |var, init| ctx.instance_variable_set(var, init) }
        end
        Hyperloop::Application::Boot.run if reboot
      else
        @context = Hash.new { |h, k| h[k] = {} }
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
hyperloop-config-0.99.6 lib/hyperloop/context.rb
hyperloop-config-0.99.5 lib/hyperloop/context.rb
hyperloop-config-0.99.4 lib/hyperloop/context.rb
hyperloop-config-0.99.3 lib/hyperloop/context.rb
hyperloop-config-0.99.2 lib/hyperloop/context.rb
hyperloop-config-0.99.1 lib/hyperloop/context.rb
hyperloop-config-0.99.0 lib/hyperloop/context.rb
hyperloop-config-1.0.0.lap28 lib/hyperloop/context.rb
hyperloop-config-1.0.0.lap27 lib/hyperloop/context.rb
hyperloop-config-1.0.0.lap26 lib/hyperloop/context.rb
hyperloop-config-1.0.0.lap25 lib/hyperloop/context.rb
hyperloop-config-1.0.0.lap24 lib/hyperloop/context.rb
hyperloop-config-1.0.0.lap23 lib/hyperloop/context.rb
hyperloop-config-1.0.0.lap22 lib/hyperloop/context.rb
hyperloop-config-1.0.0.lap21 lib/hyperloop/context.rb
hyperloop-config-0.9.11 lib/hyperloop/context.rb
hyperloop-config-0.9.10 lib/hyperloop/context.rb
hyperloop-config-0.9.9 lib/hyperloop/context.rb
hyperloop-config-0.9.8 lib/hyperloop/context.rb
hyperloop-config-0.9.7 lib/hyperloop/context.rb