Class: Class
Instance Attribute Summary collapse
-
#ctx_methods ⇒ Object
Returns the value of attribute ctx_methods.
Instance Method Summary collapse
- #class_methods ⇒ Object
- #ctx(context = :anonymous, &contextual) ⇒ Object
- #defined_methods ⇒ Object
- #object_methods ⇒ Object
Instance Attribute Details
#ctx_methods ⇒ Object
Returns the value of attribute ctx_methods.
51 52 53 |
# File 'lib/ctx.rb', line 51 def ctx_methods @ctx_methods end |
Instance Method Details
#class_methods ⇒ Object
48 |
# File 'lib/ctx.rb', line 48 def class_methods() self.singleton_methods - Object.singleton_methods end |
#ctx(context = :anonymous, &contextual) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/ctx.rb', line 53 def ctx(context = :anonymous, &contextual) @ctx_methods ||= {} template = Class.new template.class_eval(&contextual) matching = self.object_methods & template.object_methods matching.each do |method_name| if ctx_methods[method_name].nil? ctx_methods[method_name] = {} ctx_methods[method_name][nil.sym] = instance_method(method_name) end end self.class_eval(&contextual) template.object_methods.each do |method_name| if ctx_methods[method_name].nil? ctx_methods[method_name] = {} end ctx_methods[method_name][context] = instance_method(method_name) define_method(method_name) do |*args| methods = self.class.ctx_methods[method_name] matched = @@contexts.reverse.return_first { |currentctx| methods[currentctx.name] } matched.bind(self).(*args) end end end |
#defined_methods ⇒ Object
49 |
# File 'lib/ctx.rb', line 49 def defined_methods() class_methods | object_methods end |