Module: Eco::Language::Klass::Builder

Includes:
Naming, Resolver
Included in:
HelpersBuilt
Defined in:
lib/eco/language/klass/builder.rb

Instance Method Summary collapse

Methods included from Naming

#instance_variable_name, #to_constant

Methods included from Resolver

#class_resolver, #resolve_class

Instance Method Details

#new_class(name, inherits:, parent_space: nil) {|child_class| ... } ⇒ Class

If the class for name exists, it returns it. Otherwise it generates it.

Parameters:

  • name (String, Symbol)

    the name of the new class

  • inherits (Class)

    the parent class to inherit from

  • parent_space (String) (defaults to: nil)

    parent namespace of the generated class, if not given: self

Yields:

  • (child_class)

    configure the new class

Yield Parameters:

  • child_class (Class)

    the new class

Returns:

  • (Class)

    the new generated class



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/eco/language/klass/builder.rb', line 13

def new_class(name, inherits:, parent_space: nil)
  name            = name.to_sym.freeze
  class_name      = to_constant(name)
  parent_space    = parent_space ? resolve_class(parent_space) : self
  full_class_name = "#{parent_space}::#{class_name}"

  unless (target_class = resolve_class(full_class_name, exception: false))
    target_class = Class.new(inherits)
    parent_space.const_set class_name, target_class
  end

  target_class.tap do |klass|
    yield(klass) if block_given?
  end
end