Guest User

Untitled

a guest
Jun 18th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. class PropertyHash < Hash
  2. def method_missing(name, *args, &block)
  3. return self[name.to_s] if include?(name.to_s) && args.size == 0
  4. return !!self[$1] if name.to_s =~ /^(.+)\?$/ && include?($1) && args.size == 0
  5. if name.to_s =~ /^(.+)\=$/ && include?($1) && args.size == 1
  6. self[$1] = args.first
  7. return
  8. end
  9. super
  10. end
  11.  
  12. def respond_to?(name)
  13. return true if include?(name.to_s)
  14. return true if name.to_s =~ /^(.+)[\?\=]$/ && include?($1)
  15. super
  16. end
  17. end
  18.  
  19.  
  20. class YourModel
  21. attr_accessor :access
  22.  
  23. def initialize
  24. @access = PropertyHash.new
  25. end
  26. end
  27.  
  28. def assert(msg = nil)
  29. raise "Assertion failed: #{msg || 'Unknown'}" unless yield
  30. end
  31.  
  32. obj = YourModel.new
  33.  
  34. obj.access['fault'] = :yes
  35.  
  36. assert('fault getter') do
  37. obj.access.fault == :yes
  38. end
  39.  
  40. assert('fault test') do
  41. obj.access.fault?
  42. end
  43.  
  44. assert('fault setter') do
  45. obj.access.fault = :no
  46. obj.access.fault == :no
  47. end
  48.  
  49. assert('responds to getter') do
  50. obj.access.respond_to?(:fault)
  51. end
  52.  
  53. assert('responds to setter') do
  54. obj.access.respond_to?(:fault=)
  55. end
  56.  
  57. assert('responds to test') do
  58. obj.access.respond_to?(:fault?)
  59. end
Add Comment
Please, Sign In to add comment