Class: Hedgelog::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/hedgelog/context.rb

Instance Method Summary collapse

Constructor Details

#initialize(scrubber, normalizer, data = {}) ⇒ Context

Returns a new instance of Context.

Raises:

  • (::ArgumentError)


6
7
8
9
10
11
12
# 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

Instance Method Details

#[](key) ⇒ Object



20
21
22
# File 'lib/hedgelog/context.rb', line 20

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

#[]=(key, val) ⇒ Object

Raises:

  • (::ArgumentError)


14
15
16
17
18
# File 'lib/hedgelog/context.rb', line 14

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

#clearObject



28
29
30
# File 'lib/hedgelog/context.rb', line 28

def clear
  @data = {}
end

#delete(key) ⇒ Object



24
25
26
# File 'lib/hedgelog/context.rb', line 24

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

#merge(hash) ⇒ Object



32
33
34
# File 'lib/hedgelog/context.rb', line 32

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

#merge!(hash_or_context) ⇒ Object



36
37
38
39
40
41
# File 'lib/hedgelog/context.rb', line 36

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!Object



55
56
57
58
# File 'lib/hedgelog/context.rb', line 55

def normalize!
  @data = @normalizer.normalize(@data)
  self
end

#overwrite!(hash_or_context) ⇒ Object



43
44
45
46
47
48
# File 'lib/hedgelog/context.rb', line 43

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!Object



50
51
52
53
# File 'lib/hedgelog/context.rb', line 50

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

#to_hObject



60
61
62
# File 'lib/hedgelog/context.rb', line 60

def to_h
  @data
end