Class: NxtRegistry::Registry

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

Direct Known Subclasses

RecursiveRegistry

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = object_id.to_s, **options, &config) ⇒ Registry

Returns a new instance of Registry.



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/nxt_registry/registry.rb', line 3

def initialize(name = object_id.to_s, **options, &config)
  @options = options
  @name = name
  @parent = options[:parent]
  @is_leaf = true
  @namespace = build_namespace
  @config = config
  @store = {}
  @configured = false
  @patterns = []
  @config = config
  @mutex = Mutex.new

  configure(&config)
end

Instance Attribute Details

#configuredObject

Returns the value of attribute configured.



20
21
22
# File 'lib/nxt_registry/registry.rb', line 20

def configured
  @configured
end

#mutexObject (readonly)

Returns the value of attribute mutex.



19
20
21
# File 'lib/nxt_registry/registry.rb', line 19

def mutex
  @mutex
end

#nameObject (readonly)

Returns the value of attribute name.



19
20
21
# File 'lib/nxt_registry/registry.rb', line 19

def name
  @name
end

Instance Method Details

#[](key) ⇒ Object



111
112
113
# File 'lib/nxt_registry/registry.rb', line 111

def [](key)
  resolve!(key)
end

#[]=(key, value) ⇒ Object



115
116
117
# File 'lib/nxt_registry/registry.rb', line 115

def []=(key, value)
  register(key, value)
end

#allowed_keys(*keys) ⇒ Object Also known as: attrs



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

def allowed_keys(*keys)
  @allowed_keys ||= []
  return @allowed_keys if keys.empty?

  @allowed_keys += keys.map { |key| transformed_key(key) }
end

#attr(key) ⇒ Object



67
68
69
# File 'lib/nxt_registry/registry.rb', line 67

def attr(key)
  allowed_keys(key) # @deprecated
end

#configure(&block) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/nxt_registry/registry.rb', line 142

def configure(&block)
  setup_defaults(options)
  define_accessors
  define_interface
  allowed_keys(*Array(options.fetch(:allowed_keys, [])))
  required_keys(*Array(options.fetch(:required_keys, [])))

  if block.present?
    if block.arity == 1
      instance_exec(self, &block)
    else
      instance_exec(&block)
    end
  end

  validate_required_keys_given
  self.configured = true
end

#exclude?(key) ⇒ Boolean

Returns:

  • (Boolean)


131
132
133
# File 'lib/nxt_registry/registry.rb', line 131

def exclude?(key)
  store.exclude?(transformed_key(key))
end

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



135
136
137
138
# File 'lib/nxt_registry/registry.rb', line 135

def fetch(key, *args, &block)
  key = matching_key(key)
  store.fetch(key, *args, &block)
end

#include?(key) ⇒ Boolean

Returns:

  • (Boolean)


127
128
129
# File 'lib/nxt_registry/registry.rb', line 127

def include?(key)
  store.include?(transformed_key(key))
end

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


123
124
125
# File 'lib/nxt_registry/registry.rb', line 123

def key?(key)
  store.key?(transformed_key(key))
end

#keysObject



119
120
121
# File 'lib/nxt_registry/registry.rb', line 119

def keys
  store.keys.map(&method(:transformed_key))
end

#level(name, **options, &config) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/nxt_registry/registry.rb', line 22

def level(name, **options, &config)
  options = options.merge(parent: self)

  if is_a_blank?(default)
    self.is_leaf = false

    self.default = RegistryBuilder.new do
      Registry.new(name, **options, &config)
    end

    # Call the builder once to guarantee we do not create a registry with a broken setup
    default.call
  elsif default.is_a?(RegistryBuilder)
    raise ArgumentError, 'Multiple nestings on the same level'
  else
    raise ArgumentError, 'Default values cannot be defined on registries that nest others'
  end
end

#register(key = Blank.new, value = Blank.new, **options, &block) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/nxt_registry/registry.rb', line 71

def register(key = Blank.new, value = Blank.new, **options, &block)
  if block_given?
    if is_a_blank?(value)
      registry(key, **options, &block)
    else
      raise_register_argument_error
    end
  else
    __register(key, value, raise_on_key_already_registered: true)
  end
end

#register!(key = Blank.new, value = Blank.new, **options, &block) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/nxt_registry/registry.rb', line 83

def register!(key = Blank.new, value = Blank.new, **options, &block)
  if block_given?
    if is_a_blank?(value)
      registry!(key, **options, &block)
    else
      raise_register_argument_error
    end
  else
    __register(key, value, raise_on_key_already_registered: false)
  end
end

#registry(name, **options, &config) ⇒ Object



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

def registry(name, **options, &config)
  opts = conditionally_inherit_options(options)
  register(name, Registry.new(name, **opts, &config))
end

#registry!(name, **options, &config) ⇒ Object



46
47
48
49
# File 'lib/nxt_registry/registry.rb', line 46

def registry!(name, **options, &config)
  opts = conditionally_inherit_options(options)
  register!(name, Registry.new(name, **opts, &config))
end

#required_keys(*keys) ⇒ Object



51
52
53
54
55
56
# File 'lib/nxt_registry/registry.rb', line 51

def required_keys(*keys)
  @required_keys ||= []
  return @required_keys if keys.empty?

  @required_keys += keys.map { |key| transformed_key(key) }
end

#resolve(*keys) ⇒ Object



101
102
103
104
105
# File 'lib/nxt_registry/registry.rb', line 101

def resolve(*keys)
  keys.inject(self) do |current_registry, key|
    current_registry.send(:__resolve, key, raise_on_key_not_registered: false) || break
  end
end

#resolve!(*keys) ⇒ Object



95
96
97
98
99
# File 'lib/nxt_registry/registry.rb', line 95

def resolve!(*keys)
  keys.inject(self) do |current_registry, key|
    current_registry.send(:__resolve, key, raise_on_key_not_registered: true)
  end
end

#to_hObject



107
108
109
# File 'lib/nxt_registry/registry.rb', line 107

def to_h
  store
end

#to_sObject Also known as: inspect



161
162
163
# File 'lib/nxt_registry/registry.rb', line 161

def to_s
  "Registry[#{name}] -> #{store.to_s}"
end