Class: Class

Inherits:
Object show all
Defined in:
lib/ctx.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#ctx_methodsObject

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_methodsObject



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_methodsObject



49
# File 'lib/ctx.rb', line 49

def defined_methods() class_methods | object_methods end

#object_methodsObject



47
# File 'lib/ctx.rb', line 47

def object_methods() self.instance_methods - Object.instance_methods end