Module: Roap::AttributeBase

Included in:
DigestExtension, LogExtension
Defined in:
lib/roap/roap.rb

Constant Summary collapse

@@original_methods =
{}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/roap/roap.rb', line 37

def self.extended base
  local_rules = []
  
  base.__send__ :define_singleton_method, :rules do 
    local_rules
  end

  Roap::extensions.push base
end

Instance Method Details

#attr(attr_name, &block) ⇒ Object



29
30
31
# File 'lib/roap/roap.rb', line 29

def attr attr_name, &block
  
end

#get_pure(base, method_name) ⇒ Object



33
34
35
# File 'lib/roap/roap.rb', line 33

def get_pure base, method_name
  @@original_methods["#{base}::#{method_name}"]
end

#included(base) ⇒ Object



46
47
48
49
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/roap/roap.rb', line 46

def included base
  targets = []

  base.singleton_methods(false).each do |method_name|
    method = get_pure base, method_name
    method ||= base.singleton_method(method_name)

    targets.push({
      :type => :singleton_method,
      :name => method_name,
      :pure => method })
  end
  base.instance_methods(false).each do |method_name|
    method = get_pure base, method_name
    method ||= base.instance_method(method_name)

    targets.push({
      :type => :instance_method,
      :name => method_name,
      :pure => method })
  end

  targets.each do |target|
    pure = target[:pure]
    method_name = target[:name]
    type = target[:type]
    attr = target[:pure].comment
    attrs = []
  
    body = Roap::Utils::decomment pure.comment
    mds = body.scan /(@([^\n]+)\n((\s\s+[^\n]*\n?)*))/m
    mds.each do |match, key, value|
      attrs.push({:key=>key, :value=>value})
    end

    rules.each do |rule|
      mds = Roap::Utils::scanex attr, rule[:expr]

      mds.each do |md|
        on_rules = Roap.class_variable_get :@@rules
        
        on_rules.each do |on_rule|
          on_md = rule[:attrs].match on_rule[:expr]
          if on_md != nil
            # refresh method

            method = base.__send__ type, method_name
            @@original_methods["#{base}::#{method.name}"] ||= method

            method.define_singleton_method :pure do 
              @@original_methods["#{base}::#{method.name}"]
            end
            method.define_singleton_method :attrs do 
              attrs
            end

            on_rule[:block].call base, method, md, rule                
          end
        end

        break if rule[:attrs].match /^#\s*once/
      end
    end
  end

end

#on(expr, &block) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/roap/roap.rb', line 20

def on expr, &block
  attrs = block.comment.empty? ?

    "runtime" : block.comment

  rules.push({
    :expr => expr,
    :block => block,
    :attrs => attrs})
end