Module: Annotations::ClassMethods

Defined in:
lib/spiderfw/utils/annotations.rb

Instance Method Summary collapse

Instance Method Details

#__Object

Annotates the next method.



126
127
128
# File 'lib/spiderfw/utils/annotations.rb', line 126

def __()
    @annotator ||= Annotator.new(self).single
end

#___Object

Annotates all the following methods, until the end of the Class/Module.



131
132
133
# File 'lib/spiderfw/utils/annotations.rb', line 131

def ___()
    @annotator ||= Annotator.new(self).multiple
end

#annotate(method, name, *args) ⇒ Object

Explicitly annotates a method



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/spiderfw/utils/annotations.rb', line 149

def annotate(method, name, *args)
    @annotations ||= {}
    @annotations[method] ||= {}
    if (args.length == 0)
        @annotations[method][name] = true
    elsif (args[0].is_a?(Hash))
        @annotations[method][name] = args[0]
    else
        @annotations[method][name] = args
    end
    ann = find_defined_annotation(name)
    if (ann)
        ann.call(self, method, *args)
    end
end

#annotationsObject

Returns the @annotations Hash.



121
122
123
# File 'lib/spiderfw/utils/annotations.rb', line 121

def annotations
    @annotations
end

#define_annotation(name, &proc) ⇒ Object

Defines an annotation. The given block will be called whenever the “name” annotation is encountered; it will be passed the current Class, the annotated Method, and the annotation arguments.



167
168
169
170
171
172
173
174
175
# File 'lib/spiderfw/utils/annotations.rb', line 167

def define_annotation(name, &proc)
    @defined_annotations ||= {}
    @defined_annotations[name] = proc
    if (@annotations)
        @annotations.each do |method, n|
            proc.call(self, method, *@annotations[method][n]) if n == name
        end
    end
end

#defined_annotationsObject

Returns the Hash of defined annotations.



178
179
180
# File 'lib/spiderfw/utils/annotations.rb', line 178

def defined_annotations
    @defined_annotations
end

#find_defined_annotation(name) ⇒ Object

Searches for a defined annotation in class and ancestors.



183
184
185
186
187
188
189
190
191
# File 'lib/spiderfw/utils/annotations.rb', line 183

def find_defined_annotation(name)
    return nil if self.class == Module && !@defined_annotations
    k = self
    while (k != Object)
        return nil unless k < Annotations
        return k.defined_annotations[name] if k.defined_annotations && k.defined_annotations[name]
        k = k.superclass
    end
end

#inherited(subclass) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
# File 'lib/spiderfw/utils/annotations.rb', line 108

def inherited(subclass)
    if (@annotations)
        @annotations.each do |method, vals|
            vals.each do |k, args|
                args = [args] unless args.is_a?(Array)
                subclass.annotate(method, k, *args)
            end
        end
    end
    super
end

#method_added(name) ⇒ Object

:nodoc:



135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/spiderfw/utils/annotations.rb', line 135

def method_added(name) # :nodoc:
    return super unless @annotator
    @annotations ||= {}
    @annotations[name] ||= {}
    @annotator.pending.each do |key, args|
        annotate(name, key, *args)
    end
    @annotator.clear_pending
    # debugger
    # self.method(name).annotations = @annotations[name]
    super
end