Class: LittleGhost::DataMap

Inherits:
Hash
  • Object
show all
Defined in:
lib/little_ghost/data_map.rb

Overview

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 they remain easy to work with in Ruby and portable across session stores. 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.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value = {}) ⇒ DataMap

Builds a deeply normalized map from value.



22
23
24
25
# File 'lib/little_ghost/data_map.rb', line 22

def initialize(value = {})
  super()
  replace(value)
end

Class Method Details

.coerce(value) ⇒ Object

Returns value when it is already a DataMap, or normalizes a mapping.



28
29
30
31
32
# File 'lib/little_ghost/data_map.rb', line 28

def self.coerce(value)
  return value if value.is_a?(self)

  new(value)
end

Instance Method Details

#[](key) ⇒ Object

Looks up key after canonicalizing it to a String.



35
# File 'lib/little_ghost/data_map.rb', line 35

def [](key) = super(normalize_key(key))

#[]=(key, value) ⇒ Object Also known as: store

Stores value under the canonical String form of key.



38
39
40
# File 'lib/little_ghost/data_map.rb', line 38

def []=(key, value)
  super(normalize_key(key), normalize_value(value))
end

#delete(key, &block) ⇒ Object

Removes key after canonicalizing it to a String.



53
# File 'lib/little_ghost/data_map.rb', line 53

def delete(key, &block) = super(normalize_key(key), &block)

#dig(key, *names) ⇒ Object

Traverses nested DataMaps with String or Symbol keys.



56
# File 'lib/little_ghost/data_map.rb', line 56

def dig(key, *names) = super(normalize_key(key), *names)

#fetch(key, *defaults, &block) ⇒ Object

Fetches key with Hash#fetch's default and block behavior.



44
# File 'lib/little_ghost/data_map.rb', line 44

def fetch(key, *defaults, &block) = super(normalize_key(key), *defaults, &block)

#initialize_copy(other) ⇒ Object

Returns a deep independent DataMap copy.



81
82
83
84
# File 'lib/little_ghost/data_map.rb', line 81

def initialize_copy(other)
  super
  replace(other.to_h)
end

#key?(key) ⇒ Boolean Also known as: has_key?, include?, member?

Checks for key after canonicalizing it to a String.

Returns:

  • (Boolean)


47
# File 'lib/little_ghost/data_map.rb', line 47

def key?(key) = super(normalize_key(key))

#merge(other, &block) ⇒ Object

Returns a normalized copy merged with other.



59
60
61
# File 'lib/little_ghost/data_map.rb', line 59

def merge(other, &block)
  dup.merge!(other, &block)
end

#merge!(other) ⇒ Object Also known as: update

Merges other after deeply normalizing its keys and values.



64
65
66
67
68
69
# 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

#replace(other) ⇒ Object

Replaces all entries with a deeply normalized copy of other.



73
74
75
76
77
78
# 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

#to_hObject

Produces a deep ordinary Hash with canonical String keys.



87
88
89
# File 'lib/little_ghost/data_map.rb', line 87

def to_h
  plain_value(self)
end