Last active
December 11, 2020 11:26
-
-
Save a2ikm/47cf1f553b7ca0fa5212894c349701e2 to your computer and use it in GitHub Desktop.
Patching class methods in Ruby
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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