Module: Hyrax::CitationsBehaviors::NameBehavior

Includes:
CommonBehavior
Included in:
Hyrax::CitationsBehavior, Formatters::BaseFormatter
Defined in:
app/helpers/hyrax/citations_behaviors/name_behavior.rb

Instance Method Summary collapse

Methods included from CommonBehavior

#clean_end_punctuation, #persistent_url

Instance Method Details

#abbreviate_name(name) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/helpers/hyrax/citations_behaviors/name_behavior.rb', line 38

def abbreviate_name(name)
  abbreviated_name = ''
  name = name.join('') if name.is_a? Array

  # make sure we handle "Cher" correctly
  return name unless name.include?(' ') || name.include?(',')
  name = surname_first(name)
  name_segments = name.split(/,\s*/)
  abbreviated_name += name_segments.first
  abbreviated_name += ", #{name_segments.last.first}" if name_segments[1]
  abbreviated_name + "."
end

#all_authors(work, &block) ⇒ Object

return all unique authors of a work or nil if none



12
13
14
15
# File 'app/helpers/hyrax/citations_behaviors/name_behavior.rb', line 12

def all_authors(work, &block)
  authors = work.creator.uniq.compact
  block_given? ? authors.map(&block) : authors
end

#author_list(work) ⇒ Object

return all unique authors with end punctuation removed



7
8
9
# File 'app/helpers/hyrax/citations_behaviors/name_behavior.rb', line 7

def author_list(work)
  all_authors(work) { |author| clean_end_punctuation(CGI.escapeHTML(author)) }
end

#given_name_first(name) ⇒ Object



17
18
19
20
21
22
# File 'app/helpers/hyrax/citations_behaviors/name_behavior.rb', line 17

def given_name_first(name)
  name = clean_end_punctuation(name)
  return name unless name.include?(',')
  temp_name = name.split(/,\s*/)
  temp_name.last + " " + temp_name.first
end

#surname_first(name) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/helpers/hyrax/citations_behaviors/name_behavior.rb', line 24

def surname_first(name)
  name = name.join('') if name.is_a? Array
  # make sure we handle "Cher" correctly
  return name if name.include?(',')
  name_segments = name.split(' ')
  given_name = name_segments.first
  surnames = name_segments[1..-1]
  if surnames
    "#{surnames.join(' ')}, #{given_name}"
  else
    given_name
  end
end