Skip to content

Instantly share code, notes, and snippets.

@havenwood

havenwood/car.rb Secret

Last active November 8, 2019 23:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save havenwood/aba04eb85a746b91820ccf3eb772559d to your computer and use it in GitHub Desktop.
Save havenwood/aba04eb85a746b91820ccf3eb772559d to your computer and use it in GitHub Desktop.
Zero arity method example for yoshie902a
# frozen_string_literal: true
module Enforcement
def enforce_policy_level(level, meth)
original_meth = "__original_#{meth}"
alias_method original_meth, meth
define_method meth do
content = public_send original_meth
return content if current_user_level >= level
secure_text(content)
end
end
end
class Car
extend Enforcement
enforce_policy_level 1, def title
'Car Title'
end
private
def secure_text(text)
'X' * text.size
end
def current_user_level
rand 0..2
end
end
Car.new.title
#=> "XXXXXXXXX"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment