access.rb 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. require 'set'
  2. module V8
  3. class Access
  4. def get(obj, name, &dontintercept)
  5. methods = accessible_methods(obj)
  6. if methods.include?(name)
  7. method = obj.method(name)
  8. method.arity == 0 ? method.call : method.unbind
  9. elsif obj.respond_to?(:[])
  10. obj.send(:[], name, &dontintercept)
  11. else
  12. yield
  13. end
  14. end
  15. def iget(obj, index, &dontintercept)
  16. if obj.respond_to?(:[])
  17. obj.send(:[], index, &dontintercept)
  18. else
  19. yield
  20. end
  21. end
  22. def set(obj, name, value, &dontintercept)
  23. setter = name + "="
  24. methods = accessible_methods(obj, true)
  25. if methods.include?(setter)
  26. obj.send(setter, value)
  27. elsif obj.respond_to?(:[]=)
  28. obj.send(:[]=, name, value, &dontintercept)
  29. else
  30. yield
  31. end
  32. end
  33. def iset(obj, index, value, &dontintercept)
  34. if obj.respond_to?(:[]=)
  35. obj.send(:[]=, index, value, &dontintercept)
  36. else
  37. yield
  38. end
  39. end
  40. def query(obj, name, attributes)
  41. if obj.respond_to?(name)
  42. attributes.dont_delete
  43. unless obj.respond_to?(name + "=")
  44. attributes.read_only
  45. end
  46. else
  47. yield
  48. end
  49. end
  50. def iquery(obj, index, attributes)
  51. if obj.respond_to?(:[])
  52. attributes.dont_delete
  53. unless obj.respond_to?(:[]=)
  54. attributes.read_only
  55. end
  56. else
  57. yield
  58. end
  59. end
  60. def names(obj)
  61. accessible_methods(obj)
  62. end
  63. def indices(obj)
  64. obj.respond_to?(:length) ? (0..obj.length).to_a : yield
  65. end
  66. private
  67. def accessible_methods(obj, special_methods = false)
  68. obj.public_methods(false).map {|m| m.to_s}.to_set.tap do |methods|
  69. ancestors = obj.class.ancestors.dup
  70. while ancestor = ancestors.shift
  71. break if ancestor == ::Object
  72. methods.merge(ancestor.public_instance_methods(false).map {|m| m.to_s})
  73. end
  74. methods.reject! {|m| m == "[]" || m == "[]=" || m =~ /=$/} unless special_methods
  75. end
  76. end
  77. end
  78. end