Module: Annotation::AnnotationModule

Defined in:
lib/scout/annotation/annotation_module.rb

Instance Method Summary collapse

Instance Method Details

#annotation(*attrs) ⇒ Object



4
5
6
7
8
9
10
11
# File 'lib/scout/annotation/annotation_module.rb', line 4

def annotation(*attrs)
  annotations = self.instance_variable_get("@annotations")
  attrs -= annotations
  annotations.concat attrs
  attrs.each do |a|
    self.attr_accessor a
  end
end

#annotationsObject



13
14
15
# File 'lib/scout/annotation/annotation_module.rb', line 13

def annotations
  @annotations ||= []
end

#extended(obj) ⇒ Object



25
26
27
28
29
30
31
32
33
34
# File 'lib/scout/annotation/annotation_module.rb', line 25

def extended(obj)
  attrs = self.instance_variable_get("@annotations")

  obj.instance_variable_set(:@annotations, []) unless obj.instance_variables.include?(:@annotations)
  obj.annotation_types << self

  annotations = obj.instance_variable_get(:@annotations)
  attrs -= annotations
  annotations.concat attrs
end

#included(mod) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/scout/annotation/annotation_module.rb', line 17

def included(mod)
  attrs = self.instance_variable_get("@annotations")
  mod.instance_variable_set(:@annotations, []) unless mod.instance_variables.include?(:@annotations)
  annotations = mod.instance_variable_get(:@annotations)
  attrs -= annotations
  annotations.concat attrs
end

#setup(*args, &block) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/scout/annotation/annotation_module.rb', line 36

def setup(*args,&block)
  if block_given?
    obj, rest = block, args
  else
    obj, *rest = args
  end
  obj = block if obj.nil?
  return nil if obj.nil?
  begin
    obj.extend self unless self === obj
  rescue TypeError
    return obj
  end
  attrs = self.instance_variable_get("@annotations")

  return obj if attrs.nil? || attrs.empty?

  if rest.length == 1 && Hash === (rlast = rest.last) && 
      ((! (rlkey = rlast.keys.first).nil? && attrs.include?(rlkey.to_sym)) ||
       (! attrs.length != 1 ))

    pairs = rlast
  else
    pairs = attrs.zip(rest)
  end

  pairs.each do |name,value|
    next if name.to_sym === :annotation_types
    obj.instance_variable_set("@#{name}", value) unless value.nil?
  end

  obj
end