Class: WideEvent::Registry

Inherits:
Object
  • Object
show all
Defined in:
lib/wide_event/registry.rb

Overview

The attribute schema: every wide-event attribute is declared in a YAML registry (gem defaults merged with the app's registry.yml). Keys containing * are globs for dynamic attributes (feature_flag.*). In strict mode the accumulator reports every key it sees via #track; unregistered keys collect in #violations for the test suite to assert on.

Constant Summary collapse

DEFAULTS_PATH =
File.expand_path("registry/defaults.yml", __dir__)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(entries) ⇒ Registry

Returns a new instance of Registry.



30
31
32
33
34
35
36
# File 'lib/wide_event/registry.rb', line 30

def initialize(entries)
  @entries = entries
  @exact = entries.keys.reject { |k| k.include?("*") }.to_set
  @globs = entries.keys.select { |k| k.include?("*") }
  @violations = Set.new
  @mutex = Mutex.new
end

Instance Attribute Details

#entriesObject (readonly)

Returns the value of attribute entries.



28
29
30
# File 'lib/wide_event/registry.rb', line 28

def entries
  @entries
end

Class Method Details

.defaultsObject



13
14
15
# File 'lib/wide_event/registry.rb', line 13

def defaults
  new(load_file(DEFAULTS_PATH))
end

.load(path) ⇒ Object



17
18
19
# File 'lib/wide_event/registry.rb', line 17

def load(path)
  new(load_file(DEFAULTS_PATH).merge(load_file(path)))
end

Instance Method Details

#clear_violations!Object



52
53
54
55
# File 'lib/wide_event/registry.rb', line 52

def clear_violations!
  @mutex.synchronize { @violations.clear }
  nil
end

#registered?(key) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/wide_event/registry.rb', line 38

def registered?(key)
  @exact.include?(key) || @globs.any? { |glob| File.fnmatch(glob, key) }
end

#to_markdownObject



65
66
67
68
69
70
71
72
# File 'lib/wide_event/registry.rb', line 65

def to_markdown
  lines = [ "| Attribute | Type | Set by | PII | Notes |", "|---|---|---|---|---|" ]
  entries.each do |key, entry|
    entry = {} unless entry.is_a?(Hash)
    lines << "| `#{key}` | #{Array(entry["type"]).join(", ")} | #{entry["set_by"]} | #{entry["pii"]} | #{entry["notes"]} |"
  end
  lines.join("\n") + "\n"
end

#track(key) ⇒ Object



42
43
44
45
46
# File 'lib/wide_event/registry.rb', line 42

def track(key)
  return if registered?(key)
  @mutex.synchronize { @violations << key }
  nil
end

#validateObject



57
58
59
60
61
62
63
# File 'lib/wide_event/registry.rb', line 57

def validate
  entries.filter_map do |key, entry|
    unless entry.is_a?(Hash) && entry["type"]
      "#{key}: entry must be a mapping with at least a `type` field"
    end
  end
end

#violationsObject



48
49
50
# File 'lib/wide_event/registry.rb', line 48

def violations
  @violations.to_a
end