Created
November 30, 2017 18:59
-
-
Save sirwolfgang/c759ea7805b6fb1926bb968022dc5989 to your computer and use it in GitHub Desktop.
This file contains 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
# frozen_string_literal: true | |
# @see https://gist.github.com/ryanlecompte/1283413 | |
module BeforeSingletonAction | |
extend ActiveSupport::Concern | |
class_methods do | |
def singleton_method_added(method_name) | |
return if before_singleton_actions.include?(method_name) || before_singleton_action_methods.include?(method_name) | |
add_before_singleton_action_to(method_name) | |
end | |
def before_singleton_action(method_name) | |
before_singleton_actions << method_name | |
end | |
def before_singleton_actions | |
@before_singleton_actions ||= [] | |
end | |
def before_singleton_action_methods | |
@before_singleton_action_methods ||= [] | |
end | |
def add_before_singleton_action_to(method_name) | |
before_singleton_action_methods << method_name | |
original_method = method(method_name) | |
define_singleton_method(method_name) do |*args, &block| | |
before_singleton_actions.each do |before_singleton_action| | |
method(before_singleton_action).call | |
end | |
original_method.call(*args, &block) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment