class LittleGhost::DataMap
DataMap holds JSON-compatible application data with indifferent key access. It stores every key as a String while accepting String and Symbol keys for lookup and mutation, including in nested maps.
state = DataMap.new(plan: {status: "active"}) state.dig("plan", :status) # => "active" state.to_h # => {"plan" => {"status" => "active"}}
State and metadata exposed by Sessions and RunContexts use DataMap so Ruby code can use either key form while session stores keep one portable shape. Values are limited to JSON primitives, Arrays, and mappings. A mapping that supplies both a String and Symbol form of the same key is ambiguous and raises ArgumentError.
Public Class Methods
Source
# File lib/little_ghost/data_map.rb, line 28 def self.coerce(value) return value if value.is_a?(self) new(value) end
Returns value when it is already a DataMap, or normalizes a mapping.
Source
# File lib/little_ghost/data_map.rb, line 22 def initialize(value = {}) super() replace(value) end
Builds a deeply normalized map from value.
Public Instance Methods
Source
# File lib/little_ghost/data_map.rb, line 35 def [](key) = super(normalize_key(key))
Looks up key after canonicalizing it to a String.
Source
# File lib/little_ghost/data_map.rb, line 38 def []=(key, value) super(normalize_key(key), normalize_value(value)) end
Stores value under the canonical String form of key.
Source
# File lib/little_ghost/data_map.rb, line 53 def delete(key, &block) = super(normalize_key(key), &block)
Removes key after canonicalizing it to a String.
Source
# File lib/little_ghost/data_map.rb, line 56 def dig(key, *names) = super(normalize_key(key), *names)
Traverses nested DataMaps with String or Symbol keys.
# File lib/little_ghost/data_map.rb, line 44 def fetch(key, *defaults, &block) = super(normalize_key(key), *defaults, &block)
Fetches key with Hash#fetch’s default and block behavior.
Source
# File lib/little_ghost/data_map.rb, line 81 def initialize_copy(other) super replace(other.to_h) end
Returns a deep independent DataMap copy.
Source
# File lib/little_ghost/data_map.rb, line 47 def key?(key) = super(normalize_key(key))
Checks for key after canonicalizing it to a String.
Source
# File lib/little_ghost/data_map.rb, line 59 def merge(other, &block) dup.merge!(other, &block) end
Returns a normalized copy merged with other.
# File lib/little_ghost/data_map.rb, line 64 def merge!(other) canonical_pairs(other).each do |key, value| self[key] = (block_given? && key?(key)) ? yield(key, self[key], value) : value end self end
Merges other after deeply normalizing its keys and values.
Source
# File lib/little_ghost/data_map.rb, line 73 def replace(other) pairs = canonical_pairs(other) clear pairs.each { |key, value| self[key] = value } self end
Replaces all entries with a deeply normalized copy of other.
Source
# File lib/little_ghost/data_map.rb, line 87 def to_h plain_value(self) end
Produces a deep ordinary Hash with canonical String keys.