Skip to content

Instantly share code, notes, and snippets.

@a2ikm
Last active December 11, 2020 11:26
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 a2ikm/47cf1f553b7ca0fa5212894c349701e2 to your computer and use it in GitHub Desktop.
Save a2ikm/47cf1f553b7ca0fa5212894c349701e2 to your computer and use it in GitHub Desktop.
Patching class methods in Ruby
module Document
extend ActiveSupport::Concern
module ClassMethods
def key(name, type)
puts "a new key `#{name}` in `#{type}` is declared."
end
end
end
class User
include Document
key :name, Symbol
end
module Document
extend ActiveSupport::Concern
module ClassMethods
def key(name, type)
puts "a new key `#{name}` in `#{type}` is declared."
end
end
end
module Patch
module PatchedClassMethods
def key(name, type)
raise "Symbol type is deprecated" if type == Symbol
super
end
end
def included(model)
super
model.singleton_class.prepend(PatchedClassMethods)
end
end
Document.singleton_class.prepend(Patch)
class User
include Document
key :name, Symbol
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment