Class: Hedgelog::Context
- Inherits:
-
Object
- Object
- Hedgelog::Context
- Defined in:
- lib/hedgelog/context.rb
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, val) ⇒ Object
- #clear ⇒ Object
- #delete(key) ⇒ Object
-
#initialize(scrubber, normalizer, data = {}) ⇒ Context
constructor
A new instance of Context.
- #merge(hash) ⇒ Object
- #merge!(hash_or_context) ⇒ Object
- #normalize! ⇒ Object
- #overwrite!(hash_or_context) ⇒ Object
- #scrub! ⇒ Object
- #to_h ⇒ Object
Constructor Details
#initialize(scrubber, normalizer, data = {}) ⇒ Context
Returns a new instance of Context.
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
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 |
#clear ⇒ Object
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_h ⇒ Object
61 62 63 |
# File 'lib/hedgelog/context.rb', line 61 def to_h @data end |