Class: Trane::Registry::SnapshotBuilder

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

Overview

Builder collected inside a Registry::Instance#replace! block.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSnapshotBuilder

Returns a new instance of SnapshotBuilder.



31
32
33
34
35
# File 'lib/trane/registry.rb', line 31

def initialize
  @operations      = {}
  @representations = {}
  @errors          = {}
end

Class Method Details

.build_errors_by_name(errors) ⇒ Object

Build a frozen FQDN+short-name index over the given errors Hash. Raises Trane::Error on short-name collision between two distinct FQDNs. A pure function of its input — class-level so Instance#register_error can rebuild the index without allocating a builder.



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/trane/registry.rb', line 63

def self.build_errors_by_name(errors)
  return EMPTY_ERRORS_BY_NAME if errors.empty?

  index   = {}
  buckets = Hash.new { |h, k| h[k] = [] }

  errors.each do |fqdn, defn|
    index[fqdn] = defn
    short = fqdn.rpartition("::").last
    buckets[short] << defn unless short == fqdn
  end

  buckets.each do |short, defs|
    if defs.size >= 2
      fqdns   = defs.map(&:key).sort
      message = "Trane boot: error short-name collision on \"#{short}\".\n" \
                "Conflicting registrations: #{fqdns.join(', ')}.\n" \
                "Fix by renaming one, or by referencing them by FQDN in operation `errors` blocks " \
                "(e.g. key :\"#{fqdns.first}\")."
      raise Trane::Error, message
    end
    index[short] = defs.first unless index.key?(short)
  end

  index.freeze
end

Instance Method Details

#freeze_and_buildObject



49
50
51
52
53
54
55
56
57
# File 'lib/trane/registry.rb', line 49

def freeze_and_build
  errors_by_name = self.class.build_errors_by_name(@errors)
  {
    operations:      @operations.freeze,
    representations: @representations.freeze,
    errors:          @errors.freeze,
    errors_by_name:  errors_by_name
  }.freeze
end

#register_error(definition) ⇒ Object



45
46
47
# File 'lib/trane/registry.rb', line 45

def register_error(definition)
  @errors[definition.key] = definition
end

#register_operation(definition) ⇒ Object



37
38
39
# File 'lib/trane/registry.rb', line 37

def register_operation(definition)
  @operations[definition.name] = definition
end

#register_representation(definition) ⇒ Object



41
42
43
# File 'lib/trane/registry.rb', line 41

def register_representation(definition)
  @representations[definition.name] = definition
end