Module: Foobara::Namespace::NamespaceHelpers

Included in:
Module
Defined in:
foobara-0.0.130/projects/namespace/src/namespace_helpers.rb

Defined Under Namespace

Modules: AutoRegisterInstances, AutoRegisterSubclasses, InstancesAreNamespaces, ScopedDefaultNamespace, SubclassesAreNamespaces

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

._start_with?(large_array, small_array) ⇒ Boolean

TODO: move to util

Returns:

  • (Boolean)


273
274
275
276
277
278
279
280
281
# File 'foobara-0.0.130/projects/namespace/src/namespace_helpers.rb', line 273

def _start_with?(large_array, small_array)
  return false unless large_array.size > small_array.size

  small_array.each.with_index do |item, index|
    return false unless large_array[index] == item
  end

  true
end

.anon_sequence(class_name) ⇒ Object



173
174
175
176
# File 'foobara-0.0.130/projects/namespace/src/namespace_helpers.rb', line 173

def anon_sequence(class_name)
  @anon_sequences ||= {}
  @anon_sequences.key?(class_name) ? @anon_sequences[class_name] += 1 : @anon_sequences[class_name] = 1
end

.foobara_autoregister_subclasses(klass, default_namespace: nil) ⇒ Object



150
151
152
153
154
# File 'foobara-0.0.130/projects/namespace/src/namespace_helpers.rb', line 150

def foobara_autoregister_subclasses(klass, default_namespace: nil)
  klass.include ScopedDefaultNamespace unless klass < ScopedDefaultNamespace
  klass.scoped_default_namespace = default_namespace if default_namespace
  klass.include AutoRegisterSubclasses unless klass < AutoRegisterSubclasses
end

.foobara_autoset_namespace(mod, default_namespace: nil) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'foobara-0.0.130/projects/namespace/src/namespace_helpers.rb', line 156

def foobara_autoset_namespace(mod, default_namespace: nil)
  return if mod.scoped_namespace

  parent_mod = Util.module_for(mod)

  while parent_mod
    if parent_mod.is_a?(Foobara::Namespace::IsNamespace)
      mod.scoped_namespace = parent_mod
      return
    else
      parent_mod = Util.module_for(parent_mod)
    end
  end

  mod.scoped_namespace = default_namespace if default_namespace
end

.foobara_autoset_scoped_path(mod, make_top_level: false, set_namespace: false, namespace_default: nil) ⇒ Object



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
# File 'foobara-0.0.130/projects/namespace/src/namespace_helpers.rb', line 178

def foobara_autoset_scoped_path(mod, make_top_level: false, set_namespace: false, namespace_default: nil)
  return if mod.scoped_path_set?

  mod_name = mod.name

  if mod_name.nil?
    parent = mod.superclass
    super_name = nil

    begin
      super_name = parent.scoped_path_set? ? parent.scoped_full_name : parent.name
    end until super_name

    short_name = if mod.respond_to?(:symbol) && mod.symbol
                   mod.symbol.to_s
                 else
                   "Anon#{NamespaceHelpers.anon_sequence(super_name)}"
                 end

    mod_name = [super_name, short_name].join("::")
  end

  scoped_path = mod_name.split("::")

  adjusted_scoped_path = []

  next_mod = Object

  parent = namespace_default

  while next_mod
    path_part = scoped_path.shift

    break unless path_part

    next_mod = Util.constant_value(next_mod, path_part)

    if next_mod.is_a?(IsNamespace) && next_mod != mod && !make_top_level
      adjusted_scoped_path = []
      parent = next_mod
      next
    end

    adjusted_scoped_path << path_part unless mod.scoped_namespace&.scoped_ignore_module?(next_mod)
  end

  mod.scoped_path_autoset = true
  mod.scoped_path = adjusted_scoped_path

  if set_namespace
    mod.scoped_namespace = parent
  end

  if mod.is_a?(IsNamespace)
    update_children_with_new_parent(mod)
  end
end

.foobara_instances_are_namespaces!(klass, default_parent: nil, autoregister: nil) ⇒ Object



140
141
142
143
144
145
146
147
148
# File 'foobara-0.0.130/projects/namespace/src/namespace_helpers.rb', line 140

def foobara_instances_are_namespaces!(klass, default_parent: nil, autoregister: nil)
  klass.include ScopedDefaultNamespace unless klass < ScopedDefaultNamespace
  klass.scoped_default_namespace = default_parent if default_parent
  klass.include InstancesAreNamespaces unless klass < InstancesAreNamespaces

  if autoregister
    klass.include AutoRegisterInstances
  end
end

.foobara_namespace!(object, scoped_path: nil, ignore_modules: nil) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'foobara-0.0.130/projects/namespace/src/namespace_helpers.rb', line 114

def foobara_namespace!(object, scoped_path: nil, ignore_modules: nil)
  object.extend ::Foobara::Scoped

  if ignore_modules
    object.scoped_ignore_modules = ignore_modules
  end
  object.scoped_path = scoped_path if scoped_path

  object.extend ::Foobara::Namespace::IsNamespace

  if object.scoped_path_set?
    Namespace::NamespaceHelpers.update_children_with_new_parent(object)
  end
end

.foobara_subclasses_are_namespaces!(klass, default_parent: nil, autoregister: nil) ⇒ Object



129
130
131
132
133
134
135
136
137
138
# File 'foobara-0.0.130/projects/namespace/src/namespace_helpers.rb', line 129

def foobara_subclasses_are_namespaces!(klass, default_parent: nil, autoregister: nil)
  klass.include ScopedDefaultNamespace unless klass < ScopedDefaultNamespace
  klass.scoped_default_namespace = default_parent if default_parent

  klass.include SubclassesAreNamespaces unless klass < SubclassesAreNamespaces

  if autoregister
    foobara_autoregister_subclasses(klass)
  end
end

.initialize_foobara_namespace(namespace, scoped_name_or_path = nil, parent_namespace: nil) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'foobara-0.0.130/projects/namespace/src/namespace_helpers.rb', line 98

def initialize_foobara_namespace(namespace, scoped_name_or_path = nil, parent_namespace: nil)
  unless namespace.scoped_path_set?
    scoped_name_or_path = scoped_name_or_path.to_s if scoped_name_or_path.is_a?(::Symbol)

    if scoped_name_or_path.is_a?(::String)
      namespace.scoped_name = scoped_name_or_path
    elsif scoped_name_or_path.is_a?(::Array)
      namespace.scoped_path = scoped_name_or_path
    end
  end

  if namespace.scoped_path_set? && parent_namespace
    namespace.foobara_parent_namespace = parent_namespace
  end
end

.update_children_with_new_parent(mod) ⇒ Object



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
# File 'foobara-0.0.130/projects/namespace/src/namespace_helpers.rb', line 236

def update_children_with_new_parent(mod)
  if mod.scoped_full_path.empty?
    # hard to really know what we're trying to do here. Just bail out.
    return
  end

  # how to do this?
  # I guess we could just iterate over all objects and patch up any with matching prefixes?
  # is this expensive to have to look at all of them or no? I guess at least it's a one-time thing.
  # TODO: somehow look things up by prefixes since we know mod's path
  Foobara.foobara_root_namespace.foobara_each do |scoped|
    parent = scoped.scoped_namespace
    next if parent == mod

    if parent && !parent.scoped_full_path.empty?
      next if _start_with?(parent.scoped_full_path, mod.scoped_full_path)
    end

    if _start_with?(scoped.scoped_full_path, mod.scoped_full_path)
      scoped.scoped_path = scoped.scoped_full_path[mod.scoped_full_path.size..]

      if parent
        parent.foobara_unregister(scoped)

        mod.foobara_register(scoped)

        if scoped.is_a?(Namespace::IsNamespace)
          scoped.foobara_parent_namespace = mod
        else
          scoped.scoped_namespace = mod
        end
      end
    end
  end
end

Instance Method Details

#foobara_autoregister_subclasses(default_namespace: nil) ⇒ Object



296
297
298
# File 'foobara-0.0.130/projects/namespace/src/namespace_helpers.rb', line 296

def foobara_autoregister_subclasses(default_namespace: nil)
  NamespaceHelpers.foobara_autoregister_subclasses(self, default_namespace:)
end

#foobara_autoset_namespace!(default_namespace: nil) ⇒ Object



304
305
306
# File 'foobara-0.0.130/projects/namespace/src/namespace_helpers.rb', line 304

def foobara_autoset_namespace!(default_namespace: nil)
  NamespaceHelpers.foobara_autoset_namespace(self, default_namespace:)
end

#foobara_autoset_scoped_path!(make_top_level: false) ⇒ Object



308
309
310
# File 'foobara-0.0.130/projects/namespace/src/namespace_helpers.rb', line 308

def foobara_autoset_scoped_path!(make_top_level: false)
  NamespaceHelpers.foobara_autoset_scoped_path(self, make_top_level:)
end

#foobara_instances_are_namespaces!(default_parent: nil, autoregister: false) ⇒ Object



292
293
294
# File 'foobara-0.0.130/projects/namespace/src/namespace_helpers.rb', line 292

def foobara_instances_are_namespaces!(default_parent: nil, autoregister: false)
  NamespaceHelpers.foobara_instances_are_namespaces!(self, default_parent:, autoregister:)
end

#foobara_namespace!(scoped_path: nil, ignore_modules: nil) ⇒ Object



284
285
286
# File 'foobara-0.0.130/projects/namespace/src/namespace_helpers.rb', line 284

def foobara_namespace!(scoped_path: nil, ignore_modules: nil)
  NamespaceHelpers.foobara_namespace!(self, scoped_path:, ignore_modules:)
end

#foobara_root_namespace!(ignore_modules: nil) ⇒ Object



300
301
302
# File 'foobara-0.0.130/projects/namespace/src/namespace_helpers.rb', line 300

def foobara_root_namespace!(ignore_modules: nil)
  foobara_namespace!(scoped_path: [], ignore_modules:)
end

#foobara_subclasses_are_namespaces!(default_parent: nil, autoregister: false) ⇒ Object



288
289
290
# File 'foobara-0.0.130/projects/namespace/src/namespace_helpers.rb', line 288

def foobara_subclasses_are_namespaces!(default_parent: nil, autoregister: false)
  NamespaceHelpers.foobara_subclasses_are_namespaces!(self, default_parent:, autoregister:)
end