Class: Scripref::Bookname
- Inherits:
-
Object
- Object
- Scripref::Bookname
- Defined in:
- lib/scripref/bookname.rb
Instance Attribute Summary collapse
-
#book_id ⇒ Object
readonly
Returns the value of attribute book_id.
Class Method Summary collapse
-
.parse(s) ⇒ Object
Helper method to create Bookname instances from a String format: [book_id]: [full name]|[abbrev1]|[abbrev2]|...(, [alternative name]|[abbrev1],...]*.
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#dump ⇒ Object
Convert the instance to an equivalent string representation.
-
#each_name ⇒ Object
Enumerator over all names (name and name of alternatives).
-
#each_string ⇒ Object
Enumerator over all names and abbreviations (including alternatives).
-
#format(abbrev_level: 0) ⇒ Object
Format the bookname (full name or abbreviation).
-
#initialize(book_id:, name:, abbrevs: [], alternatives: []) ⇒ Bookname
constructor
A new instance of Bookname.
- #inspect ⇒ Object
- #to_s ⇒ Object (also: #to_str)
Constructor Details
#initialize(book_id:, name:, abbrevs: [], alternatives: []) ⇒ Bookname
Returns a new instance of Bookname.
14 15 16 17 18 19 |
# File 'lib/scripref/bookname.rb', line 14 def initialize book_id:, name:, abbrevs: [], alternatives: [] @book_id = book_id @name = name @abbrevs = Array(abbrevs) @alternatives = Array(alternatives) end |
Instance Attribute Details
#book_id ⇒ Object (readonly)
Returns the value of attribute book_id.
8 9 10 |
# File 'lib/scripref/bookname.rb', line 8 def book_id @book_id end |
Class Method Details
.parse(s) ⇒ Object
Helper method to create Bookname instances from a String format: [book_id]: [full name]|[abbrev1]|[abbrev2]|...(, [alternative name]|[abbrev1],...]*
94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/scripref/bookname.rb', line 94 def parse s book_id, rest = s.split(/:\s*/).map(&:strip) book_id = book_id.to_sym org, *alternatives = rest.split(/,\s*/).map(&:strip) name, *abbrevs = org.split('|').map(&:strip) alternatives.map! do |s| n, *a = s.split('|').map(&:strip) Bookname.new(book_id: book_id, name: n, abbrevs: a) end Bookname.new(book_id: book_id, name: name, abbrevs: abbrevs, alternatives: alternatives) end |
Instance Method Details
#==(other) ⇒ Object
79 80 81 |
# File 'lib/scripref/bookname.rb', line 79 def == other other.respond_to?(:dump) && dump == other.dump end |
#dump ⇒ Object
Convert the instance to an equivalent string representation
70 71 72 73 |
# File 'lib/scripref/bookname.rb', line 70 def dump s = Kernel.format('%s: %s', @book_id, [@name, @abbrevs].flatten.join('|')) [s, @alternatives.map(&:dump).map {|s| s.sub(/^.+?: /, '')}].flatten.join(', ') end |
#each_name ⇒ Object
Enumerator over all names (name and name of alternatives)
35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/scripref/bookname.rb', line 35 def each_name if block_given? yield @name @alternatives.each do |bn| bn.each_name do |alt_name| yield alt_name end end else enum_for :each_name end end |
#each_string ⇒ Object
Enumerator over all names and abbreviations (including alternatives)
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/scripref/bookname.rb', line 49 def each_string if block_given? yield @name @abbrevs.each do |a| yield a end @alternatives.each do |bn| bn.each_string do |s| yield s end end else enum_for :each_string end end |
#format(abbrev_level: 0) ⇒ Object
Format the bookname (full name or abbreviation)
23 24 25 26 27 28 29 30 31 32 |
# File 'lib/scripref/bookname.rb', line 23 def format abbrev_level: 0 case when abbrev_level == 0 @name when abbrev_level > 0 @abbrevs[abbrev_level - 1] || @abbrevs[-1] || @name else fail ArgumentError, 'negative values for abbrev_level are not allowed' end end |
#inspect ⇒ Object
75 76 77 |
# File 'lib/scripref/bookname.rb', line 75 def inspect Kernel.format('#<%s %s>', self.class, dump.inspect) end |
#to_s ⇒ Object Also known as: to_str
65 66 67 |
# File 'lib/scripref/bookname.rb', line 65 def to_s @name end |