Sha256: b857bbce934e7dd726f2b7d91774da2a9ebde2ea34dfd908f1c70dbe41c77b64

Contents?: true

Size: 1.46 KB

Versions: 37

Compression:

Stored size: 1.46 KB

Contents

require "cabin/namespace"

# Logging context exists to make it easy to add and later undo any changes made
# to the context data associated with a given Logging::Channel
# 
# Usage:
#
#     context = channel.context
#     context["foo"] = "Hello world!"
#     channel.info("Sample log") # output includes { "foo" => "Hello world!" }
#     context.clear
#     channel.info("Sample log 2") # context cleared, key "foo" removed.
#
# Essentially, a Cabin::Context acts as a transactional proxy on top of a
# Cabin::Channel. Any changes you make in a context are passed through to
# the channel while keeping an ordered record of the changes made so
# you can unroll those changes when the context is no longer needed..
# 
class Cabin::Context
  def initialize(channel)
    @changes = []
    @channel = channel
  end # def initialize

  def on_clear(&block)
    @clear_callback = block
  end # def on_clear

  def []=(key, value)
    # Maintain a record of what was changed so clear() can undo this context.
    # This record is in reverse order so it can be undone in reverse later.
    @changes.unshift([key, value, @channel[key]])
    @channel[key] = value
  end # def []=

  def [](key)
    @channel[key]
  end # def []
  
  # Undo any changes made to the channel by this context.
  def clear
    @changes.each do |key, value, original|
      if original.nil?
        @channel.remove(key)
      else
        @channel[key] = original
      end
    end
  end # def clear
end # class Cabin::Context

Version data entries

37 entries across 35 versions & 6 rubygems

Version Path
logstash-filter-zabbix-0.1.2 vendor/bundle/jruby/1.9/gems/cabin-0.8.1/lib/cabin/context.rb
logstash-filter-zabbix-0.1.1 vendor/bundle/jruby/1.9/gems/cabin-0.8.1/lib/cabin/context.rb
ivanvc-logstash-input-s3-3.1.1.4 vendor/local/gems/cabin-0.8.1/lib/cabin/context.rb
ivanvc-logstash-input-s3-3.1.1.3 vendor/local/gems/cabin-0.8.1/lib/cabin/context.rb
ivanvc-logstash-input-s3-3.1.1.2 vendor/local/gems/cabin-0.8.1/lib/cabin/context.rb
cabin-0.9.0 lib/cabin/context.rb
able-neo4j-1.0.0 vendor/bundle/jruby/1.9/gems/cabin-0.7.1/lib/cabin/context.rb
cabin-0.8.1 lib/cabin/context.rb
cabin-0.8.0 lib/cabin/context.rb
logstash-input-beats-2.0.2 vendor/jruby/1.9/gems/cabin-0.7.2/lib/cabin/context.rb
logstash-input-beats-2.0.2 vendor/jruby/1.9/gems/cabin-0.7.1/lib/cabin/context.rb
logstash-input-beats-2.0.2 vendor/jruby/1.9/gems/logstash-codec-json-2.0.3/vendor/gems/cabin-0.7.2/lib/cabin/context.rb
logstash-codec-json-2.0.3 vendor/gems/cabin-0.7.2/lib/cabin/context.rb
cabin-0.7.2 lib/cabin/context.rb
logstash-input-beats-0.9.2 vendor/jruby/1.9/gems/cabin-0.7.1/lib/cabin/context.rb
logstash-input-beats-0.9.1 vendor/jruby/1.9/gems/cabin-0.7.1/lib/cabin/context.rb
cabin-0.7.1 lib/cabin/context.rb
cabin-0.6.1 lib/cabin/context.rb
cabin-0.6.0 lib/cabin/context.rb
cabin-0.5.0 lib/cabin/context.rb