Class: ChangeTheSubject

Inherits:
Object
  • Object
show all
Defined in:
lib/change_the_subject.rb,
lib/change_the_subject/version.rb

Overview

The creation and management of metadata are not neutral activities.

Defined Under Namespace

Classes: Error

Constant Summary collapse

VERSION =
"0.3.6"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(separators: nil) ⇒ ChangeTheSubject

Returns a new instance of ChangeTheSubject.



19
20
21
# File 'lib/change_the_subject.rb', line 19

def initialize(separators: nil)
  @separators = separators || [""]
end

Instance Attribute Details

#separatorsObject (readonly)

Returns the value of attribute separators.



17
18
19
# File 'lib/change_the_subject.rb', line 17

def separators
  @separators
end

Class Method Details

.check_for_replacement(term:, separators: nil) ⇒ Object



13
14
15
# File 'lib/change_the_subject.rb', line 13

def self.check_for_replacement(term:, separators: nil)
  new(separators: separators).check_for_replacement(term: term)
end

.fix(subject_terms:, separators: nil) ⇒ Object



9
10
11
# File 'lib/change_the_subject.rb', line 9

def self.fix(subject_terms:, separators: nil)
  new(separators: separators).fix(subject_terms: subject_terms)
end

Instance Method Details

#check_for_replacement(term:) ⇒ String

Given a term, check whether there is a suggested replacement. If there is, return it. If there is not, return the term unaltered.

Parameters:

  • term (String)

Returns:

  • (String)


46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/change_the_subject.rb', line 46

def check_for_replacement(term:)
  separators.each do |separator|
    subterms = term.split(separator)
    replacement = replacement_config_for_subterms(subterms)
    next unless replacement

    new_terms = replacement_terms(replacement)
    return subterms.drop(new_terms.count)
                   .prepend(new_terms)
                   .join(separator)
  end

  term
end

#fix(subject_terms:) ⇒ <String>

Given an array of subject terms, replace the ones that need replacing

Parameters:

  • subject_terms (<String>)

Returns:

  • (<String>)


30
31
32
33
34
35
36
37
38
39
40
# File 'lib/change_the_subject.rb', line 30

def fix(subject_terms:)
  return [] if subject_terms.nil?

  subject_terms = subject_terms.compact.reject(&:empty?)
  return [] if subject_terms.empty? || subject_terms.nil?

  subject_terms.map do |term|
    replacement = check_for_replacement(term: term)
    replacement unless replacement.empty?
  end.compact.uniq
end

#terms_mappingObject



23
24
25
# File 'lib/change_the_subject.rb', line 23

def terms_mapping
  @terms_mapping ||= config
end