Sha256: ac5d512574dae18491685ac8a64a3c560c73529c6396fe3397493617097d38aa

Contents?: true

Size: 1.47 KB

Versions: 1

Compression:

Stored size: 1.47 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)
      puts "calling set_var(#{var})"
      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

1 entries across 1 versions & 1 rubygems

Version Path
hyperloop-config-0.9.6 lib/hyperloop/context.rb