Class: Class
Instance Attribute Summary collapse
-
#ctx_methods ⇒ Object
Returns the value of attribute ctx_methods.
Instance Method Summary collapse
Instance Attribute Details
#ctx_methods ⇒ Object
Returns the value of attribute ctx_methods.
48 49 50 |
# File 'lib/ctx.rb', line 48 def ctx_methods @ctx_methods end |
Instance Method Details
#ctx(context = :anonymous, &contextual) ⇒ Object
50 51 52 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 84 85 86 87 88 89 90 |
# File 'lib/ctx.rb', line 50 def ctx(context = :anonymous, &contextual) @ctx_methods ||= {} template = Template.new template.instance_exec(self, &contextual) matching = template.added matching.each do |method_name| if ctx_methods[method_name].nil? ctx_methods[method_name] = {} if instance_methods(true).includes? method_name ctx_methods[method_name][nil.sym] = instance_method(method_name) elsif singleton_methods(true).include? method_name ctx_methods[method_name][nil.sym] = singleton_class.instance_method(method_name) end end end self.class_eval(&contextual) matching.each do |method_name| if instance_methods(true).includes? method_name ctx_methods[method_name][context] = instance_method(method_name) define_method(method_name) do |*args| methods = self.class.ctx_methods[method_name] matched = @@contexts.return_first { |currentctx| methods[currentctx.name] } matched.bind(self).(*args) end elsif singleton_methods(true).include? method_name ctx_methods[method_name][context] = singleton_class.instance_method(method_name) define_singleton_method(method_name) do |*args| methods = self.ctx_methods[method_name] matched = @@contexts.return_first { |currentctx| methods[currentctx.name] } matched.bind(self).(*args) end end end end |