Created
March 11, 2020 12:21
-
-
Save ktimothy/efdec7cdea00a1c23a356d4e22ea6e63 to your computer and use it in GitHub Desktop.
call `using` dynamically 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 Restrict | |
def ololo | |
module Restrictions | |
def allow(method_name, *classes) | |
# puts self.instance_methods(false) | |
# puts Model.instance_methods(false) | |
# puts Model.singleton_class.instance_methods(false) | |
puts self | |
self.instance_eval { alias_method("__#{method_name}", method_name) } | |
self.instance_eval { define_method(method_name) { raise 'not allowed' } } | |
model_class = self | |
classes.each do |klass| | |
refined = Module.new do | |
puts "in module #{self}" | |
refine model_class do | |
puts "in refine #{self}" | |
define_method(method_name) { send("__#{method_name}") } | |
define_method(:azaza) { puts "azazashed (#{self})" } | |
end | |
end | |
klass.send(:eval, 'using refined') | |
end | |
end | |
def self.included(klass) | |
puts "extended with " + klass.to_s | |
klass.extend Restrictions | |
end | |
def self.Allowed | |
puts 'here' | |
puts self | |
Module.new do | |
end | |
end | |
def self.__hash | |
@@hash ||= {} | |
end | |
def self.[](klass) | |
@@hash[klass] ||= {} | |
mod = Module.new do | |
end | |
mod | |
end | |
end | |
class Allowed | |
using Restrictions[Model] | |
def run | |
Model.new.azaza | |
Model.new.meth | |
end | |
end | |
class Denied | |
def run | |
Model.new.meth | |
end | |
end | |
class Model | |
include Restrictions | |
def meth | |
:ok | |
end | |
allow(:meth, Allowed) | |
end | |
puts Allowed.new.run | |
puts Denied.new.run | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment