Class: Scripref::Bookname

Inherits:
Object
  • Object
show all
Defined in:
lib/scripref/bookname.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(book_id:, name:, abbrevs: [], alternatives: []) ⇒ Bookname

Returns a new instance of Bookname.

Parameters:

  • book_id —

    OSID-ID of the book

  • name —

    full name of the book

  • abbrevs (defaults to: []) —

    possible abbreviations for the book

  • alternatives (defaults to: []) —

    further Bookname instances with alternative names and abbreviations for book



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],...]*

Examples:

Format

Zeph: Zefanja|Zef, Zephanja|Zeph

Code equivalent

Bookname.new(book_id: :Zeph, name: 'Zefanja', abbrevs: ['Zef'], alternatives: Bookname.new(book_id: :Zeph, name: 'Zephanja', abbrevs: ['Zeph']))

Parameters:

  • s —

    string to parse



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)

Parameters:

  • abbrev_level (defaults to: 0) —

    if 0 full name, if >0 an 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