I'd like to patch parts of an existing library with a config object:
module Library
class A
end
class B
end
end
module Config
def config(&block)
@config ||= block&.call
end
end
module Patch
refine Library::A.singleton_class do
extend Config
config { "my for A" }
end
refine Library::B.singleton_class do
extend Config
config { "my for B" }
end
end
Library::A
should have some predefined config and Library::B
should have its own config. Unfortunately, this throws an error:
using Patch
Library::A.config
=> undefined method `config' for Library::A:Class (NoMethodError)
I guess I don't understand how refinements work in this case. But is there a way to achieve something like this?
here is my solution, basically i
include Config
to thePatch
to be able to define configurations at class level, theninclude
thePatch
itself to the refined classes (Library::A and Library::B) to be able to access the defined configurations.then