Module: Annotation::AnnotatedObject

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.serialize(obj) ⇒ Object



23
24
25
# File 'lib/scout/annotation/annotated_object.rb', line 23

def self.serialize(obj)
  Annotation.purge(obj.annotation_info.merge(literal: obj))
end

Instance Method Details

#annotate(other) ⇒ Object



37
38
39
40
41
42
# File 'lib/scout/annotation/annotated_object.rb', line 37

def annotate(other)
  annotation_types.each do |type|
    type.setup(other, annotation_hash)
  end
  other
end

#annotation_hashObject



11
12
13
14
15
16
17
# File 'lib/scout/annotation/annotated_object.rb', line 11

def annotation_hash
  attr_hash = {}
  @annotations.each do |name|
    attr_hash[name] = self.instance_variable_get("@#{name}")
  end if @annotations
  attr_hash
end

#annotation_idObject Also known as: id



31
32
33
# File 'lib/scout/annotation/annotated_object.rb', line 31

def annotation_id
  Misc.digest([self, annotation_info])
end

#annotation_infoObject



19
20
21
# File 'lib/scout/annotation/annotated_object.rb', line 19

def annotation_info
  annotation_hash.merge(annotation_types: annotation_types, annotated_array: (AnnotatedArray === self))
end

#annotation_typesObject



7
8
9
# File 'lib/scout/annotation/annotated_object.rb', line 7

def annotation_types
  @annotation_types ||= []
end

#annotationsObject



3
4
5
# File 'lib/scout/annotation/annotated_object.rb', line 3

def annotations
  @annotations ||= []
end

#make_arrayObject



62
63
64
65
66
67
# File 'lib/scout/annotation/annotated_object.rb', line 62

def make_array
  new = [self]
  self.annotate(new)
  new.extend AnnotatedArray
  new
end

#purgeObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/scout/annotation/annotated_object.rb', line 44

def purge
  new = self.dup

  if new.instance_variables.include?(:@annotations)
    new.instance_variable_get(:@annotations).each do |a|
      var_name = "@#{a}".to_sym
      new.remove_instance_variable(var_name) if new.instance_variables.include? var_name
    end
    new.remove_instance_variable(:@annotations)
  end

  if new.instance_variables.include?(:@annotation_types)
    new.remove_instance_variable(:@annotation_types)
  end

  new
end

#serializeObject



27
28
29
# File 'lib/scout/annotation/annotated_object.rb', line 27

def serialize
  AnnotatedObject.serialize(self)
end