Module: Format
- Included in:
- ACS, BMC, Bioinformatics, JTP, MLA
- Defined in:
- lib/format.rb
Defined Under Namespace
Classes: ACS, BMC, Bioinformatics, JTP, MLA
Constant Summary collapse
- Symbol_to_class_string =
{ }
- MediaForwarding =
{ :i => true, :b => true, :u => true, :header => true, :footer => true, :periodize => true, :par => true, :br => true, }
Class Method Summary collapse
Instance Method Summary collapse
-
#author_list(after_initials = '.', separate_last_and_initials = ' ', delim = ", ", and_word = "and", join_with_ands = false) ⇒ Object
probably only the first argument would you ever change if delim is nil, then et al.
-
#finish(arg) ⇒ Object
if given an array, will finish it with compaction and periodizing otherwise, won’t touch it.
- #format(cits) ⇒ Object
- #initialize(media_obj) ⇒ Object
- #method_missing(*args) ⇒ Object
-
#par(st) ⇒ Object
parenthesizes any ‘true’ object that has to_s method, otherwise ”.
-
#punctuate_initials(initials, punc = '.') ⇒ Object
The method should take an array of strings, each formatted in whatever method, and ensure that each string ends in a period.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(*args) ⇒ Object
24 25 26 27 28 29 30 31 32 33 |
# File 'lib/format.rb', line 24 def method_missing(*args) meth = args.first if MediaForwarding.key?(meth) @media_obj.send(*args) elsif @cit and @cit.respond_to?(meth) @cit.send(*args) else raise NoMethodError, "method '#{meth}' called with args (#{args[1..-1].join(',')})" end end |
Class Method Details
.new(media_obj, tp = :jtp) ⇒ Object
16 17 18 19 20 21 22 |
# File 'lib/format.rb', line 16 def self.new(media_obj, tp=:jtp) require "format/#{tp}" klass_st = ((x = Symbol_to_class_string[tp]) ? x : tp.to_s.capitalize) klass = Format.const_get(klass_st) include_super = true obj = klass.new(media_obj) end |
Instance Method Details
#author_list(after_initials = '.', separate_last_and_initials = ' ', delim = ", ", and_word = "and", join_with_ands = false) ⇒ Object
probably only the first argument would you ever change if delim is nil, then et al. format is used (1 author, fine, 2 authors connect with ‘and’, 3 authors = et al
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/format.rb', line 78 def (after_initials='.', separate_last_and_initials=' ', delim=", ", and_word="and", join_with_ands=false) if .is_a? String else names = [] names = .map do |auth| auth.last + separate_last_and_initials + punctuate_initials(auth.initials, after_initials) end if delim.nil? case .size when 1 names.first when 2 names.join(" #{and_word} ") else names.first + ' ' + i('et al.') end else if join_with_ands names[0...-1].join(delim) + " #{and_word} " + names[-1] else names.join(delim) end end end end |
#finish(arg) ⇒ Object
if given an array, will finish it with compaction and periodizing otherwise, won’t touch it
67 68 69 70 71 72 73 |
# File 'lib/format.rb', line 67 def finish(arg) if arg.is_a? Array periodize(arg.compact).join(' ') else arg end end |
#format(cits) ⇒ Object
57 58 59 60 61 62 63 |
# File 'lib/format.rb', line 57 def format(cits) as_strings = cits.map do |cit| @cit = cit finish(send(@cit.bibtype)) end @media_obj.list(as_strings) end |
#initialize(media_obj) ⇒ Object
35 36 37 38 |
# File 'lib/format.rb', line 35 def initialize(media_obj) @media_obj = media_obj @cit = nil end |
#par(st) ⇒ Object
parenthesizes any ‘true’ object that has to_s method, otherwise ”
110 111 112 113 114 115 116 |
# File 'lib/format.rb', line 110 def par(st) if st "(#{st})" else '' end end |
#punctuate_initials(initials, punc = '.') ⇒ Object
The method should take an array of strings, each formatted in whatever method, and ensure that each string ends in a period. This is annoying to define, but it simplifies the writing of citation formats dramatically def periodize(array)
array.map do |st|
if st[-1,1] == '.'
st
else
st << '.'
end
end
end
53 54 55 |
# File 'lib/format.rb', line 53 def punctuate_initials(initials, punc='.') initials.split('').map { |i| i + punc }.join('') end |