Module: Foobara::Namespace::NamespaceHelpers

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


246
247
248
249
250
251
252
253
254
# File 'foobara-0.0.110/projects/namespace/src/namespace_helpers.rb', line 246

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

.foobara_autoregister_subclasses(klass, default_namespace: nil) ⇒ Object



141
142
143
144
145
# File 'foobara-0.0.110/projects/namespace/src/namespace_helpers.rb', line 141

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



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'foobara-0.0.110/projects/namespace/src/namespace_helpers.rb', line 147

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) ⇒ Object



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

def foobara_autoset_scoped_path(mod, make_top_level: false)
  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

    mod_name = [super_name, mod.object_id.to_s(16)].join("::")
  end

  scoped_path = mod_name.split("::")

  adjusted_scoped_path = []

  next_mod = Object

  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 = []
      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 mod.is_a?(IsNamespace)
    update_children_with_new_parent(mod)
  end
end

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



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

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



105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'foobara-0.0.110/projects/namespace/src/namespace_helpers.rb', line 105

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



120
121
122
123
124
125
126
127
128
129
# File 'foobara-0.0.110/projects/namespace/src/namespace_helpers.rb', line 120

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



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'foobara-0.0.110/projects/namespace/src/namespace_helpers.rb', line 89

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



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

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



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

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

#foobara_autoset_namespace!(default_namespace: nil) ⇒ Object



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

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

#foobara_autoset_scoped_path!(make_top_level: false) ⇒ Object



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

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



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

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



257
258
259
# File 'foobara-0.0.110/projects/namespace/src/namespace_helpers.rb', line 257

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



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

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

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



261
262
263
# File 'foobara-0.0.110/projects/namespace/src/namespace_helpers.rb', line 261

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