Module: Lucerna::Traits

Defined in:
lib/lucerna/traits.rb

Overview

Trait normalization. Traits are always Record<string, string> on the wire and in the engine — stringification happens at the boundary.

Class Method Summary collapse

Class Method Details

.normalize(traits) ⇒ Object

Lenient normalization for gates reads, which never raise: anything stringifiable passes.



26
27
28
29
30
# File 'lib/lucerna/traits.rb', line 26

def normalize(traits)
  return {} unless traits.is_a?(Hash)

  traits.each_with_object({}) { |(key, value), out| out[key.to_s] = value.to_s }
end

.normalize!(traits) ⇒ Object

Strict normalization for Identity construction / identify: bad input is a programming error and fails loud.

Raises:

  • (ArgumentError)


11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/lucerna/traits.rb', line 11

def normalize!(traits)
  raise ArgumentError, "traits must be a Hash (got #{traits.class})" unless traits.is_a?(Hash)

  traits.each_with_object({}) do |(key, value), out|
    unless value.is_a?(String) || value.is_a?(Symbol) || value.is_a?(Numeric) ||
        value.equal?(true) || value.equal?(false)
      raise ArgumentError,
        "trait #{key.inspect} must be a String, Symbol, Numeric, or boolean (got #{value.class})"
    end
    out[key.to_s] = value.to_s
  end
end