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 =
"1.0.0"

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

.change_the_subject_config_fileObject



95
96
97
# File 'lib/change_the_subject.rb', line 95

def self.change_the_subject_config_file
  File.join(File.dirname(__FILE__), "../", "config", "change_the_subject.yml")
end

.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

.config_yamlObject



88
89
90
91
92
93
# File 'lib/change_the_subject.rb', line 88

def self.config_yaml
  change_the_subject_erb = ERB.new(File.read(change_the_subject_config_file)).result
  YAML.safe_load(change_the_subject_erb, aliases: true)
rescue StandardError, SyntaxError => error
  raise Error, "#{change_the_subject_config_file} was found, but could not be parsed. \n#{error.inspect}"
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)


49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/change_the_subject.rb', line 49

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

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

  term
end

#check_for_replacement_subdivision(term:) ⇒ Object



64
65
66
67
68
69
70
71
# File 'lib/change_the_subject.rb', line 64

def check_for_replacement_subdivision(term:)
  separators.each do |separator|
    next unless term.include?(separator)

    return replace_subdivisions(term: term, separator: 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>)


34
35
36
37
38
39
40
41
42
43
# File 'lib/change_the_subject.rb', line 34

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

  subject_terms.compact.reject(&:empty?).map do |term|
    replacement = check_for_replacement(term: term)
    subdivision_replacement = check_for_replacement_subdivision(term: replacement)

    subdivision_replacement unless subdivision_replacement.empty?
  end.compact.uniq
end

#main_term_mappingObject



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

def main_term_mapping
  @main_term_mapping ||= config["main_term"]
end

#replace_subdivisions(term:, separator:) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/change_the_subject.rb', line 73

def replace_subdivisions(term:, separator:)
  subterms = term.split(separator)
  subterms.each.with_index do |sub_term, index|
    next if index.zero?

    clean_subterm = sub_term.delete_suffix(".")
    term_config = subdivision_term_mapping[clean_subterm]
    next unless term_config

    subterms[index] = term_config["replacement"]
  end

  subterms.join(separator)
end

#subdivision_term_mappingObject



27
28
29
# File 'lib/change_the_subject.rb', line 27

def subdivision_term_mapping
  @subdivision_term_mapping ||= config["subdivision"]
end