Module: Foobara::Namespace::IsNamespace
- Includes:
- Scoped
- Included in:
- Foobara::Namespace, NamespaceHelpers::InstancesAreNamespaces
- Defined in:
- foobara-0.2.2/projects/namespace/src/is_namespace.rb
Instance Attribute Summary
Attributes included from Scoped
#foobara_manifest_reference, #scoped_namespace, #unregistered_foobara_manifest_reference
Instance Method Summary collapse
- #foobara_add_category(symbol, &block) ⇒ Object
- #foobara_add_category_for_instance_of(symbol, klass) ⇒ Object
- #foobara_add_category_for_subclass_of(symbol, klass) ⇒ Object
- #foobara_all(filter: nil, mode: Namespace::LookupMode::GENERAL) ⇒ Object
- #foobara_categories ⇒ Object
- #foobara_category_symbol_for(object) ⇒ Object
- #foobara_children ⇒ Object
- #foobara_depends_on_namespaces ⇒ Object
- #foobara_each(filter: nil, mode: Namespace::LookupMode::GENERAL) ⇒ Object
- #foobara_lookup(path, filter: nil, mode: LookupMode::GENERAL) ⇒ Object
- #foobara_lookup!(name) ⇒ Object
- #foobara_lookup_without_cache(path, filter:, mode:, visited:) ⇒ Object
- #foobara_parent_namespace ⇒ Object
- #foobara_parent_namespace=(namespace) ⇒ Object
-
#foobara_register(scoped) ⇒ Object
TODO: make this thread-safe.
- #foobara_registered?(path) ⇒ Boolean
- #foobara_registry ⇒ Object
- #foobara_root? ⇒ Boolean
- #foobara_root_namespace ⇒ Object
- #foobara_unregister(scoped) ⇒ Object
- #foobara_unregister_all ⇒ Object
- #lru_cache ⇒ Object
- #method_missing(method_name) ⇒ Object
- #respond_to_missing?(method_name, include_private = false) ⇒ Boolean
- #scoped_clear_caches ⇒ Object
- #to_scoped(scopedish) ⇒ Object
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_reregistering!, #scoped_short_name, #scoped_short_path, #scoped_unregistered!, #scoped_unregistered?
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name) ⇒ Object
346 347 348 349 350 351 352 353 354 355 356 357 |
# File 'foobara-0.2.2/projects/namespace/src/is_namespace.rb', line 346 def method_missing(method_name, *, **, &) filter, method, bang = _filter_from_method_name(method_name) if filter method = "foobara_#{method}#{"!" if bang}" send(method, *, **, filter:, &) else # :nocov: super # :nocov: end end |
Instance Method Details
#foobara_add_category(symbol, &block) ⇒ Object
38 39 40 |
# File 'foobara-0.2.2/projects/namespace/src/is_namespace.rb', line 38 def (symbol, &block) @foobara_categories = { symbol.to_sym => block }.merge() end |
#foobara_add_category_for_instance_of(symbol, klass) ⇒ Object
42 43 44 |
# File 'foobara-0.2.2/projects/namespace/src/is_namespace.rb', line 42 def (symbol, klass) (symbol) { is_a?(klass) } end |
#foobara_add_category_for_subclass_of(symbol, klass) ⇒ Object
46 47 48 |
# File 'foobara-0.2.2/projects/namespace/src/is_namespace.rb', line 46 def (symbol, klass) (symbol) { is_a?(::Class) && self < klass } end |
#foobara_all(filter: nil, mode: Namespace::LookupMode::GENERAL) ⇒ Object
328 329 330 331 332 333 334 335 336 |
# File 'foobara-0.2.2/projects/namespace/src/is_namespace.rb', line 328 def (filter: nil, mode: Namespace::LookupMode::GENERAL) all = [] (filter:, mode:) do |scoped| all << scoped end all end |
#foobara_categories ⇒ Object
50 51 52 |
# File 'foobara-0.2.2/projects/namespace/src/is_namespace.rb', line 50 def @foobara_categories ||= &. || {} end |
#foobara_category_symbol_for(object) ⇒ Object
54 55 56 57 58 59 60 |
# File 'foobara-0.2.2/projects/namespace/src/is_namespace.rb', line 54 def (object) .each_pair do |symbol, block| return symbol if object.instance_eval(&block) end nil end |
#foobara_children ⇒ Object
66 67 68 |
# File 'foobara-0.2.2/projects/namespace/src/is_namespace.rb', line 66 def @foobara_children ||= [] end |
#foobara_depends_on_namespaces ⇒ Object
78 79 80 |
# File 'foobara-0.2.2/projects/namespace/src/is_namespace.rb', line 78 def @foobara_depends_on_namespaces ||= [] end |
#foobara_each(filter: nil, mode: Namespace::LookupMode::GENERAL) ⇒ Object
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 |
# File 'foobara-0.2.2/projects/namespace/src/is_namespace.rb', line 309 def (filter: nil, mode: Namespace::LookupMode::GENERAL, &) .each_scoped(filter:, &) if mode == LookupMode::GENERAL || mode == LookupMode::CHILDREN_ONLY || mode == LookupMode::ABSOLUTE || mode == LookupMode::ABSOLUTE_SINGLE_NAMESPACE .each do |child| child.(filter:, mode:, &) end end if mode == Namespace::LookupMode::GENERAL .each do |dependent| dependent.(filter:, mode:, &) end end end |
#foobara_lookup(path, filter: nil, mode: LookupMode::GENERAL) ⇒ Object
157 158 159 160 161 162 163 164 165 |
# File 'foobara-0.2.2/projects/namespace/src/is_namespace.rb', line 157 def (path, filter: nil, mode: LookupMode::GENERAL) LookupMode.validate!(mode) path = Namespace.to_registry_path(path) lru_cache.cached([self, path, mode, *filter]) do visited = Set.new (path, filter:, mode:, visited:) end end |
#foobara_lookup!(name) ⇒ Object
297 298 299 300 301 302 303 304 305 306 307 |
# File 'foobara-0.2.2/projects/namespace/src/is_namespace.rb', line 297 def (name, **) object = (name, **) unless object # :nocov: raise NotFoundError, "Could not find #{name}" # :nocov: end object end |
#foobara_lookup_without_cache(path, filter:, mode:, visited:) ⇒ Object
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 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 |
# File 'foobara-0.2.2/projects/namespace/src/is_namespace.rb', line 167 def (path, filter:, mode:, visited:) visited_key = [path, mode, self] return nil if visited.include?(visited_key) visited << visited_key if mode == LookupMode::RELAXED scoped = (path, filter:, mode: LookupMode::GENERAL, visited:) return scoped if scoped candidates = .map do |namespace| namespace.(path, filter:, mode:, visited:) end.compact if candidates.size > 1 # :nocov: raise AmbiguousNameError, "#{path} is ambiguous. Matches the following: #{candidates.map(&:scoped_full_name)}" # :nocov: end scoped = candidates.first || &.(path, filter:, mode:, visited:) return scoped end if path[0] == "" if mode == LookupMode::DIRECT return nil unless scoped_full_name == "" path = path[1..] else path = path[(.scoped_path.size + 1)..] end return (path, filter:, mode: LookupMode::ABSOLUTE, visited:) end root = if mode == LookupMode::ABSOLUTE [ root, *root..map(&:foobara_root_namespace) ].uniq.each do |namespace| scoped = namespace.(path, filter:, mode: LookupMode::CHILDREN_ONLY, visited:) return scoped if scoped end return nil end if mode == LookupMode::ABSOLUTE_SINGLE_NAMESPACE return .(path, filter:, mode: LookupMode::CHILDREN_ONLY, visited:) end partial = .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 += end scoped = _lookup_in(path, to_consider, filter:, visited:) if scoped return scoped end if (mode == LookupMode::GENERAL || mode == LookupMode::STRICT) && scoped = .( path, filter:, mode: LookupMode::STRICT, visited: ) if scoped return scoped end end if mode == LookupMode::GENERAL scoped = _lookup_in(path, , filter:, visited:) if scoped return scoped end end to_consider = case mode when LookupMode::GENERAL else [] end candidates = to_consider.map do |namespace| namespace.(path, filter:, mode:, visited:) end.compact if candidates.size > 1 # :nocov: raise AmbiguousLookupError, "Multiple things matched #{path}" # :nocov: end candidates.first || partial end |
#foobara_parent_namespace ⇒ Object
293 294 295 |
# File 'foobara-0.2.2/projects/namespace/src/is_namespace.rb', line 293 def scoped_namespace end |
#foobara_parent_namespace=(namespace) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'foobara-0.2.2/projects/namespace/src/is_namespace.rb', line 24 def (namespace) self.scoped_namespace = namespace if namespace if namespace..include?(self) # :nocov: raise "Already registered on parent" # :nocov: end namespace. << self end end |
#foobara_register(scoped) ⇒ Object
TODO: make this thread-safe
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 |
# File 'foobara-0.2.2/projects/namespace/src/is_namespace.rb', line 101 def (scoped) if scoped.scoped_unregistered? scoped.scoped_reregistering! end .register(scoped) if scoped.is_a?(Namespace::IsNamespace) scoped. = self else scoped.scoped_namespace = self end # TODO: do we really need to clear the whole cache? Why not just the possible # impacted keys based on scoped.scoped_path ? Namespace.clear_lru_cache! Namespace.fire_changed! if scoped. scoped. = nil end if scoped.respond_to?(:foobara_on_register) scoped. end rescue PrefixlessRegistry::RegisteringScopedWithPrefixError, BaseRegistry::WouldMakeRegistryAmbiguousError => e _upgrade_registry(e) (scoped) end |
#foobara_registered?(path) ⇒ Boolean
338 339 340 341 342 343 344 |
# File 'foobara-0.2.2/projects/namespace/src/is_namespace.rb', line 338 def (path, ...) if path.is_a?(Types::Type) return false unless path.scoped_path_set? end !(path, ...).nil? end |
#foobara_registry ⇒ Object
62 63 64 |
# File 'foobara-0.2.2/projects/namespace/src/is_namespace.rb', line 62 def @foobara_registry ||= Foobara::Namespace::PrefixlessRegistry.new end |
#foobara_root? ⇒ Boolean
82 83 84 |
# File 'foobara-0.2.2/projects/namespace/src/is_namespace.rb', line 82 def .nil? end |
#foobara_root_namespace ⇒ Object
70 71 72 73 74 75 76 |
# File 'foobara-0.2.2/projects/namespace/src/is_namespace.rb', line 70 def ns = self ns = ns. until ns. ns end |
#foobara_unregister(scoped) ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'foobara-0.2.2/projects/namespace/src/is_namespace.rb', line 132 def (scoped) scoped = to_scoped(scoped) .unregister(scoped) .delete(scoped) scoped. = scoped. scoped.scoped_namespace = nil scoped.scoped_unregistered! Namespace.clear_lru_cache! Namespace.fire_changed! end |
#foobara_unregister_all ⇒ Object
147 148 149 150 151 |
# File 'foobara-0.2.2/projects/namespace/src/is_namespace.rb', line 147 def .each_scoped do |child| (child) end end |
#lru_cache ⇒ Object
153 154 155 |
# File 'foobara-0.2.2/projects/namespace/src/is_namespace.rb', line 153 def lru_cache @lru_cache ||= Namespace.lru_cache end |
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
359 360 361 |
# File 'foobara-0.2.2/projects/namespace/src/is_namespace.rb', line 359 def respond_to_missing?(method_name, include_private = false) !!_filter_from_method_name(method_name) || super end |
#scoped_clear_caches ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'foobara-0.2.2/projects/namespace/src/is_namespace.rb', line 8 def scoped_clear_caches super (&: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..merge(@foobara_categories) end end end |
#to_scoped(scopedish) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'foobara-0.2.2/projects/namespace/src/is_namespace.rb', line 86 def to_scoped(scopedish) if scopedish.is_a?(::String) || scopedish.is_a?(::Symbol) || scopedish.is_a?(::Array) scopedish = (scopedish) unless scopedish # :nocov: raise ArgumentError, "Cannot unregister non-existent #{scopedish}" # :nocov: end end scopedish end |