Module: Eco::Language::Klass::Builder
Instance Method Summary collapse
-
#new_class(name, inherits:, parent_space: nil) {|child_class| ... } ⇒ Class
If the class for
nameexists, it returns it.
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.
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 |