Skip to content

Instantly share code, notes, and snippets.

@AMHOL

AMHOL/ex.rb Secret

Created May 11, 2016 22:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AMHOL/717fdae15ec9e97d05a41944a31bcee2 to your computer and use it in GitHub Desktop.
Save AMHOL/717fdae15ec9e97d05a41944a31bcee2 to your computer and use it in GitHub Desktop.
module Dry
module Types
REGISTERED_STRUCTS = Concurrent::Array.new
STRUCT_INHERITED = Struct.method(:inherited)
def self.register_class(klass, meth = :new)
type = Definition.new(klass).constructor do |*args, &block|
Object.const_get(klass.to_s).public_send(meth, *args, &block)
end
container.register(identifier(klass), type)
end
class Struct
def self.inherited(klass)
REGISTERED_STRUCTS << klass
STRUCT_INHERITED.call(klass)
end
def self.unregister
Dry::Types.container._container.delete(Dry::Types.identifier(self))
end
end
module Rails
class Railtie < ::Rails::Railtie
config.to_prepare do
REGISTERED_STRUCTS.uniq.each(&:unregister)
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment