Class: LD4L::OpenAnnotationRDF::TagAnnotation

Inherits:
Annotation
  • Object
show all
Defined in:
lib/ld4l/open_annotation_rdf/tag_annotation.rb

Instance Method Summary collapse

Methods inherited from Annotation

find_by_target, #getBody, #persist!, resume, #setAnnotatedAtNow

Constructor Details

#initialize(*args) ⇒ TagAnnotation

Special processing for new and resumed TagAnnotations



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/ld4l/open_annotation_rdf/tag_annotation.rb', line 98

def initialize(*args)
  super(*args)

  # set motivatedBy
  m = get_values(:motivatedBy)
  m = m.to_a if Object::ActiveTriples.const_defined?("Relation") && m.kind_of?(ActiveTriples::Relation)
  set_value(:motivatedBy, RDF::Vocab::OA.tagging) unless m.kind_of?(Array) && m.size > 0

  # resume TagBody if it exists
  tag_uri = get_values(:hasBody).first
  if( tag_uri )
    tag_uri = tag_uri.rdf_subject  if tag_uri.kind_of?(ActiveTriples::Resource)
    @body  = LD4L::OpenAnnotationRDF::TagBody.new(tag_uri)
  end
end

Instance Method Details

#destroyObject



114
115
116
117
118
119
120
121
122
123
124
# File 'lib/ld4l/open_annotation_rdf/tag_annotation.rb', line 114

def destroy
  # TODO Determine behavior of destroy
  #   Behaviour Options
  #     * Always destroy TagAnnotation
  #     * Handling of TagBody
  #     **  If TagBody is used only by this TagAnnotation, destroy it.
  #     **  Otherwise, do not destroy it.
  # TODO Write tests for this behaviour.
  # TODO Write code here to enforce.
  super
end

#getTagObject

Get the value of the tag stored in a tag annotation.

Returns:

  • text value of tag



17
18
19
20
# File 'lib/ld4l/open_annotation_rdf/tag_annotation.rb', line 17

def getTag
  tags = @body.tag
  tags && tags.size > 0 ? tags.first : ""   # TODO What is the appropriate default value for a tag?
end

#setTag(tag) ⇒ Object

Set the hasBody property to the URI of the one and only TagBody holding the tag value. Create a new TagBody if one doesn’t exist with this value.

Parameters:

  • tag (String)

    value

Returns:

  • instance of TagBody

Raises:

  • (ArgumentError)


29
30
31
32
33
34
35
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
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
# File 'lib/ld4l/open_annotation_rdf/tag_annotation.rb', line 29

def setTag(tag)
  raise ArgumentError, 'Argument must be a string with at least one character'  unless tag.kind_of?(String) && tag.size > 0

  # return existing body if tag value is unchanged
  old_tag = @body ? @body.tag : nil
  return @body if old_tag && old_tag.include?(tag)

  if LD4L::OpenAnnotationRDF.configuration.unique_tags
    # when unique_tags = true, try to find an existing TagBody with the tag value before creating a new TagBody
    # TODO Determine behavior of setTag when unique_tags=true
    #   Behaviour Options:
    #     * Look for an existing TagBody with this value.
    #     **  If none found, create a new TagBody.
    #     **  If one found, set @body to this TagBody
    #     **  If multiple found, use the first one found
    #           ### the same one may not be the first one found each time the query executes
    if self.respond_to? 'persistence_strategy'  # >= ActiveTriples 0.8
      @body = LD4L::OpenAnnotationRDF.configuration.unique_tags ? LD4L::OpenAnnotationRDF::TagBody.fetch_by_tag_value(tag,self) : nil
    else
      @body = LD4L::OpenAnnotationRDF.configuration.unique_tags ? LD4L::OpenAnnotationRDF::TagBody.fetch_by_tag_value(tag) : nil
    end
    if @body == nil
      if self.respond_to? 'persistence_strategy'  # >= ActiveTriples 0.8
        @body = LD4L::OpenAnnotationRDF::TagBody.new(
          ActiveTriples::LocalName::Minter.generate_local_name(
              LD4L::OpenAnnotationRDF::TagBody, 10, @localname_prefix,
              LD4L::OpenAnnotationRDF.configuration.localname_minter ),self)
      else # < ActiveTriples 0.8
        @body = LD4L::OpenAnnotationRDF::TagBody.new(
            ActiveTriples::LocalName::Minter.generate_local_name(
                LD4L::OpenAnnotationRDF::TagBody, 10, @localname_prefix,
                LD4L::OpenAnnotationRDF.configuration.localname_minter ))
      end
      @body.tag = tag
    end
  else
    # when unique_tags = false, ???  (see TODO)
    # TODO Determine behavior of setTag when unique_tags=false
    #   Behaviour Options:
    #     * If this TagAnnotation does not have a TagBody (@body) set, then create a new TagBody.
    #     * If this TagBody is used only by this TagAnnotation, then change the value in the TagBody.
    #     * If this TagBody is used by multiple TagAnnotations,
    #     **  EITHER change the value in the TagBody which changes it for all the TagAnnotations.
    #           ### Likely an undesirable side effect having the value change for all TagAnnotations
    #     **  OR create a new TagBody and update @body to that TagBody
    #   OR
    #     * [CURRENT] Always create a new TagBody each time setTag is called and update @body
    #         ### This last options has the potential for orphaned TagBodys that no TagAnnotation references.
    # TODO Rethink the current behavior which is always to create a new TagBody potentially leaving around orphans.
    if self.respond_to? 'persistence_strategy'  # >= ActiveTriples 0.8
      @body = LD4L::OpenAnnotationRDF::TagBody.new(
          ActiveTriples::LocalName::Minter.generate_local_name(
              LD4L::OpenAnnotationRDF::TagBody, 10, @localname_prefix,
              LD4L::OpenAnnotationRDF.configuration.localname_minter ),self)
    else # < ActiveTriples 0.8
      @body = LD4L::OpenAnnotationRDF::TagBody.new(
          ActiveTriples::LocalName::Minter.generate_local_name(
              LD4L::OpenAnnotationRDF::TagBody, 10, @localname_prefix,
              LD4L::OpenAnnotationRDF.configuration.localname_minter ))
    end
    @body.tag = tag
  end
  set_value(:hasBody, @body)
  @body
end