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
13
# 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



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

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

#[]=(key, val) ⇒ Object

Raises:

  • (::ArgumentError)


15
16
17
18
19
# 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

#clearObject



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

def clear
  @data = {}
end

#delete(key) ⇒ Object



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

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

#merge(hash) ⇒ Object



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

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

#merge!(hash_or_context) ⇒ Object



37
38
39
40
41
42
# 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!Object



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

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

#overwrite!(hash_or_context) ⇒ Object



44
45
46
47
48
49
# 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!Object



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

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

#to_hObject



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

def to_h
  @data
end