Module: Foobara::Domain::ModuleExtension

Included in:
Module
Defined in:
foobara-0.0.110/projects/domain/src/module_extension.rb

Overview

TODO: should we just couple domain project and commands project to simplify this connection? TODO: move this stuff to extensions/ directory

Defined Under Namespace

Classes: CannotBeOrganizationAndDomainAtSameTime

Instance Method Summary collapse

Instance Method Details

#foobara_domain!Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'foobara-0.0.110/projects/domain/src/module_extension.rb', line 11

def foobara_domain!
  if foobara_organization?
    # :nocov:
    raise CannotBeOrganizationAndDomainAtSameTime
    # :nocov:
  end

  unless is_a?(Namespace::IsNamespace)
    foobara_namespace!
    foobara_autoset_namespace!(default_namespace: Foobara::GlobalOrganization)
    foobara_autoset_scoped_path!

    # TODO: wow this is awkward. We should find a cleaner way to set children on namespaces.
    parent = foobara_parent_namespace
    parent.foobara_register(self)
    self.foobara_parent_namespace = parent
  end

  include(DomainModuleExtension)

  children = foobara_children
  children = children.sort_by { |child| child.scoped_path.size }

  # see if we are upgrading from prefix to domain and copy over types to Types module
  children.each do |child|
    next unless child.is_a?(Types::Type)

    types_mod_path = scoped_full_path.dup
    unless child.scoped_path.first == "Types"
      types_mod_path << "Types"
    end

    types_mod_path += child.scoped_path[..-2]
    types_mod = Util.make_module_p(types_mod_path.join("::"))

    # TODO: dry this up
    # TODO: this doesn't handle a type nested under a lower-case type for now
    if child.scoped_short_name =~ /^[a-z]/
      unless types_mod.respond_to?(child.scoped_short_name)
        types_mod.singleton_class.define_method child.scoped_short_name do
          child
        end

        unless types_mod.instance_variable_defined?(:@foobara_lowercase_constants)
          # TODO: test this path or delete it if unreachable
          # :nocov:
          types_mod.instance_variable_set(:@foobara_lowercase_constants, [])
          # :nocov:
        end

        types_mod.instance_variable_get(:@foobara_lowercase_constants) << child.scoped_short_name
      end
    else
      value = if types_mod.const_defined?(child.scoped_short_name, false)
                # TODO: test this path or delete it if unreachable
                # :nocov:
                types_mod.const_get(child.scoped_short_name, false)
                # :nocov:
              end

      if value != child
        types_mod.send(:remove_const, child.scoped_short_name) if value
        # TODO: can we decouple this from the model project?
        new_value = if child.extends?("::model")
                      child.target_class
                    else
                      # TODO: test this path or delete it if unreachable
                      # :nocov:
                      child
                      # :nocov:
                    end
        types_mod.const_set(child.scoped_short_name, new_value)
      end
    end
  end
end

#foobara_domain?Boolean

Returns:

  • (Boolean)


109
110
111
# File 'foobara-0.0.110/projects/domain/src/module_extension.rb', line 109

def foobara_domain?
  false
end

#foobara_organization!Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'foobara-0.0.110/projects/domain/src/module_extension.rb', line 88

def foobara_organization!
  if foobara_domain?
    # :nocov:
    raise CannotBeOrganizationAndDomainAtSameTime
    # :nocov:
  end

  include(OrganizationModuleExtension)

  unless is_a?(Namespace::IsNamespace)
    foobara_namespace!
    self.scoped_namespace = Namespace.global
    foobara_autoset_scoped_path!(make_top_level: true)

    # TODO: wow this is awkward. We should find a cleaner way to set children on namespaces.
    parent = foobara_parent_namespace
    parent.foobara_register(self)
    self.foobara_parent_namespace = parent
  end
end

#foobara_organization?Boolean

Returns:

  • (Boolean)


113
114
115
# File 'foobara-0.0.110/projects/domain/src/module_extension.rb', line 113

def foobara_organization?
  false
end