Created
October 27, 2017 12:11
-
-
Save Loschcode/5212ec3b8e570defa46cfb3ecd343fd3 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
def perform | |
if requests[request] | |
requests[request].response | |
end | |
# if requests.map(&:keys).include? request | |
# # it's a match | |
# end | |
end | |
def subclasses(target) | |
target.constants.map(&target.method(:const_get)).grep(Class).select { |c| c.to_s.include? "::#{target.to_s.split('::').last}" } #c.to_s =~ /^(.*::Schemes::[^::]+)$/ } | |
end | |
# git all the child request (messages / actions) | |
def requests | |
@requests ||= begin | |
subclasses(self.class).reduce({}) do |acc, subclass| | |
acc["#{subclass.new.request}"] = subclass | |
acc | |
end | |
end | |
end |
yeahboi
#newboi
# will get all the valid subclasses of this class, ignoring the irrelevant ones
class SubclassLoader
attr_reader :mainclass
def initialize(mainclass)
@mainclass = mainclass
end
def perform
valid_subclasses
end
private
def valid_subclasses
subclasses.select do |subclass|
valid_subclass? subclass
end
end
def subclasses
constants.map(&const_get).grep(Class)
end
def const_get
mainclass.method(:const_get)
end
def constants
mainclass.constants
end
def valid_subclass?(subclass)
subclass.to_s =~ /^(.*#{end_class}::[^::]+)$/ #subclass.to_s.include? "::#{end_class}"
end
def end_class
mainclass.to_s.split('::').last
end
end
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
fukyu