Class: Dor::TagService

Inherits:
Object
  • Object
show all
Defined in:
lib/dor/services/tag_service.rb

Overview

Manage tags on an object

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(item) ⇒ TagService

Returns a new instance of TagService.



18
19
20
# File 'lib/dor/services/tag_service.rb', line 18

def initialize(item)
  @item = item
end

Class Method Details

.add(item, tag) ⇒ Object



6
7
8
# File 'lib/dor/services/tag_service.rb', line 6

def self.add(item, tag)
  new(item).add(tag)
end

.remove(item, tag) ⇒ Object



10
11
12
# File 'lib/dor/services/tag_service.rb', line 10

def self.remove(item, tag)
  new(item).remove(tag)
end

.update(item, old_tag, new_tag) ⇒ Object



14
15
16
# File 'lib/dor/services/tag_service.rb', line 14

def self.update(item, old_tag, new_tag)
  new(item).update(old_tag, new_tag)
end

Instance Method Details

#add(tag) ⇒ Object

Add an administrative tag to an item, you will need to seperately save the item to write it to fedora

Parameters:

  • tag (string)

    The tag you wish to add



24
25
26
27
# File 'lib/dor/services/tag_service.rb', line 24

def add(tag)
  normalized_tag = validate_and_normalize_tag(tag, .tags)
  .add_value(:tag, normalized_tag)
end

#remove(tag) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/dor/services/tag_service.rb', line 29

def remove(tag)
  normtag = normalize_tag(tag)
  tag_nodes
    .select { |node| normalize_tag(node.content) == normtag }
    .each { .ng_xml_will_change! }
    .each(&:remove)
    .any?
end

#update(old_tag, new_tag) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/dor/services/tag_service.rb', line 38

def update(old_tag, new_tag)
  normtag = normalize_tag(old_tag)
  tag_nodes
    .select { |node| normalize_tag(node.content) == normtag }
    .each { .ng_xml_will_change! }
    .each { |node| node.content = normalize_tag(new_tag) }
    .any?
end