Sha256: d8856d43b328c1e5fba70f7d67f7db8c8004fcc3f293d4a13ed61797919f229f

Contents?: true

Size: 1.21 KB

Versions: 35

Compression:

Stored size: 1.21 KB

Contents

require 'thread'

if RUBY_PLATFORM == 'opal'
  # Stub thread class
  class Thread
    def self.current
      @current ||= {}
    end
  end
end

module Volt
  # Modes provide a way to effect the state inside of a block that
  # can be checked from elsewhere.  This is very useful if you have
  # some flag you may want to change without needing to pass all
  # of the way through some other code.
  module Modes
    module ClassMethods
      # Takes a block that when run, changes to mode inside of it
      def run_in_mode(mode_name)
        previous = Thread.current[mode_name]
        Thread.current[mode_name] = true
        begin
          yield
        ensure
          Thread.current[mode_name] = previous
        end
      end

      def run_in_mode_if(conditional, mode_name)
        if conditional
          # Yes, run in the mode
          Volt.run_in_mode(mode_name) do
            yield
          end
        else
          # Just run normally
          yield
        end
      end

      # Check to see if we are in the specified mode
      def in_mode?(mode_name)
        defined?(Thread) && Thread.current[mode_name]
      end
    end

    def self.included(base)
      base.send :extend, ClassMethods
    end
  end
end

Version data entries

35 entries across 35 versions & 1 rubygems

Version Path
volt-0.9.7.pre8 lib/volt/utils/modes.rb
volt-0.9.7.pre7 lib/volt/utils/modes.rb
volt-0.9.7.pre6 lib/volt/utils/modes.rb
volt-0.9.7.pre5 lib/volt/utils/modes.rb
volt-0.9.7.pre3 lib/volt/utils/modes.rb
volt-0.9.7.pre2 lib/volt/utils/modes.rb
volt-0.9.6 lib/volt/utils/modes.rb
volt-0.9.6.pre3 lib/volt/utils/modes.rb
volt-0.9.6.pre2 lib/volt/utils/modes.rb
volt-0.9.6.pre1 lib/volt/utils/modes.rb
volt-0.9.5 lib/volt/utils/modes.rb
volt-0.9.5.pre12 lib/volt/utils/modes.rb
volt-0.9.5.pre11 lib/volt/utils/modes.rb
volt-0.9.5.pre9 lib/volt/utils/modes.rb
volt-0.9.5.pre8 lib/volt/utils/modes.rb
volt-0.9.5.pre7 lib/volt/utils/modes.rb
volt-0.9.5.pre6 lib/volt/utils/modes.rb
volt-0.9.5.pre5 lib/volt/utils/modes.rb
volt-0.9.5.pre4 lib/volt/utils/modes.rb
volt-0.9.5.pre3 lib/volt/utils/modes.rb