lib/dry/core/class_builder.rb
# Generate a class based on options
#
# @example Create anonymous class
#   builder = Dry::Core::ClassBuilder.new(name: 'MyClass')
#
#   klass = builder.call
#   klass.name # => "MyClass"
#
# @example Create named class
#   builder = Dry::Core::ClassBuilder.new(name: 'User', namespace: Entities)
#
#   klass = builder.call
#   klass.name # => "Entities::User"
#   klass.superclass.name # => "Entities::User"
#   Entities::User # => "Entities::User"
#   klass.superclass == Entities::User # => true
#
# @return [Class]
Generate a class based on options
Generate a class based on options
def call
        klass = if namespace
                  create_named
                else
                  create_anonymous
                end
        yield(klass) if block_given?
        klass
      end
def call klass = if namespace create_named else create_anonymous end yield ( klass ) if block_given? klass end
Generate a class based on options
Generate a class based on options
None
Class
example
Create anonymous class builder = Dry::Core::ClassBuilder.new(name: 'MyClass') klass = builder.call klass.name # => "MyClass"
Create anonymous class builder = Dry :: Core :: ClassBuilder . new ( name : ' MyClass ' ) klass = builder . call klass . name # = > " MyClass "
example
Create named class builder = Dry::Core::ClassBuilder.new(name: 'User', namespace: Entities) klass = builder.call klass.name # => "Entities::User" klass.superclass.name # => "Entities::User" Entities::User # => "Entities::User" klass.superclass == Entities::User # => true
Create named class builder = Dry :: Core :: ClassBuilder . new ( name : ' User ' namespace : Entities ) klass = builder . call klass . name # = > " Entities :: User " klass . superclass . name # = > " Entities :: User " Entities :: User # = > " Entities :: User " klass . superclass == Entities :: User # = > true