Class: Lumberjack::AttributesHelper
- Inherits:
-
Object
- Object
- Lumberjack::AttributesHelper
- Defined in:
- lib/lumberjack/attributes_helper.rb
Overview
This class provides an interface for manipulating a attribute hash in a consistent manner.
Direct Known Subclasses
Class Method Summary collapse
-
.expand_runtime_values(hash) ⇒ Hash
Expand any values in a hash that are Proc’s by calling them and replacing the value with the result.
Instance Method Summary collapse
-
#[](name) ⇒ Object
Get a attribute value.
-
#[]=(name, value) ⇒ void
Set a attribute value.
-
#delete(*names) ⇒ void
Remove attributes from the context.
-
#initialize(attributes) ⇒ AttributesHelper
constructor
A new instance of AttributesHelper.
-
#to_h ⇒ Hash
Return a copy of the attributes as a hash.
-
#update(attributes) ⇒ void
Merge new attributes into the context attributes.
Constructor Details
#initialize(attributes) ⇒ AttributesHelper
Returns a new instance of AttributesHelper.
27 28 29 |
# File 'lib/lumberjack/attributes_helper.rb', line 27 def initialize(attributes) @attributes = attributes end |
Class Method Details
.expand_runtime_values(hash) ⇒ Hash
Expand any values in a hash that are Proc’s by calling them and replacing the value with the result. This allows setting global tags with runtime values.
12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/lumberjack/attributes_helper.rb', line 12 def (hash) return nil if hash.nil? return hash if hash.all? { |key, value| key.is_a?(String) && !value.is_a?(Proc) } copy = {} hash.each do |key, value| if value.is_a?(Proc) && (value.arity == 0 || value.arity == -1) value = value.call end copy[key.to_s] = value end copy end |
Instance Method Details
#[](name) ⇒ Object
Get a attribute value.
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/lumberjack/attributes_helper.rb', line 46 def [](name) return nil if @attributes.empty? name = name.to_s return @attributes[name] if @attributes.include?(name) # Check for partial matches in dot notation and return the hash representing the partial match. prefix_key = "#{name}." matching_attributes = {} @attributes.each do |key, value| if key.start_with?(prefix_key) # Remove the prefix to get the relative key relative_key = key[prefix_key.length..] matching_attributes[relative_key] = value end end return nil if matching_attributes.empty? matching_attributes end |
#[]=(name, value) ⇒ void
This method returns an undefined value.
Set a attribute value.
73 74 75 76 77 78 79 |
# File 'lib/lumberjack/attributes_helper.rb', line 73 def []=(name, value) if value.is_a?(Hash) @attributes.merge!(Utils.flatten_attributes(name => value)) else @attributes[name.to_s] = value end end |
#delete(*names) ⇒ void
This method returns an undefined value.
Remove attributes from the context.
85 86 87 88 89 90 91 |
# File 'lib/lumberjack/attributes_helper.rb', line 85 def delete(*names) names.each do |name| prefix_key = "#{name}." @attributes.delete_if { |k, _| k == name.to_s || k.start_with?(prefix_key) } end nil end |
#to_h ⇒ Hash
Return a copy of the attributes as a hash.
96 97 98 |
# File 'lib/lumberjack/attributes_helper.rb', line 96 def to_h @attributes.dup end |
#update(attributes) ⇒ void
This method returns an undefined value.
Merge new attributes into the context attributes. Attribute values will be flattened using dot notation on the keys. So { a: { b: ‘c’ } } will become { ‘a.b’ => ‘c’ }.
If a block is given, then the attributes will only be added for the duration of the block.
38 39 40 |
# File 'lib/lumberjack/attributes_helper.rb', line 38 def update(attributes) @attributes.merge!(Utils.flatten_attributes(attributes)) end |