Skip to content

Add a superclass/module for containers to allow defining containers as classes #7

Description

@woarewe

Currently, the only way to define a container is using the DSL:

Using a global container

F.define do
  namespace :hello do
    f(:world) { -> { "Hello, world!" } }
  end
end

Using a custom container

container = F::Container.new

F.define(container) do
 namespace :hello do
    f(:world) { -> { "Hello, world!" } }
  end
end

The goal is to make the following code work:

# using inheritance
class MyContainer < F::Container::DSL
  namespace :hello do
      f(:world) { -> { "Hello, world!" } }
  end
end

# or using extend, which I am leaning toward most

class MyContainer
  extend F::Container::DSL

  namespace :hello do
      f(:world) { -> { "Hello, world!" } }
  end
end

MyContainer['hello.world'].() #=> "Hello, world!"


class Service
  include MyContainer.import(:hello)

  def call
    f(:word)
  end
end

I was thinking about making fun_ruby fully compatible with the Rails environment and its autoloading features based on zeitwerk and the only way do that smoothly is to allow creating containers as constants

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions