Sha256: 244785bee29204bed1a4c79ff62c5fc75dbd34513c66e63d5565b67cd33e7b51

Contents?: true

Size: 1.31 KB

Versions: 1

Compression:

Stored size: 1.31 KB

Contents

class Object
  def resugan(namespace = '', &block)
    namespace ||= ''
    current_thread = Thread.current
    current_thread.push_resugan_context(namespace)
    begin
      block.call
    ensure
      context = current_thread.pop_resugan_context
    end
    context
  end

  def resugan!(namespace = '', &block)
    namespace ||= ''
    current_thread = Thread.current
    current_thread.push_resugan_context(namespace, true)
    begin
      block.call
    ensure
      context = current_thread.pop_resugan_context(true)
    end
    context
  end

  def _fire(event, params = {})
    params[:_source] = caller[0] if Resugan::Kernel.line_trace_enabled?

    current_thread = Thread.current
    if current_thread.resugan_context
      current_thread.resugan_context.register(event, params)
    else
      puts "WARN: #{event} called in #{caller[0]} but was not inside a resugan {} block" if Resugan::Kernal.warn_no_context_events?
    end
  end

  def _listener(event, options = {}, &block)
    Resugan::Kernel.register_with_namespace(options[:namespace], event, options[:id], ->(params) {
        block.call(params)
      })
  end

  def _listener!(event, options = {}, &block)
    Resugan::Kernel.register_with_namespace(options[:namespace], event, options[:id] || caller[0], ->(params) {
        block.call(params)
      })
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
resugan-0.1.14 lib/resugan/object.rb