Skip to content

Instantly share code, notes, and snippets.

@ktimothy
Created March 11, 2020 12:21
Show Gist options
  • Save ktimothy/efdec7cdea00a1c23a356d4e22ea6e63 to your computer and use it in GitHub Desktop.
Save ktimothy/efdec7cdea00a1c23a356d4e22ea6e63 to your computer and use it in GitHub Desktop.
call `using` dynamically in ruby
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