Sha256: 62ebe22f7293e824b5e52944e516a059f880b1534643914b3a4df571f7fbc728

Contents?: true

Size: 1.53 KB

Versions: 4

Compression:

Stored size: 1.53 KB

Contents

require 'hedgelog/scrubber'

class Hedgelog
  class Context
    def initialize(scrubber, 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
    end

    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

    def [](key)
      @data[key]
    end

    def delete(key)
      @data.delete(key)
    end

    def clear
      @data = {}
    end

    def merge(hash)
      @data.merge(hash)
    end

    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

    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

    def scrub!
      @data = @scrubber.scrub(@data)
      self
    end

    def to_h
      @data
    end

    private

    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}." if invalid_keys.length > 0
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
hedgelog-0.1.3 lib/hedgelog/context.rb
hedgelog-0.1.2 lib/hedgelog/context.rb
hedgelog-0.1.1 lib/hedgelog/context.rb
hedgelog-0.1.0 lib/hedgelog/context.rb