Module: Foobara::Namespace::IsNamespace

Includes:
Scoped
Included in:
Foobara::Namespace, NamespaceHelpers::InstancesAreNamespaces
Defined in:
foobara-0.0.110/projects/namespace/src/is_namespace.rb

Instance Attribute Summary

Attributes included from Scoped

#foobara_manifest_reference, #scoped_namespace

Instance Method Summary collapse

Methods included from Scoped

#scoped_absolute_name, #scoped_category, #scoped_full_name, #scoped_full_path, #scoped_ignore_module?, #scoped_ignore_modules=, #scoped_name, #scoped_name=, #scoped_path, #scoped_path=, #scoped_path_autoset=, #scoped_path_autoset?, #scoped_path_set?, #scoped_prefix, #scoped_short_name, #scoped_short_path

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name) ⇒ Object



266
267
268
269
270
271
272
273
274
275
276
277
# File 'foobara-0.0.110/projects/namespace/src/is_namespace.rb', line 266

def method_missing(method_name, *, **, &)
  filter, method, bang = _filter_from_method_name(method_name)

  if filter
    method = "foobara_#{method}#{bang ? "!" : ""}"
    send(method, *, **, filter:, &)
  else
    # :nocov:
    super
    # :nocov:
  end
end

Instance Method Details

#foobara_add_category(symbol, &block) ⇒ Object



29
30
31
# File 'foobara-0.0.110/projects/namespace/src/is_namespace.rb', line 29

def foobara_add_category(symbol, &block)
  @foobara_categories = { symbol.to_sym => block }.merge(foobara_categories)
end

#foobara_add_category_for_instance_of(symbol, klass) ⇒ Object



33
34
35
# File 'foobara-0.0.110/projects/namespace/src/is_namespace.rb', line 33

def foobara_add_category_for_instance_of(symbol, klass)
  foobara_add_category(symbol) { is_a?(klass) }
end

#foobara_add_category_for_subclass_of(symbol, klass) ⇒ Object



37
38
39
# File 'foobara-0.0.110/projects/namespace/src/is_namespace.rb', line 37

def foobara_add_category_for_subclass_of(symbol, klass)
  foobara_add_category(symbol) { is_a?(::Class) && self < klass }
end

#foobara_all(filter: nil, mode: Namespace::LookupMode::GENERAL) ⇒ Object



248
249
250
251
252
253
254
255
256
# File 'foobara-0.0.110/projects/namespace/src/is_namespace.rb', line 248

def foobara_all(filter: nil, mode: Namespace::LookupMode::GENERAL)
  all = []

  foobara_each(filter:, mode:) do |scoped|
    all << scoped
  end

  all
end

#foobara_categoriesObject



41
42
43
# File 'foobara-0.0.110/projects/namespace/src/is_namespace.rb', line 41

def foobara_categories
  @foobara_categories ||= foobara_parent_namespace&.foobara_categories || {}
end

#foobara_category_symbol_for(object) ⇒ Object



45
46
47
48
49
50
51
# File 'foobara-0.0.110/projects/namespace/src/is_namespace.rb', line 45

def foobara_category_symbol_for(object)
  foobara_categories.each_pair do |symbol, block|
    return symbol if object.instance_eval(&block)
  end

  nil
end

#foobara_childrenObject



57
58
59
# File 'foobara-0.0.110/projects/namespace/src/is_namespace.rb', line 57

def foobara_children
  @foobara_children ||= []
end

#foobara_depends_on_namespacesObject



69
70
71
# File 'foobara-0.0.110/projects/namespace/src/is_namespace.rb', line 69

def foobara_depends_on_namespaces
  @foobara_depends_on_namespaces ||= []
end

#foobara_each(filter: nil, mode: Namespace::LookupMode::GENERAL) ⇒ Object



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'foobara-0.0.110/projects/namespace/src/is_namespace.rb', line 230

def foobara_each(filter: nil, mode: Namespace::LookupMode::GENERAL, &)
  foobara_registry.each_scoped(filter:, &)

  return if mode == Namespace::LookupMode::DIRECT

  if [Namespace::LookupMode::GENERAL, Namespace::LookupMode::ABSOLUTE].include?(mode)
    foobara_children.each do |child|
      child.foobara_each(filter:, mode:, &)
    end
  end

  if mode == Namespace::LookupMode::GENERAL
    foobara_depends_on_namespaces.each do |dependent|
      dependent.foobara_each(filter:, mode:, &)
    end
  end
end

#foobara_lookup(path, filter: nil, mode: LookupMode::GENERAL, visited: Set.new, initial: true) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'foobara-0.0.110/projects/namespace/src/is_namespace.rb', line 97

def foobara_lookup(
  path,
  filter: nil,
  mode: LookupMode::GENERAL,
  visited: Set.new,
  initial: true
)
  path = Namespace.to_registry_path(path)
  visited_key = [path, mode, initial, self]
  return nil if visited.include?(visited_key)

  visited << visited_key

  LookupMode.validate!(mode)

  if mode == LookupMode::RELAXED
    scoped = foobara_lookup(
      path,
      filter:,
      mode: LookupMode::GENERAL,
      visited:,
      initial: false
    )
    return scoped if scoped

    candidates = foobara_children.map do |namespace|
      namespace.foobara_lookup(path, filter:, mode:, visited:, initial: false)
    end.compact

    if candidates.size > 1
      # :nocov:
      raise AmbiguousNameError,
            "#{path} is ambiguous. Matches the following: #{candidates.map(&:scoped_full_name)}"
      # :nocov:
    end

    return candidates.first ||
           foobara_parent_namespace&.foobara_lookup(path, filter:, mode:, visited:, initial: false)
  end

  if path[0] == ""
    if mode == LookupMode::DIRECT
      return nil unless scoped_full_name == ""

      path = path[1..]
    else
      path = path[(foobara_root_namespace.scoped_path.size + 1)..]
    end

    return foobara_root_namespace.foobara_lookup(path, filter:, mode: LookupMode::ABSOLUTE, initial: true)
  end

  partial = foobara_registry.lookup(path, filter)

  if mode == LookupMode::DIRECT
    return partial
  end

  if partial
    if partial.scoped_path == path
      return partial
    end
  end

  to_consider = [self]

  if mode != LookupMode::STRICT
    to_consider += foobara_children
  end

  scoped = _lookup_in(path, to_consider, filter:, visited:)
  return scoped if scoped

  if [LookupMode::GENERAL, LookupMode::STRICT].include?(mode) && foobara_parent_namespace
    scoped = foobara_parent_namespace.foobara_lookup(
      path, filter:, mode: LookupMode::STRICT, visited:, initial: false
    )
    return scoped if scoped
  end

  if mode == LookupMode::GENERAL
    scoped = _lookup_in(path, foobara_depends_on_namespaces, filter:, visited:)
    return scoped if scoped
  end

  to_consider = case mode
                when LookupMode::GENERAL
                  foobara_depends_on_namespaces
                else
                  []
                end

  candidates = to_consider.map do |namespace|
    namespace.foobara_lookup(path, filter:, mode:, visited:, initial: false)
  end.compact

  if candidates.size > 1
    # :nocov:
    raise AmbiguousLookupError, "Multiple things matched #{path}"
    # :nocov:
  end

  scoped = candidates.first || partial
  return scoped if scoped

  # As a last resort we'll see if we're trying to fetch a builtin type from a different namespace
  # TODO: these lookup modes are really confusing and were designed prior to having multiple root
  # namespaces playing a role in command connectors.
  if initial
    scoped = Namespace.global.foobara_lookup(path, filter:, mode: LookupMode::ABSOLUTE, visited:, initial: false)

    if scoped.is_a?(Types::Type) && scoped.builtin?
      scoped
    end
  end
end

#foobara_lookup!(name) ⇒ Object



218
219
220
221
222
223
224
225
226
227
228
# File 'foobara-0.0.110/projects/namespace/src/is_namespace.rb', line 218

def foobara_lookup!(name, **)
  object = foobara_lookup(name, **)

  unless object
    # :nocov:
    raise NotFoundError, "Could not find #{name}"
    # :nocov:
  end

  object
end

#foobara_parent_namespaceObject



214
215
216
# File 'foobara-0.0.110/projects/namespace/src/is_namespace.rb', line 214

def foobara_parent_namespace
  scoped_namespace
end

#foobara_parent_namespace=(namespace) ⇒ Object



24
25
26
27
# File 'foobara-0.0.110/projects/namespace/src/is_namespace.rb', line 24

def foobara_parent_namespace=(namespace)
  self.scoped_namespace = namespace
  scoped_namespace.foobara_children << self if namespace
end

#foobara_register(scoped) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'foobara-0.0.110/projects/namespace/src/is_namespace.rb', line 77

def foobara_register(scoped)
  foobara_registry.register(scoped)
  # awkward??
  scoped.scoped_namespace = self

  if scoped.respond_to?(:foobara_on_register)
    scoped.foobara_on_register
  end
rescue PrefixlessRegistry::RegisteringScopedWithPrefixError,
       BaseRegistry::WouldMakeRegistryAmbiguousError => e
  _upgrade_registry(e)
  foobara_register(scoped)
end

#foobara_registered?(path) ⇒ Boolean

Returns:

  • (Boolean)


258
259
260
261
262
263
264
# File 'foobara-0.0.110/projects/namespace/src/is_namespace.rb', line 258

def foobara_registered?(path, ...)
  if path.is_a?(Types::Type)
    return false unless path.scoped_path_set?
  end

  !foobara_lookup(path, ...).nil?
end

#foobara_registryObject



53
54
55
# File 'foobara-0.0.110/projects/namespace/src/is_namespace.rb', line 53

def foobara_registry
  @foobara_registry ||= Foobara::Namespace::PrefixlessRegistry.new
end

#foobara_root?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'foobara-0.0.110/projects/namespace/src/is_namespace.rb', line 73

def foobara_root?
  foobara_parent_namespace.nil?
end

#foobara_root_namespaceObject



61
62
63
64
65
66
67
# File 'foobara-0.0.110/projects/namespace/src/is_namespace.rb', line 61

def foobara_root_namespace
  ns = self

  ns = ns.foobara_parent_namespace until ns.foobara_root?

  ns
end

#foobara_unregister(scoped) ⇒ Object



91
92
93
94
95
# File 'foobara-0.0.110/projects/namespace/src/is_namespace.rb', line 91

def foobara_unregister(scoped)
  foobara_registry.unregister(scoped)
  foobara_children.delete(scoped)
  scoped.scoped_namespace = nil
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


279
280
281
# File 'foobara-0.0.110/projects/namespace/src/is_namespace.rb', line 279

def respond_to_missing?(method_name, include_private = false)
  !!_filter_from_method_name(method_name) || super
end

#scoped_clear_cachesObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'foobara-0.0.110/projects/namespace/src/is_namespace.rb', line 8

def scoped_clear_caches
  super

  foobara_each(&:scoped_clear_caches)

  if defined?(@foobara_categories)
    if @foobara_categories.empty?
      # :nocov:
      remove_instance_variable(:@foobara_categories)
      # :nocov:
    elsif scoped_namespace
      @foobara_categories = scoped_namespace.foobara_categories.merge(@foobara_categories)
    end
  end
end