class Hedgelog::Context

Public Class Methods

new(scrubber, normalizer, data = {}) click to toggle source
# File lib/hedgelog/context.rb, line 6
def initialize(scrubber, normalizer, data = {})
  raise ::ArgumentError, "#{self.class}: argument must be Hash got #{data.class}." unless data.is_a? Hash

  check_reserved_keys(data)
  @data = data
  @scrubber = scrubber
  @normalizer = normalizer
end

Public Instance Methods

[](key) click to toggle source
# File lib/hedgelog/context.rb, line 21
def [](key)
  @data[key]
end
[]=(key, val) click to toggle source
# File lib/hedgelog/context.rb, line 15
def []=(key, val)
  raise ::ArgumentError, "#{self.class}: The #{key} is a reserved key and cannot be used." if Hedgelog::RESERVED_KEYS.include? key.to_sym

  @data[key] = val
end
clear() click to toggle source
# File lib/hedgelog/context.rb, line 29
def clear
  @data = {}
end
delete(key) click to toggle source
# File lib/hedgelog/context.rb, line 25
def delete(key)
  @data.delete(key)
end
merge(hash) click to toggle source
# File lib/hedgelog/context.rb, line 33
def merge(hash)
  @data.merge(hash)
end
merge!(hash_or_context) click to toggle source
# File lib/hedgelog/context.rb, line 37
def merge!(hash_or_context)
  check_reserved_keys(hash_or_context) unless hash_or_context.is_a? Hedgelog::Context

  hash_or_context = hash_or_context.to_h if hash_or_context.respond_to?(:to_h)
  @data = hash_or_context.merge(@data)
end
normalize!() click to toggle source
# File lib/hedgelog/context.rb, line 56
def normalize!
  @data = @normalizer.normalize(@data)
  self
end
overwrite!(hash_or_context) click to toggle source
# File lib/hedgelog/context.rb, line 44
def overwrite!(hash_or_context)
  check_reserved_keys(hash_or_context) unless hash_or_context.is_a? Hedgelog::Context

  hash_or_context = hash_or_context.to_h if hash_or_context.respond_to?(:to_h)
  @data.merge!(hash_or_context)
end
scrub!() click to toggle source
# File lib/hedgelog/context.rb, line 51
def scrub!
  @data = @scrubber.scrub(@data)
  self
end
to_h() click to toggle source
# File lib/hedgelog/context.rb, line 61
def to_h
  @data
end

Private Instance Methods

check_reserved_keys(hash) click to toggle source
# File lib/hedgelog/context.rb, line 65
        def check_reserved_keys(hash)
  invalid_keys = Hedgelog::RESERVED_KEYS & hash.keys
  raise ::ArgumentError, "#{self.class}: The following keys are reserved and cannot be used #{invalid_keys.to_a}." unless invalid_keys.empty?
end