Module: Foobara::Namespace::NamespaceHelpers

Included in:
Module
Defined in:
foobara-0.2.2/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

.anon_sequence(class_name) ⇒ Object



167
168
169
170
# File 'foobara-0.2.2/projects/namespace/src/namespace_helpers.rb', line 167

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



144
145
146
147
148
# File 'foobara-0.2.2/projects/namespace/src/namespace_helpers.rb', line 144

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



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'foobara-0.2.2/projects/namespace/src/namespace_helpers.rb', line 150

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



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

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



134
135
136
137
138
139
140
141
142
# File 'foobara-0.2.2/projects/namespace/src/namespace_helpers.rb', line 134

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



108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'foobara-0.2.2/projects/namespace/src/namespace_helpers.rb', line 108

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



123
124
125
126
127
128
129
130
131
132
# File 'foobara-0.2.2/projects/namespace/src/namespace_helpers.rb', line 123

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



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'foobara-0.2.2/projects/namespace/src/namespace_helpers.rb', line 92

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



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

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
  # TODO: can't there be multiple namespace roots??
  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 Foobara::Util.start_with?(parent.scoped_full_path, mod.scoped_full_path)
    end

    next if scoped.scoped_full_path == mod.scoped_full_path

    if Foobara::Util.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)

      end
    end
  end
end

Instance Method Details

#foobara_autoregister_subclasses(default_namespace: nil) ⇒ Object



277
278
279
# File 'foobara-0.2.2/projects/namespace/src/namespace_helpers.rb', line 277

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

#foobara_autoset_namespace!(default_namespace: nil) ⇒ Object



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

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

#foobara_autoset_scoped_path!(make_top_level: false) ⇒ Object



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

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



273
274
275
# File 'foobara-0.2.2/projects/namespace/src/namespace_helpers.rb', line 273

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



265
266
267
# File 'foobara-0.2.2/projects/namespace/src/namespace_helpers.rb', line 265

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



281
282
283
# File 'foobara-0.2.2/projects/namespace/src/namespace_helpers.rb', line 281

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

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



269
270
271
# File 'foobara-0.2.2/projects/namespace/src/namespace_helpers.rb', line 269

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