Class: ROBundle::Annotation

Inherits:
ManifestEntry show all
Defined in:
lib/ro-bundle/ro/annotation.rb

Overview

A class to represent an Annotation in a Research Object.

Instance Method Summary collapse

Methods inherited from ManifestEntry

#edited?, #stored

Methods included from Provenance

#add_author, #authored_by, #authored_on, #authored_on=, #created_by, #created_by=, #created_on, #created_on=, #remove_author, #retrieved_by, #retrieved_by=, #retrieved_from, #retrieved_from=, #retrieved_on, #retrieved_on=

Constructor Details

#initialize(object, content = nil) ⇒ Annotation

:call-seq:

new(target, content = nil)

Create a new Annotation with the specified “about” identifier. A new annotation ID is generated and set for the new annotation. The content parameter can be optionally used to set the file or URI that holds the body of the annotation.

An annotation id is a UUID prefixed with “urn:uuid” as per RFC4122.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/ro-bundle/ro/annotation.rb', line 25

def initialize(object, content = nil)
  super()

  if object.instance_of?(Hash)
    @structure = object
    @structure[:about] = [*@structure[:about]]
    init_provenance_defaults(@structure)
  else
    @structure = {}
    @structure[:about] = [*object]
    @structure[:uri] = UUID.generate(:urn)
    @structure[:content] = content
  end
end

Instance Method Details

#add_target(add) ⇒ Object

:call-seq:

add_target(new_target, ...) -> added target(s)

Add a new target, or targets, to this annotation.

The target(s) added are returned.



67
68
69
70
71
72
# File 'lib/ro-bundle/ro/annotation.rb', line 67

def add_target(add)
  @structure[:about] += [*add]

  @edited = true
  add
end

#annotates?(target) ⇒ Boolean

:call-seq:

annotates?(target) -> true or false

Does this annotation object annotate the supplied target?

Returns:

  • (Boolean)


44
45
46
# File 'lib/ro-bundle/ro/annotation.rb', line 44

def annotates?(target)
  @structure[:about].include?(target)
end

#contentObject

:call-seq:

content

The identifier for a resource that contains the body of the annotation.



93
94
95
# File 'lib/ro-bundle/ro/annotation.rb', line 93

def content
  @structure[:content]
end

#content=(new_content) ⇒ Object

:call-seq:

content = new_content

Set the content of this annotation.



101
102
103
104
# File 'lib/ro-bundle/ro/annotation.rb', line 101

def content=(new_content)
  @edited = true
  @structure[:content] = new_content
end

#remove_target(remove) ⇒ Object

:call-seq:

remove_target(target) -> target or nil

Remove a target from this annotation. An annotation must always have a target so this method will do nothing if it already has only one target.

If the target can be removed then it is returned, otherwise nil is returned.



82
83
84
85
86
87
# File 'lib/ro-bundle/ro/annotation.rb', line 82

def remove_target(remove)
  return if @structure[:about].length == 1

  @edited = true
  @structure[:about].delete(remove)
end

#targetObject

:call-seq:

target -> String or Array

The identifier(s) for the annotated resource. This is considered the target of the annotation, that is the resource (or resources) the annotation content is “somewhat about”.

The target can either be a singleton or a list of targets.



56
57
58
59
# File 'lib/ro-bundle/ro/annotation.rb', line 56

def target
  about = @structure[:about]
  about.length == 1 ? about[0] : about.dup
end

#to_json(*a) ⇒ Object

:call-seq:

to_json(options = nil) -> String

Write this Annotation out as a json string. Takes the same options as JSON#generate.



119
120
121
122
123
# File 'lib/ro-bundle/ro/annotation.rb', line 119

def to_json(*a)
  cleaned = Util.clean_json(@structure)
  cleaned[:about] = target
  JSON.generate(cleaned,*a)
end

#uriObject

:call-seq:

uri -> String in the form of a urn:uuid URI.

Return the annotation id of this Annotation.



110
111
112
# File 'lib/ro-bundle/ro/annotation.rb', line 110

def uri
  @structure[:uri]
end